From 1478c2de052374c6356db5513749a144c13791b1 Mon Sep 17 00:00:00 2001 From: Eric Andersen Date: Thu, 7 Dec 2006 23:24:02 +0000 Subject: Major cleanup of internal mutex locking. Be more consistant in how we do things, and avoid potential deadlocks caused when a thread holding a uClibc internal lock get canceled and terminates without releasing the lock. This change also provides a single place, bits/uClibc_mutex.h, for thread libraries to modify to change all instances of internal locking. --- libc/stdlib/random.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) (limited to 'libc/stdlib/random.c') diff --git a/libc/stdlib/random.c b/libc/stdlib/random.c index 9f1977ee3..3eb8aed8a 100644 --- a/libc/stdlib/random.c +++ b/libc/stdlib/random.c @@ -32,13 +32,12 @@ libc_hidden_proto(srandom_r) libc_hidden_proto(setstate_r) libc_hidden_proto(initstate_r) -#ifdef __UCLIBC_HAS_THREADS__ -# include /* POSIX.1c requires that there is mutual exclusion for the `rand' and `srand' functions to prevent concurrent calls from modifying common data. */ -static pthread_mutex_t lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; -#endif +#include +__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP); + /* An improved random number generation package. In addition to the standard rand()/srand() like interface, this package also has a special state info @@ -186,9 +185,9 @@ static struct random_data unsafe_state = for default usage relies on values produced by this routine. */ void srandom (unsigned int x) { - __pthread_mutex_lock(&lock); + __UCLIBC_MUTEX_LOCK(mylock); srandom_r (x, &unsafe_state); - __pthread_mutex_unlock(&lock); + __UCLIBC_MUTEX_UNLOCK(mylock); } strong_alias(srandom,srand) @@ -207,10 +206,10 @@ char * initstate (unsigned int seed, char *arg_state, size_t n) { int32_t *ostate; - __pthread_mutex_lock(&lock); + __UCLIBC_MUTEX_LOCK(mylock); ostate = &unsafe_state.state[-1]; initstate_r (seed, arg_state, n, &unsafe_state); - __pthread_mutex_unlock(&lock); + __UCLIBC_MUTEX_UNLOCK(mylock); return (char *) ostate; } @@ -226,11 +225,11 @@ char * setstate (char *arg_state) { int32_t *ostate; - __pthread_mutex_lock(&lock); + __UCLIBC_MUTEX_LOCK(mylock); ostate = &unsafe_state.state[-1]; if (setstate_r (arg_state, &unsafe_state) < 0) ostate = NULL; - __pthread_mutex_unlock(&lock); + __UCLIBC_MUTEX_UNLOCK(mylock); return (char *) ostate; } @@ -250,9 +249,9 @@ long int random (void) { int32_t retval; - __pthread_mutex_lock(&lock); + __UCLIBC_MUTEX_LOCK(mylock); random_r (&unsafe_state, &retval); - __pthread_mutex_unlock(&lock); + __UCLIBC_MUTEX_UNLOCK(mylock); return retval; } libc_hidden_def(random) -- cgit v1.2.3