summaryrefslogtreecommitdiff
path: root/src/lib/libc/stdlib
diff options
context:
space:
mode:
authormarc <>2002-11-05 22:19:55 +0000
committermarc <>2002-11-05 22:19:55 +0000
commit46d7850737001c91bbb8e55a40aa9db06e2285ff (patch)
treef8fddac020c667341da9d9d290f231141b047d5d /src/lib/libc/stdlib
parentc8b772af00b03eb1dd9425663fd4b7f60c9693bb (diff)
downloadopenbsd-46d7850737001c91bbb8e55a40aa9db06e2285ff.tar.gz
openbsd-46d7850737001c91bbb8e55a40aa9db06e2285ff.tar.bz2
openbsd-46d7850737001c91bbb8e55a40aa9db06e2285ff.zip
thread safe libc -- 2nd try. OK miod@, millert@
Thanks to miod@ for m68k and vax fixes
Diffstat (limited to 'src/lib/libc/stdlib')
-rw-r--r--src/lib/libc/stdlib/abort.c10
-rw-r--r--src/lib/libc/stdlib/malloc.c57
2 files changed, 14 insertions, 53 deletions
diff --git a/src/lib/libc/stdlib/abort.c b/src/lib/libc/stdlib/abort.c
index 7252cd8a8b..a833a1a8b7 100644
--- a/src/lib/libc/stdlib/abort.c
+++ b/src/lib/libc/stdlib/abort.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: abort.c,v 1.10 2002/11/03 23:58:39 marc Exp $"; 35static char *rcsid = "$OpenBSD: abort.c,v 1.11 2002/11/05 22:19:55 marc 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>
@@ -55,11 +55,7 @@ abort()
55 * any errors -- X311J doesn't allow abort to return anyway. 55 * any errors -- X311J doesn't allow abort to return anyway.
56 */ 56 */
57 sigdelset(&mask, SIGABRT); 57 sigdelset(&mask, SIGABRT);
58#ifdef _THREAD_SAFE
59 (void)_thread_sys_sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL); 58 (void)_thread_sys_sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL);
60#else /* _THREAD_SAFE */
61 (void)sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL);
62#endif /* _THREAD_SAFE */
63 59
64 /* 60 /*
65 * POSIX requires we flush stdio buffers on abort 61 * POSIX requires we flush stdio buffers on abort
@@ -80,11 +76,7 @@ abort()
80 * it again, only harder. 76 * it again, only harder.
81 */ 77 */
82 (void)signal(SIGABRT, SIG_DFL); 78 (void)signal(SIGABRT, SIG_DFL);
83#ifdef _THREAD_SAFE
84 (void)_thread_sys_sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL); 79 (void)_thread_sys_sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL);
85#else /* _THREAD_SAFE */
86 (void)sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL);
87#endif /* _THREAD_SAFE */
88 (void)kill(getpid(), SIGABRT); 80 (void)kill(getpid(), SIGABRT);
89 exit(1); 81 exit(1);
90} 82}
diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c
index 2ebc1eff05..3cdacc59b9 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.50 2002/11/03 23:58:39 marc Exp $"; 11static char rcsid[] = "$OpenBSD: malloc.c,v 1.51 2002/11/05 22:19:55 marc Exp $";
12#endif /* LIBC_SCCS and not lint */ 12#endif /* LIBC_SCCS and not lint */
13 13
14/* 14/*
@@ -48,6 +48,8 @@ static char rcsid[] = "$OpenBSD: malloc.c,v 1.50 2002/11/03 23:58:39 marc Exp $"
48#include <fcntl.h> 48#include <fcntl.h>
49#include <errno.h> 49#include <errno.h>
50 50
51#include "thread_private.h"
52
51/* 53/*
52 * The basic parameters you can tweak. 54 * The basic parameters you can tweak.
53 * 55 *
@@ -67,39 +69,6 @@ static char rcsid[] = "$OpenBSD: malloc.c,v 1.50 2002/11/03 23:58:39 marc Exp $"
67# define malloc_pageshift 13U 69# define malloc_pageshift 13U
68#endif /* __OpenBSD__ */ 70#endif /* __OpenBSD__ */
69 71
70#ifdef _THREAD_SAFE
71# include "thread_private.h"
72# if 0
73 /* kernel threads */
74# include <pthread.h>
75 static pthread_mutex_t malloc_lock;
76# define THREAD_LOCK() pthread_mutex_lock(&malloc_lock)
77# define THREAD_UNLOCK() pthread_mutex_unlock(&malloc_lock)
78# define THREAD_LOCK_INIT() pthread_mutex_init(&malloc_lock, 0);
79# else
80 /* user threads */
81# include "spinlock.h"
82 static spinlock_t malloc_lock = _SPINLOCK_INITIALIZER;
83# define THREAD_LOCK() if (__isthreaded) _SPINLOCK(&malloc_lock)
84# define THREAD_UNLOCK() if (__isthreaded) _SPINUNLOCK(&malloc_lock)
85# define THREAD_LOCK_INIT()
86 /*
87 * Malloc can't use the wrapped write() if it fails very early, so
88 * we use the unwrapped syscall _thread_sys_write()
89 */
90# define write _thread_sys_write
91 ssize_t write(int, const void *, size_t);
92# undef malloc
93# undef realloc
94# undef free
95# endif
96#else
97 /* no threads */
98# define THREAD_LOCK()
99# define THREAD_UNLOCK()
100# define THREAD_LOCK_INIT()
101#endif
102
103/* 72/*
104 * No user serviceable parts behind this point. 73 * No user serviceable parts behind this point.
105 * 74 *
@@ -494,7 +463,7 @@ malloc_init ()
494 int i, j; 463 int i, j;
495 int save_errno = errno; 464 int save_errno = errno;
496 465
497 THREAD_LOCK_INIT(); 466 _MALLOC_LOCK_INIT();
498 467
499 INIT_MMAP(); 468 INIT_MMAP();
500 469
@@ -1244,17 +1213,17 @@ malloc(size_t size)
1244 register void *r; 1213 register void *r;
1245 1214
1246 malloc_func = " in malloc():"; 1215 malloc_func = " in malloc():";
1247 THREAD_LOCK(); 1216 _MALLOC_LOCK();
1248 if (malloc_active++) { 1217 if (malloc_active++) {
1249 wrtwarning("recursive call.\n"); 1218 wrtwarning("recursive call.\n");
1250 malloc_active--; 1219 malloc_active--;
1251 THREAD_UNLOCK(); 1220 _MALLOC_UNLOCK();
1252 return (0); 1221 return (0);
1253 } 1222 }
1254 r = imalloc(size); 1223 r = imalloc(size);
1255 UTRACE(0, size, r); 1224 UTRACE(0, size, r);
1256 malloc_active--; 1225 malloc_active--;
1257 THREAD_UNLOCK(); 1226 _MALLOC_UNLOCK();
1258 if (malloc_xmalloc && !r) 1227 if (malloc_xmalloc && !r)
1259 wrterror("out of memory.\n"); 1228 wrterror("out of memory.\n");
1260 return (r); 1229 return (r);
@@ -1264,17 +1233,17 @@ void
1264free(void *ptr) 1233free(void *ptr)
1265{ 1234{
1266 malloc_func = " in free():"; 1235 malloc_func = " in free():";
1267 THREAD_LOCK(); 1236 _MALLOC_LOCK();
1268 if (malloc_active++) { 1237 if (malloc_active++) {
1269 wrtwarning("recursive call.\n"); 1238 wrtwarning("recursive call.\n");
1270 malloc_active--; 1239 malloc_active--;
1271 THREAD_UNLOCK(); 1240 _MALLOC_UNLOCK();
1272 return; 1241 return;
1273 } 1242 }
1274 ifree(ptr); 1243 ifree(ptr);
1275 UTRACE(ptr, 0, 0); 1244 UTRACE(ptr, 0, 0);
1276 malloc_active--; 1245 malloc_active--;
1277 THREAD_UNLOCK(); 1246 _MALLOC_UNLOCK();
1278 return; 1247 return;
1279} 1248}
1280 1249
@@ -1284,11 +1253,11 @@ realloc(void *ptr, size_t size)
1284 register void *r; 1253 register void *r;
1285 1254
1286 malloc_func = " in realloc():"; 1255 malloc_func = " in realloc():";
1287 THREAD_LOCK(); 1256 _MALLOC_LOCK();
1288 if (malloc_active++) { 1257 if (malloc_active++) {
1289 wrtwarning("recursive call.\n"); 1258 wrtwarning("recursive call.\n");
1290 malloc_active--; 1259 malloc_active--;
1291 THREAD_UNLOCK(); 1260 _MALLOC_UNLOCK();
1292 return (0); 1261 return (0);
1293 } 1262 }
1294 if (!ptr) { 1263 if (!ptr) {
@@ -1298,7 +1267,7 @@ realloc(void *ptr, size_t size)
1298 } 1267 }
1299 UTRACE(ptr, size, r); 1268 UTRACE(ptr, size, r);
1300 malloc_active--; 1269 malloc_active--;
1301 THREAD_UNLOCK(); 1270 _MALLOC_UNLOCK();
1302 if (malloc_xmalloc && !r) 1271 if (malloc_xmalloc && !r)
1303 wrterror("out of memory.\n"); 1272 wrterror("out of memory.\n");
1304 return (r); 1273 return (r);