diff options
Diffstat (limited to 'libc/stdlib/random.c')
-rw-r--r-- | libc/stdlib/random.c | 23 |
1 files changed, 11 insertions, 12 deletions
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 <pthread.h> /* 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 <bits/uClibc_mutex.h> +__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) |