diff options
Diffstat (limited to 'src/lib/libc/stdlib')
-rw-r--r-- | src/lib/libc/stdlib/Makefile.inc | 2 | ||||
-rw-r--r-- | src/lib/libc/stdlib/abort.c | 11 | ||||
-rw-r--r-- | src/lib/libc/stdlib/exit.c | 11 | ||||
-rw-r--r-- | src/lib/libc/stdlib/malloc.c | 30 | ||||
-rw-r--r-- | src/lib/libc/stdlib/rand.3 | 19 | ||||
-rw-r--r-- | src/lib/libc/stdlib/rand.c | 12 |
6 files changed, 70 insertions, 15 deletions
diff --git a/src/lib/libc/stdlib/Makefile.inc b/src/lib/libc/stdlib/Makefile.inc index bdd5280dde..3191f08328 100644 --- a/src/lib/libc/stdlib/Makefile.inc +++ b/src/lib/libc/stdlib/Makefile.inc | |||
@@ -1,7 +1,7 @@ | |||
1 | # $OpenBDS: Makefile.inc,v 1.6 1996/08/21 03:47:21 tholo Exp $ | 1 | # $OpenBDS: Makefile.inc,v 1.6 1996/08/21 03:47:21 tholo Exp $ |
2 | 2 | ||
3 | # stdlib sources | 3 | # stdlib sources |
4 | .PATH: ${.CURDIR}/arch/${MACHINE_ARCH}/stdlib ${.CURDIR}/stdlib | 4 | .PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/stdlib ${LIBCSRCDIR}/stdlib |
5 | 5 | ||
6 | SRCS+= a64l.c abort.c atexit.c atoi.c atof.c atol.c bsearch.c calloc.c \ | 6 | SRCS+= a64l.c abort.c atexit.c atoi.c atof.c atol.c bsearch.c calloc.c \ |
7 | cfree.c exit.c getenv.c getopt.c getsubopt.c heapsort.c l64a.c \ | 7 | cfree.c exit.c getenv.c getopt.c getsubopt.c heapsort.c l64a.c \ |
diff --git a/src/lib/libc/stdlib/abort.c b/src/lib/libc/stdlib/abort.c index 4ea8a2ca4b..4cc6257acb 100644 --- a/src/lib/libc/stdlib/abort.c +++ b/src/lib/libc/stdlib/abort.c | |||
@@ -32,12 +32,13 @@ | |||
32 | */ | 32 | */ |
33 | 33 | ||
34 | #if defined(LIBC_SCCS) && !defined(lint) | 34 | #if defined(LIBC_SCCS) && !defined(lint) |
35 | static char *rcsid = "$OpenBSD: abort.c,v 1.5 1997/06/22 20:21:25 tholo Exp $"; | 35 | static char *rcsid = "$OpenBSD: abort.c,v 1.6 1998/11/20 11:18:49 d Exp $"; |
36 | #endif /* LIBC_SCCS and not lint */ | 36 | #endif /* LIBC_SCCS and not lint */ |
37 | 37 | ||
38 | #include <signal.h> | 38 | #include <signal.h> |
39 | #include <stdlib.h> | 39 | #include <stdlib.h> |
40 | #include <unistd.h> | 40 | #include <unistd.h> |
41 | #include "thread_private.h" | ||
41 | 42 | ||
42 | void (*__cleanup)(); | 43 | void (*__cleanup)(); |
43 | 44 | ||
@@ -54,7 +55,11 @@ abort() | |||
54 | * any errors -- X311J doesn't allow abort to return anyway. | 55 | * any errors -- X311J doesn't allow abort to return anyway. |
55 | */ | 56 | */ |
56 | sigdelset(&mask, SIGABRT); | 57 | sigdelset(&mask, SIGABRT); |
58 | #ifdef _THREAD_SAFE | ||
59 | (void)_thread_sys_sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL); | ||
60 | #else _THREAD_SAFE | ||
57 | (void)sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL); | 61 | (void)sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL); |
62 | #endif _THREAD_SAFE | ||
58 | 63 | ||
59 | /* | 64 | /* |
60 | * POSIX requires we flush stdio buffers on abort | 65 | * POSIX requires we flush stdio buffers on abort |
@@ -71,7 +76,11 @@ abort() | |||
71 | * it again, only harder. | 76 | * it again, only harder. |
72 | */ | 77 | */ |
73 | (void)signal(SIGABRT, SIG_DFL); | 78 | (void)signal(SIGABRT, SIG_DFL); |
79 | #ifdef _THREAD_SAFE | ||
80 | (void)_thread_sys_sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL); | ||
81 | #else _THREAD_SAFE | ||
74 | (void)sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL); | 82 | (void)sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL); |
83 | #endif _THREAD_SAFE | ||
75 | (void)kill(getpid(), SIGABRT); | 84 | (void)kill(getpid(), SIGABRT); |
76 | exit(1); | 85 | exit(1); |
77 | } | 86 | } |
diff --git a/src/lib/libc/stdlib/exit.c b/src/lib/libc/stdlib/exit.c index e358c94fd6..bc7fd395ca 100644 --- a/src/lib/libc/stdlib/exit.c +++ b/src/lib/libc/stdlib/exit.c | |||
@@ -32,7 +32,7 @@ | |||
32 | */ | 32 | */ |
33 | 33 | ||
34 | #if defined(LIBC_SCCS) && !defined(lint) | 34 | #if defined(LIBC_SCCS) && !defined(lint) |
35 | static char *rcsid = "$OpenBSD: exit.c,v 1.2 1996/08/19 08:33:30 tholo Exp $"; | 35 | static char *rcsid = "$OpenBSD: exit.c,v 1.3 1998/11/20 11:18:50 d Exp $"; |
36 | #endif /* LIBC_SCCS and not lint */ | 36 | #endif /* LIBC_SCCS and not lint */ |
37 | 37 | ||
38 | #include <stdlib.h> | 38 | #include <stdlib.h> |
@@ -42,6 +42,15 @@ static char *rcsid = "$OpenBSD: exit.c,v 1.2 1996/08/19 08:33:30 tholo Exp $"; | |||
42 | void (*__cleanup)(); | 42 | void (*__cleanup)(); |
43 | 43 | ||
44 | /* | 44 | /* |
45 | * This variable is zero until a process has created a thread. | ||
46 | * It is used to avoid calling locking functions in libc when they | ||
47 | * are not required. By default, libc is intended to be(come) | ||
48 | * thread-safe, but without a (significant) penalty to non-threaded | ||
49 | * processes. | ||
50 | */ | ||
51 | int __isthreaded = 0; | ||
52 | |||
53 | /* | ||
45 | * Exit, flushing stdio buffers if necessary. | 54 | * Exit, flushing stdio buffers if necessary. |
46 | */ | 55 | */ |
47 | void | 56 | void |
diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c index d1d8759791..ecbf93dc48 100644 --- a/src/lib/libc/stdlib/malloc.c +++ b/src/lib/libc/stdlib/malloc.c | |||
@@ -8,7 +8,7 @@ | |||
8 | */ | 8 | */ |
9 | 9 | ||
10 | #if defined(LIBC_SCCS) && !defined(lint) | 10 | #if defined(LIBC_SCCS) && !defined(lint) |
11 | static char rcsid[] = "$OpenBSD: malloc.c,v 1.32 1998/08/06 16:26:32 millert Exp $"; | 11 | static char rcsid[] = "$OpenBSD: malloc.c,v 1.33 1998/11/20 11:18:50 d Exp $"; |
12 | #endif /* LIBC_SCCS and not lint */ | 12 | #endif /* LIBC_SCCS and not lint */ |
13 | 13 | ||
14 | /* | 14 | /* |
@@ -87,15 +87,27 @@ static char rcsid[] = "$OpenBSD: malloc.c,v 1.32 1998/08/06 16:26:32 millert Exp | |||
87 | #endif /* __OpenBSD__ */ | 87 | #endif /* __OpenBSD__ */ |
88 | 88 | ||
89 | #ifdef _THREAD_SAFE | 89 | #ifdef _THREAD_SAFE |
90 | #include <pthread.h> | 90 | # include "thread_private.h" |
91 | static pthread_mutex_t malloc_lock; | 91 | # if 0 |
92 | #define THREAD_LOCK() pthread_mutex_lock(&malloc_lock) | 92 | /* kernel threads */ |
93 | #define THREAD_UNLOCK() pthread_mutex_unlock(&malloc_lock) | 93 | # include <pthread.h> |
94 | #define THREAD_LOCK_INIT() pthread_mutex_init(&malloc_lock, 0); | 94 | static pthread_mutex_t malloc_lock; |
95 | # define THREAD_LOCK() pthread_mutex_lock(&malloc_lock) | ||
96 | # define THREAD_UNLOCK() pthread_mutex_unlock(&malloc_lock) | ||
97 | # define THREAD_LOCK_INIT() pthread_mutex_init(&malloc_lock, 0); | ||
98 | # else | ||
99 | /* user threads */ | ||
100 | # include "spinlock.h" | ||
101 | static spinlock_t malloc_lock = _SPINLOCK_INITIALIZER; | ||
102 | # define THREAD_LOCK() if (__isthreaded) _SPINLOCK(&malloc_lock) | ||
103 | # define THREAD_UNLOCK() if (__isthreaded) _SPINUNLOCK(&malloc_lock) | ||
104 | # define THREAD_LOCK_INIT() | ||
105 | # endif | ||
95 | #else | 106 | #else |
96 | #define THREAD_LOCK() | 107 | /* no threads */ |
97 | #define THREAD_UNLOCK() | 108 | # define THREAD_LOCK() |
98 | #define THREAD_LOCK_INIT() | 109 | # define THREAD_UNLOCK() |
110 | # define THREAD_LOCK_INIT() | ||
99 | #endif | 111 | #endif |
100 | 112 | ||
101 | /* | 113 | /* |
diff --git a/src/lib/libc/stdlib/rand.3 b/src/lib/libc/stdlib/rand.3 index 32d32761f1..28496ec12a 100644 --- a/src/lib/libc/stdlib/rand.3 +++ b/src/lib/libc/stdlib/rand.3 | |||
@@ -33,7 +33,7 @@ | |||
33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
34 | .\" SUCH DAMAGE. | 34 | .\" SUCH DAMAGE. |
35 | .\" | 35 | .\" |
36 | .\" $OpenBSD: rand.3,v 1.4 1998/07/05 19:54:22 millert Exp $ | 36 | .\" $OpenBSD: rand.3,v 1.5 1998/11/20 11:18:50 d Exp $ |
37 | .\" | 37 | .\" |
38 | .Dd June 29, 1991 | 38 | .Dd June 29, 1991 |
39 | .Dt RAND 3 | 39 | .Dt RAND 3 |
@@ -48,6 +48,8 @@ | |||
48 | .Fn srand "unsigned seed" | 48 | .Fn srand "unsigned seed" |
49 | .Ft int | 49 | .Ft int |
50 | .Fn rand void | 50 | .Fn rand void |
51 | .Ft int | ||
52 | .Fn rand_r "unsigned int *seed" | ||
51 | .Sh DESCRIPTION | 53 | .Sh DESCRIPTION |
52 | .Bf -symbolic | 54 | .Bf -symbolic |
53 | These interfaces are obsoleted by | 55 | These interfaces are obsoleted by |
@@ -73,6 +75,14 @@ with the same seed value. | |||
73 | .Pp | 75 | .Pp |
74 | If no seed value is provided, the functions are automatically | 76 | If no seed value is provided, the functions are automatically |
75 | seeded with a value of 1. | 77 | seeded with a value of 1. |
78 | .Pp | ||
79 | The | ||
80 | .Fn rand_r | ||
81 | is a thread-safe version of | ||
82 | .Fn rand . | ||
83 | Storage for the seed must be provided through the | ||
84 | .Ar seed | ||
85 | argument, and needs to have been initialized by the caller. | ||
76 | .Sh SEE ALSO | 86 | .Sh SEE ALSO |
77 | .Xr arc4random 3 , | 87 | .Xr arc4random 3 , |
78 | .Xr rand48 3 , | 88 | .Xr rand48 3 , |
@@ -85,3 +95,10 @@ and | |||
85 | functions | 95 | functions |
86 | conform to | 96 | conform to |
87 | .St -ansiC . | 97 | .St -ansiC . |
98 | .Pp | ||
99 | The | ||
100 | .Fn rand_r | ||
101 | function | ||
102 | conforms to ISO/IEC 9945-1 ANSI/IEEE | ||
103 | .Pq Dq Tn POSIX | ||
104 | Std 1003.1c Draft 10. | ||
diff --git a/src/lib/libc/stdlib/rand.c b/src/lib/libc/stdlib/rand.c index f270ffd986..61fb66e5ec 100644 --- a/src/lib/libc/stdlib/rand.c +++ b/src/lib/libc/stdlib/rand.c | |||
@@ -32,7 +32,7 @@ | |||
32 | */ | 32 | */ |
33 | 33 | ||
34 | #if defined(LIBC_SCCS) && !defined(lint) | 34 | #if defined(LIBC_SCCS) && !defined(lint) |
35 | static char *rcsid = "$OpenBSD: rand.c,v 1.2 1996/08/19 08:33:44 tholo Exp $"; | 35 | static char *rcsid = "$OpenBSD: rand.c,v 1.3 1998/11/20 11:18:50 d Exp $"; |
36 | #endif /* LIBC_SCCS and not lint */ | 36 | #endif /* LIBC_SCCS and not lint */ |
37 | 37 | ||
38 | #include <sys/types.h> | 38 | #include <sys/types.h> |
@@ -41,9 +41,17 @@ static char *rcsid = "$OpenBSD: rand.c,v 1.2 1996/08/19 08:33:44 tholo Exp $"; | |||
41 | static u_long next = 1; | 41 | static u_long next = 1; |
42 | 42 | ||
43 | int | 43 | int |
44 | rand_r(seed) | ||
45 | u_int *seed; | ||
46 | { | ||
47 | *seed = *seed * 1103515245 + 12345; | ||
48 | return (u_int)(*seed / 65536) % ((u_int)RAND_MAX + 1); | ||
49 | } | ||
50 | |||
51 | int | ||
44 | rand() | 52 | rand() |
45 | { | 53 | { |
46 | return ((next = next * 1103515245 + 12345) % ((u_int)RAND_MAX + 1)); | 54 | return rand_r(&next); |
47 | } | 55 | } |
48 | 56 | ||
49 | void | 57 | void |