apps/testing/ostest: Add test of pthread_mutex_timedlock()

This commit is contained in:
Gregory Nutt 2019-02-24 15:43:45 -06:00
parent 5492fcdafa
commit 6d631c360d
4 changed files with 243 additions and 3 deletions

View File

@ -79,9 +79,9 @@ CSRCS += waitpid.c
endif
ifneq ($(CONFIG_DISABLE_PTHREAD),y)
CSRCS += cancel.c cond.c mutex.c sem.c semtimed.c barrier.c timedwait.c
CSRCS += pthread_rwlock.c
CSRCS += pthread_rwlock_cancel.c
CSRCS += cancel.c cond.c mutex.c timedmutex.c sem.c semtimed.c barrier.c
CSRCS += timedwait.c
CSRCS += pthread_rwlock.c pthread_rwlock_cancel.c
ifneq ($(CONFIG_NPTHREAD_KEYS),0)
CSRCS += specific.c

View File

@ -150,6 +150,10 @@ int waitpid_test(void);
void mutex_test(void);
/* timedmutex.c *************************************************************/
void timedmutex_test(void);
/* rmutex.c *****************************************************************/
void recursive_mutex_test(void);

View File

@ -363,6 +363,10 @@ static int user_main(int argc, char *argv[])
printf("\nuser_main: mutex test\n");
mutex_test();
check_test_memory_usage();
printf("\nuser_main: timed mutex test\n");
timedmutex_test();
check_test_memory_usage();
#endif
#if !defined(CONFIG_DISABLE_PTHREAD) && defined(CONFIG_PTHREAD_MUTEX_TYPES)

232
testing/ostest/timedmutex.c Normal file
View File

@ -0,0 +1,232 @@
/****************************************************************************
* apps/testing/ostest/timedmutex.c
*
* Copyright (C) 2019 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <stdbool.h>
#include <stdio.h>
#include <pthread.h>
#include <time.h>
#include "ostest.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#ifndef NULL
# define NULL (void*)0
#endif
#ifndef OK
# define OK 0
#endif
/****************************************************************************
* 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);
if (status != OK)
{
int errcode = errno;
fprintf(stderr, "pthread: clock_gettime() failed: %d\n", errcode);
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)
{
if (status == EAGAIN)
{
printf("pthread: Got the timeout. Terminating\n");
}
else
{
fprintf(stderr, "pthread: clock_gettime() failed: %d\n", status);
}
g_result = status;
break;
}
printf("pthread: Got the lock\n");
/* Release the lock and wait a bit in case the main thread wants it. */
(void)pthread_mutex_unlock(&g_mutex);
usleep(500*1000);
}
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)
{
fprintf(stderr, "mutex_test: Failed to get mutex: %d\n", status);
goto errout_with_mutex;
}
/* Start a thread */
printf("mutex_test: Starting thread\n");
#ifdef SDCC
(void)pthread_attr_init(&attr);
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)
{
fprintf(stderr, "mutex_test: Error in thread creation: %d\n", status);
goto errout_with_lock;
}
/* Wait a bit to assure that the thread gets a chance to start */
usleep(500*1000);
/* Then unlock the mutex. This should wake up the pthread. */
printf("mutex_test: Unlocking\n");
(void)pthread_mutex_unlock(&g_mutex);
/* 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)
*/
usleep(500*1000);
/* 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)
{
fprintf(stderr, "mutex_test: Failed to get mutex: %d\n", status);
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");
}
else if (g_result != EAGAIN)
{
fprintf(stderr, "mutex_test: ERROR: Result of timeout was not EAGAIN: %d\n",
g_result);
}
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:
(void)pthread_mutex_unlock(&g_mutex);
errout_with_mutex:
(void)pthread_mutex_destroy(&g_mutex);
}