summaryrefslogtreecommitdiff
path: root/src/lib/libc/stdlib
diff options
context:
space:
mode:
authord <>1998-11-20 11:18:51 +0000
committerd <>1998-11-20 11:18:51 +0000
commitc1f295aa8666eb1c08a1edf944ac5617659a066c (patch)
treeefe0a5dfb4f9767f96ab03ce9c00dc5bcc98074c /src/lib/libc/stdlib
parent5a11336d3f08469f2747ebcf26ae777dba46fd22 (diff)
downloadopenbsd-c1f295aa8666eb1c08a1edf944ac5617659a066c.tar.gz
openbsd-c1f295aa8666eb1c08a1edf944ac5617659a066c.tar.bz2
openbsd-c1f295aa8666eb1c08a1edf944ac5617659a066c.zip
Add thread-safety to libc, so that libc_r will build (on i386 at least).
All POSIX libc api now there (to P1003.1c/D10) (more md stuff is needed for other libc/arch/*) (setlogin is no longer a special syscall) Add -pthread option to gcc (that makes it use -lc_r and -D_POSIX_THREADS). Doc some re-entrant routines Add libc_r to intro(3) dig() uses some libc srcs and an extra -I was needed there. Add more md stuff to libc_r. Update includes for the pthreads api Update libc_r TODO
Diffstat (limited to 'src/lib/libc/stdlib')
-rw-r--r--src/lib/libc/stdlib/Makefile.inc2
-rw-r--r--src/lib/libc/stdlib/abort.c11
-rw-r--r--src/lib/libc/stdlib/exit.c11
-rw-r--r--src/lib/libc/stdlib/malloc.c30
-rw-r--r--src/lib/libc/stdlib/rand.319
-rw-r--r--src/lib/libc/stdlib/rand.c12
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
6SRCS+= a64l.c abort.c atexit.c atoi.c atof.c atol.c bsearch.c calloc.c \ 6SRCS+= 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)
35static char *rcsid = "$OpenBSD: abort.c,v 1.5 1997/06/22 20:21:25 tholo Exp $"; 35static 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
42void (*__cleanup)(); 43void (*__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)
35static char *rcsid = "$OpenBSD: exit.c,v 1.2 1996/08/19 08:33:30 tholo Exp $"; 35static 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 $";
42void (*__cleanup)(); 42void (*__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 */
51int __isthreaded = 0;
52
53/*
45 * Exit, flushing stdio buffers if necessary. 54 * Exit, flushing stdio buffers if necessary.
46 */ 55 */
47void 56void
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)
11static char rcsid[] = "$OpenBSD: malloc.c,v 1.32 1998/08/06 16:26:32 millert Exp $"; 11static 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"
91static 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
53These interfaces are obsoleted by 55These interfaces are obsoleted by
@@ -73,6 +75,14 @@ with the same seed value.
73.Pp 75.Pp
74If no seed value is provided, the functions are automatically 76If no seed value is provided, the functions are automatically
75seeded with a value of 1. 77seeded with a value of 1.
78.Pp
79The
80.Fn rand_r
81is a thread-safe version of
82.Fn rand .
83Storage for the seed must be provided through the
84.Ar seed
85argument, 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
85functions 95functions
86conform to 96conform to
87.St -ansiC . 97.St -ansiC .
98.Pp
99The
100.Fn rand_r
101function
102conforms to ISO/IEC 9945-1 ANSI/IEEE
103.Pq Dq Tn POSIX
104Std 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)
35static char *rcsid = "$OpenBSD: rand.c,v 1.2 1996/08/19 08:33:44 tholo Exp $"; 35static 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 $";
41static u_long next = 1; 41static u_long next = 1;
42 42
43int 43int
44rand_r(seed)
45u_int *seed;
46{
47 *seed = *seed * 1103515245 + 12345;
48 return (u_int)(*seed / 65536) % ((u_int)RAND_MAX + 1);
49}
50
51int
44rand() 52rand()
45{ 53{
46 return ((next = next * 1103515245 + 12345) % ((u_int)RAND_MAX + 1)); 54 return rand_r(&next);
47} 55}
48 56
49void 57void