sched/pthread: Implement pthread_mutex_trylock() for recursive mutexes
This commit is contained in:
parent
2ba737b5e5
commit
2f67a661e7
@ -168,7 +168,7 @@ static void restart_thread(pthread_t *waiter, int cancelable)
|
||||
/* Destroy the mutex */
|
||||
|
||||
printf("restart_thread: Destroying mutex\n");
|
||||
status = pthread_cond_destroy(&cond);
|
||||
status = pthread_mutex_destroy(&mutex);
|
||||
if (status != 0)
|
||||
{
|
||||
printf("restart_thread: ERROR pthread_mutex_destroy failed, status=%d\n", status);
|
||||
|
@ -37,10 +37,6 @@
|
||||
#include <pthread.h>
|
||||
#include "ostest.h"
|
||||
|
||||
#ifndef NULL
|
||||
# define NULL (void*)0
|
||||
#endif
|
||||
|
||||
#define NTHREADS 3
|
||||
#define NLOOPS 3
|
||||
#define NRECURSIONS 3
|
||||
@ -63,6 +59,26 @@ static void thread_inner(int id, int level)
|
||||
}
|
||||
printf("thread_inner[%d, %d]: Locked\n", id, level);
|
||||
|
||||
/* Try-lock already locked recursive mutex. */
|
||||
|
||||
status = pthread_mutex_trylock(&mut);
|
||||
if (status != 0)
|
||||
{
|
||||
printf("thread_inner[%d, %d]: ERROR pthread_mutex_trylock failed: %d\n",
|
||||
id, level, status);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Unlock the try-lock. */
|
||||
|
||||
status = pthread_mutex_unlock(&mut);
|
||||
if (status != 0)
|
||||
{
|
||||
printf("thread_inner[%d, %d]: ERROR pthread_mutex_unlock after try-lock failed: %d\n",
|
||||
id, level, status);
|
||||
}
|
||||
}
|
||||
|
||||
/* Give the other threads a chance */
|
||||
|
||||
pthread_yield();
|
||||
@ -131,7 +147,11 @@ void recursive_mutex_test(void)
|
||||
/* Initialize the mutex */
|
||||
|
||||
printf("recursive_mutex_test: Initializing mutex\n");
|
||||
pthread_mutex_init(&mut, &mattr);
|
||||
status = pthread_mutex_init(&mut, &mattr);
|
||||
if (status != 0)
|
||||
{
|
||||
printf("recursive_mutex_test: ERROR pthread_mutex_init failed, status=%d\n", status);
|
||||
}
|
||||
|
||||
/* Start the threads -- all at the same, default priority */
|
||||
|
||||
@ -146,7 +166,7 @@ void recursive_mutex_test(void)
|
||||
#endif
|
||||
if (status != 0)
|
||||
{
|
||||
printf("recursive_mutex_test: ERRROR thread#%d creation: %d\n", i+1, status);
|
||||
printf("recursive_mutex_test: ERROR thread#%d creation: %d\n", i+1, status);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user