From c1f295aa8666eb1c08a1edf944ac5617659a066c Mon Sep 17 00:00:00 2001 From: d <> Date: Fri, 20 Nov 1998 11:18:51 +0000 Subject: 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 --- src/lib/libc/stdlib/Makefile.inc | 2 +- src/lib/libc/stdlib/abort.c | 11 ++++++++++- src/lib/libc/stdlib/exit.c | 11 ++++++++++- src/lib/libc/stdlib/malloc.c | 30 +++++++++++++++++++++--------- src/lib/libc/stdlib/rand.3 | 19 ++++++++++++++++++- src/lib/libc/stdlib/rand.c | 12 ++++++++++-- 6 files changed, 70 insertions(+), 15 deletions(-) (limited to 'src/lib/libc/stdlib') 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 @@ # $OpenBDS: Makefile.inc,v 1.6 1996/08/21 03:47:21 tholo Exp $ # stdlib sources -.PATH: ${.CURDIR}/arch/${MACHINE_ARCH}/stdlib ${.CURDIR}/stdlib +.PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/stdlib ${LIBCSRCDIR}/stdlib SRCS+= a64l.c abort.c atexit.c atoi.c atof.c atol.c bsearch.c calloc.c \ 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 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char *rcsid = "$OpenBSD: abort.c,v 1.5 1997/06/22 20:21:25 tholo Exp $"; +static char *rcsid = "$OpenBSD: abort.c,v 1.6 1998/11/20 11:18:49 d Exp $"; #endif /* LIBC_SCCS and not lint */ #include #include #include +#include "thread_private.h" void (*__cleanup)(); @@ -54,7 +55,11 @@ abort() * any errors -- X311J doesn't allow abort to return anyway. */ sigdelset(&mask, SIGABRT); +#ifdef _THREAD_SAFE + (void)_thread_sys_sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL); +#else _THREAD_SAFE (void)sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL); +#endif _THREAD_SAFE /* * POSIX requires we flush stdio buffers on abort @@ -71,7 +76,11 @@ abort() * it again, only harder. */ (void)signal(SIGABRT, SIG_DFL); +#ifdef _THREAD_SAFE + (void)_thread_sys_sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL); +#else _THREAD_SAFE (void)sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL); +#endif _THREAD_SAFE (void)kill(getpid(), SIGABRT); exit(1); } 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 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char *rcsid = "$OpenBSD: exit.c,v 1.2 1996/08/19 08:33:30 tholo Exp $"; +static char *rcsid = "$OpenBSD: exit.c,v 1.3 1998/11/20 11:18:50 d Exp $"; #endif /* LIBC_SCCS and not lint */ #include @@ -41,6 +41,15 @@ static char *rcsid = "$OpenBSD: exit.c,v 1.2 1996/08/19 08:33:30 tholo Exp $"; void (*__cleanup)(); +/* + * This variable is zero until a process has created a thread. + * It is used to avoid calling locking functions in libc when they + * are not required. By default, libc is intended to be(come) + * thread-safe, but without a (significant) penalty to non-threaded + * processes. + */ +int __isthreaded = 0; + /* * Exit, flushing stdio buffers if necessary. */ 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 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: malloc.c,v 1.32 1998/08/06 16:26:32 millert Exp $"; +static char rcsid[] = "$OpenBSD: malloc.c,v 1.33 1998/11/20 11:18:50 d Exp $"; #endif /* LIBC_SCCS and not lint */ /* @@ -87,15 +87,27 @@ static char rcsid[] = "$OpenBSD: malloc.c,v 1.32 1998/08/06 16:26:32 millert Exp #endif /* __OpenBSD__ */ #ifdef _THREAD_SAFE -#include -static pthread_mutex_t malloc_lock; -#define THREAD_LOCK() pthread_mutex_lock(&malloc_lock) -#define THREAD_UNLOCK() pthread_mutex_unlock(&malloc_lock) -#define THREAD_LOCK_INIT() pthread_mutex_init(&malloc_lock, 0); +# include "thread_private.h" +# if 0 + /* kernel threads */ +# include + static pthread_mutex_t malloc_lock; +# define THREAD_LOCK() pthread_mutex_lock(&malloc_lock) +# define THREAD_UNLOCK() pthread_mutex_unlock(&malloc_lock) +# define THREAD_LOCK_INIT() pthread_mutex_init(&malloc_lock, 0); +# else + /* user threads */ +# include "spinlock.h" + static spinlock_t malloc_lock = _SPINLOCK_INITIALIZER; +# define THREAD_LOCK() if (__isthreaded) _SPINLOCK(&malloc_lock) +# define THREAD_UNLOCK() if (__isthreaded) _SPINUNLOCK(&malloc_lock) +# define THREAD_LOCK_INIT() +# endif #else -#define THREAD_LOCK() -#define THREAD_UNLOCK() -#define THREAD_LOCK_INIT() + /* no threads */ +# define THREAD_LOCK() +# define THREAD_UNLOCK() +# define THREAD_LOCK_INIT() #endif /* 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 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $OpenBSD: rand.3,v 1.4 1998/07/05 19:54:22 millert Exp $ +.\" $OpenBSD: rand.3,v 1.5 1998/11/20 11:18:50 d Exp $ .\" .Dd June 29, 1991 .Dt RAND 3 @@ -48,6 +48,8 @@ .Fn srand "unsigned seed" .Ft int .Fn rand void +.Ft int +.Fn rand_r "unsigned int *seed" .Sh DESCRIPTION .Bf -symbolic These interfaces are obsoleted by @@ -73,6 +75,14 @@ with the same seed value. .Pp If no seed value is provided, the functions are automatically seeded with a value of 1. +.Pp +The +.Fn rand_r +is a thread-safe version of +.Fn rand . +Storage for the seed must be provided through the +.Ar seed +argument, and needs to have been initialized by the caller. .Sh SEE ALSO .Xr arc4random 3 , .Xr rand48 3 , @@ -85,3 +95,10 @@ and functions conform to .St -ansiC . +.Pp +The +.Fn rand_r +function +conforms to ISO/IEC 9945-1 ANSI/IEEE +.Pq Dq Tn POSIX +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 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char *rcsid = "$OpenBSD: rand.c,v 1.2 1996/08/19 08:33:44 tholo Exp $"; +static char *rcsid = "$OpenBSD: rand.c,v 1.3 1998/11/20 11:18:50 d Exp $"; #endif /* LIBC_SCCS and not lint */ #include @@ -40,10 +40,18 @@ static char *rcsid = "$OpenBSD: rand.c,v 1.2 1996/08/19 08:33:44 tholo Exp $"; static u_long next = 1; +int +rand_r(seed) +u_int *seed; +{ + *seed = *seed * 1103515245 + 12345; + return (u_int)(*seed / 65536) % ((u_int)RAND_MAX + 1); +} + int rand() { - return ((next = next * 1103515245 + 12345) % ((u_int)RAND_MAX + 1)); + return rand_r(&next); } void -- cgit v1.2.3-55-g6feb