summaryrefslogtreecommitdiff
path: root/libc/stdlib/random.c
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2006-12-07 23:24:02 +0000
committerEric Andersen <andersen@codepoet.org>2006-12-07 23:24:02 +0000
commit1478c2de052374c6356db5513749a144c13791b1 (patch)
tree3b22a3f8361f94c99508c497e240ecb71acf8641 /libc/stdlib/random.c
parent99d6c367c4820a072dc4ada51561df17e2093778 (diff)
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.
Diffstat (limited to 'libc/stdlib/random.c')
-rw-r--r--libc/stdlib/random.c23
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)