2019-02-24 22:43:45 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/testing/ostest/timedmutex.c
|
|
|
|
*
|
2021-06-10 07:51:27 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2019-02-24 22:43:45 +01:00
|
|
|
*
|
2021-06-10 07:51:27 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2019-02-24 22:43:45 +01:00
|
|
|
*
|
2021-06-10 07:51:27 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2019-02-24 22:43:45 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-03-16 03:35:15 +01:00
|
|
|
#include <assert.h>
|
2021-05-17 18:53:15 +02:00
|
|
|
#include <errno.h>
|
2023-03-16 03:35:15 +01:00
|
|
|
#include <pthread.h>
|
2019-02-24 22:43:45 +01:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
2023-02-04 14:56:06 +01:00
|
|
|
#include <unistd.h>
|
2021-05-17 18:53:15 +02:00
|
|
|
|
2019-02-24 22:43:45 +01:00
|
|
|
#include "ostest.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static pthread_mutex_t g_mutex;
|
|
|
|
static bool g_running;
|
|
|
|
static int g_result;
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void *thread_func(FAR void *parameter)
|
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
printf("pthread: Started\n");
|
|
|
|
g_running = true;
|
|
|
|
|
|
|
|
for (; ; )
|
|
|
|
{
|
|
|
|
printf("pthread: Waiting for lock or timeout\n");
|
|
|
|
|
|
|
|
/* Get the current time */
|
|
|
|
|
|
|
|
status = clock_gettime(CLOCK_REALTIME, &ts);
|
2019-02-26 01:14:12 +01:00
|
|
|
if (status < 0)
|
2019-02-24 22:43:45 +01:00
|
|
|
{
|
|
|
|
int errcode = errno;
|
2022-10-15 16:36:29 +02:00
|
|
|
fprintf(stderr, "pthread: "
|
|
|
|
"ERROR clock_gettime() failed: %d\n", errcode);
|
|
|
|
ASSERT(false);
|
2019-02-24 22:43:45 +01:00
|
|
|
g_result = errcode;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a time two seconds in the future (we presume that we cannot be
|
|
|
|
* blocked for two seconds here!)
|
|
|
|
*/
|
|
|
|
|
|
|
|
ts.tv_sec += 2;
|
|
|
|
|
|
|
|
/* Now wait until either we get the lock or until the timeout occurs */
|
|
|
|
|
|
|
|
status = pthread_mutex_timedlock(&g_mutex, &ts);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
2019-02-26 01:14:12 +01:00
|
|
|
if (status == ETIMEDOUT)
|
2019-02-24 22:43:45 +01:00
|
|
|
{
|
|
|
|
printf("pthread: Got the timeout. Terminating\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-10-15 16:36:29 +02:00
|
|
|
fprintf(stderr, "pthread: "
|
|
|
|
"ERROR pthread_mutex_timedlock() failed: %d\n",
|
2019-02-26 01:14:12 +01:00
|
|
|
status);
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2019-02-24 22:43:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
g_result = status;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("pthread: Got the lock\n");
|
|
|
|
|
|
|
|
/* Release the lock and wait a bit in case the main thread wants it. */
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
pthread_mutex_unlock(&g_mutex);
|
2021-04-30 01:58:23 +02:00
|
|
|
usleep(500 * 1000);
|
2019-02-24 22:43:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
g_running = false;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void timedmutex_test(void)
|
|
|
|
{
|
|
|
|
pthread_t thread;
|
|
|
|
#ifdef SDCC
|
|
|
|
pthread_addr_t result;
|
|
|
|
#endif
|
|
|
|
int status;
|
|
|
|
|
|
|
|
/* Initialize the mutex */
|
|
|
|
|
|
|
|
printf("mutex_test: Initializing mutex\n");
|
|
|
|
pthread_mutex_init(&g_mutex, NULL);
|
|
|
|
|
|
|
|
/* Lock the mutex */
|
|
|
|
|
|
|
|
status = pthread_mutex_lock(&g_mutex);
|
|
|
|
if (status != OK)
|
|
|
|
{
|
2022-10-15 16:36:29 +02:00
|
|
|
fprintf(stderr, "mutex_test: ERROR Failed to get mutex: %d\n", status);
|
|
|
|
ASSERT(false);
|
2019-02-24 22:43:45 +01:00
|
|
|
goto errout_with_mutex;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Start a thread */
|
|
|
|
|
|
|
|
printf("mutex_test: Starting thread\n");
|
|
|
|
#ifdef SDCC
|
2020-01-02 13:09:50 +01:00
|
|
|
pthread_attr_init(&attr);
|
2019-02-24 22:43:45 +01:00
|
|
|
status = pthread_create(&thread, &attr, thread_func, (pthread_addr_t)0);
|
|
|
|
#else
|
|
|
|
status = pthread_create(&thread, NULL, thread_func, (pthread_addr_t)0);
|
|
|
|
#endif
|
|
|
|
if (status != 0)
|
|
|
|
{
|
2021-04-30 00:41:05 +02:00
|
|
|
fprintf(stderr, "mutex_test: ERROR in thread creation: %d\n", status);
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2019-02-24 22:43:45 +01:00
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait a bit to assure that the thread gets a chance to start */
|
|
|
|
|
2021-04-30 01:58:23 +02:00
|
|
|
usleep(500 * 1000);
|
2019-02-24 22:43:45 +01:00
|
|
|
|
|
|
|
/* Then unlock the mutex. This should wake up the pthread. */
|
|
|
|
|
|
|
|
printf("mutex_test: Unlocking\n");
|
2020-01-02 13:09:50 +01:00
|
|
|
pthread_mutex_unlock(&g_mutex);
|
2019-02-24 22:43:45 +01:00
|
|
|
|
|
|
|
/* Wait a bit to assure that the thread gets a chance to lock the mutex at
|
|
|
|
* least once (it may probably loop and retake the mutex several times)
|
|
|
|
*/
|
|
|
|
|
2021-04-30 01:58:23 +02:00
|
|
|
usleep(500 * 1000);
|
2019-02-24 22:43:45 +01:00
|
|
|
|
|
|
|
/* The re-lock the mutex. The pthread should now be waiting for the lock
|
|
|
|
* or a timeout.
|
|
|
|
*/
|
|
|
|
|
|
|
|
status = pthread_mutex_lock(&g_mutex);
|
|
|
|
if (status != OK)
|
|
|
|
{
|
2022-10-15 16:36:29 +02:00
|
|
|
fprintf(stderr, "mutex_test: ERROR Failed to get mutex: %d\n", status);
|
|
|
|
ASSERT(false);
|
2019-02-24 22:43:45 +01:00
|
|
|
goto errout_with_mutex;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The pthread should timeout in two seconds. Let's wait four. At that
|
|
|
|
* time, the pthread should no longer be running.
|
|
|
|
*/
|
|
|
|
|
|
|
|
sleep(4);
|
|
|
|
|
|
|
|
/* The pthread should no longer be running and it should have terminated
|
|
|
|
* because of EAGAIN.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (g_running)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "mutex_test: ERROR: The pthread is still running!\n");
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2019-02-24 22:43:45 +01:00
|
|
|
}
|
2019-02-26 01:14:12 +01:00
|
|
|
else if (g_result != ETIMEDOUT)
|
2019-02-24 22:43:45 +01:00
|
|
|
{
|
2019-02-26 01:14:12 +01:00
|
|
|
fprintf(stderr, "mutex_test: ERROR: Result was not ETIMEDOUT: %d\n",
|
2019-02-24 22:43:45 +01:00
|
|
|
g_result);
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2019-02-24 22:43:45 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("mutex_test: PASSED\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Let's reap any join droppings left from the pthread */
|
|
|
|
|
|
|
|
#ifdef SDCC
|
|
|
|
pthread_join(thread, &result);
|
|
|
|
#else
|
|
|
|
pthread_join(thread, NULL);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
errout_with_lock:
|
2020-01-02 13:09:50 +01:00
|
|
|
pthread_mutex_unlock(&g_mutex);
|
2019-02-24 22:43:45 +01:00
|
|
|
errout_with_mutex:
|
2020-01-02 13:09:50 +01:00
|
|
|
pthread_mutex_destroy(&g_mutex);
|
2019-02-24 22:43:45 +01:00
|
|
|
}
|