summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormarc <>2002-11-03 23:58:39 +0000
committermarc <>2002-11-03 23:58:39 +0000
commitc8b772af00b03eb1dd9425663fd4b7f60c9693bb (patch)
tree2206a8dcf3012ad649fe0287a808260b5bd519ea /src
parentc64ae263713a52181fce5e72d090dfc2a7b7b008 (diff)
downloadopenbsd-c8b772af00b03eb1dd9425663fd4b7f60c9693bb.tar.gz
openbsd-c8b772af00b03eb1dd9425663fd4b7f60c9693bb.tar.bz2
openbsd-c8b772af00b03eb1dd9425663fd4b7f60c9693bb.zip
back out previous patch.. there are still some vax/m68k issues
Diffstat (limited to 'src')
-rw-r--r--src/lib/libc/include/thread_private.h70
-rw-r--r--src/lib/libc/stdlib/abort.c10
-rw-r--r--src/lib/libc/stdlib/malloc.c57
3 files changed, 93 insertions, 44 deletions
diff --git a/src/lib/libc/include/thread_private.h b/src/lib/libc/include/thread_private.h
index 976e330cac..89faade556 100644
--- a/src/lib/libc/include/thread_private.h
+++ b/src/lib/libc/include/thread_private.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: thread_private.h,v 1.12 2002/11/03 20:36:43 marc Exp $ */ 1/* $OpenBSD: thread_private.h,v 1.13 2002/11/03 23:58:39 marc Exp $ */
2 2
3#ifndef _THREAD_PRIVATE_H_ 3#ifndef _THREAD_PRIVATE_H_
4#define _THREAD_PRIVATE_H_ 4#define _THREAD_PRIVATE_H_
@@ -18,7 +18,19 @@ extern int __isthreaded;
18 * Use WEAK_NAME(n) to get a libc-private name for n (_weak_n), 18 * Use WEAK_NAME(n) to get a libc-private name for n (_weak_n),
19 * WEAK_ALIAS(n) to generate the weak symbol n pointing to _weak_n, 19 * WEAK_ALIAS(n) to generate the weak symbol n pointing to _weak_n,
20 * WEAK_PROTOTYPE(n) to generate a prototype for _weak_n (based on n). 20 * WEAK_PROTOTYPE(n) to generate a prototype for _weak_n (based on n).
21 *
22 * If the symbol _NO_WEAK_ALIASES is defined, then symbols will be
21 */ 23 */
24
25#ifdef _NO_WEAK_ALIASES
26#ifdef _THREAD_SAFE
27#define WEAK_NAME(name) __CONCAT(_weak,name)
28#else
29#define WEAK_NAME(name) name
30#endif
31#define WEAK_ALIAS(name) /* unavailable */
32#define WEAK_PROTOTYPE(name) /* unnecessary */
33#else /* !_NO_WEAK_ALIASES */
22#define WEAK_NAME(name) __CONCAT(_weak_,name) 34#define WEAK_NAME(name) __CONCAT(_weak_,name)
23#define WEAK_ALIAS(name) __weak_alias(name, WEAK_NAME(name)) 35#define WEAK_ALIAS(name) __weak_alias(name, WEAK_NAME(name))
24#ifdef __GNUC__ 36#ifdef __GNUC__
@@ -26,6 +38,7 @@ extern int __isthreaded;
26#else 38#else
27#define WEAK_PROTOTYPE(name) /* typeof() only in gcc */ 39#define WEAK_PROTOTYPE(name) /* typeof() only in gcc */
28#endif 40#endif
41#endif /* !_NO_WEAK_ALIASES */
29 42
30/* 43/*
31 * These macros help in making persistent storage thread-specific. 44 * These macros help in making persistent storage thread-specific.
@@ -49,7 +62,7 @@ struct _thread_private_key_struct {
49void _libc_private_storage_lock(pthread_mutex_t *); 62void _libc_private_storage_lock(pthread_mutex_t *);
50void _libc_private_storage_unlock(pthread_mutex_t *); 63void _libc_private_storage_unlock(pthread_mutex_t *);
51void * _libc_private_storage(volatile struct _thread_private_key_struct *, 64void * _libc_private_storage(volatile struct _thread_private_key_struct *,
52 void *, size_t, void *); 65 void *, size_t, void *);
53 66
54/* Declare a module mutex. */ 67/* Declare a module mutex. */
55#define _THREAD_PRIVATE_MUTEX(name) \ 68#define _THREAD_PRIVATE_MUTEX(name) \
@@ -84,7 +97,18 @@ void * _libc_private_storage(volatile struct _thread_private_key_struct *,
84 */ 97 */
85#define _THREAD_PRIVATE(keyname, storage, error) \ 98#define _THREAD_PRIVATE(keyname, storage, error) \
86 _libc_private_storage(&__THREAD_KEY_NAME(keyname), \ 99 _libc_private_storage(&__THREAD_KEY_NAME(keyname), \
87 &(storage), sizeof (storage), error) 100 &(storage), sizeof (storage), error)
101
102/*
103 * Macros for locking and unlocking FILEs. These test if the
104 * process is threaded to avoid locking when not required.
105 */
106#ifdef _FLOCK_DEBUG
107#define FLOCKFILE(fp) _flockfile_debug(fp, __FILE__, __LINE__)
108#else
109#define FLOCKFILE(fp) flockfile(fp)
110#endif
111#define FUNLOCKFILE(fp) funlockfile(fp)
88 112
89/* 113/*
90 * File descriptor locking definitions. 114 * File descriptor locking definitions.
@@ -93,33 +117,19 @@ void * _libc_private_storage(volatile struct _thread_private_key_struct *,
93#define FD_WRITE 0x2 117#define FD_WRITE 0x2
94#define FD_RDWR (FD_READ | FD_WRITE) 118#define FD_RDWR (FD_READ | FD_WRITE)
95 119
96#define _FD_LOCK(_fd,_type,_ts) \ 120#ifdef _LOCK_DEBUG
97 _thread_fd_lock(_fd, _type, _ts, __FILE__, __LINE__) 121#define _FD_LOCK(_fd,_type,_ts) _thread_fd_lock_debug(_fd, _type, \
98#define _FD_UNLOCK(_fd,_type) \ 122 _ts, __FILE__, __LINE__)
99 _thread_fd_unlock(_fd, _type, __FILE__, __LINE__) 123#define _FD_UNLOCK(_fd,_type) _thread_fd_unlock_debug(_fd, _type, \
100 124 __FILE__, __LINE__)
101int _thread_fd_lock(int, int, struct timespec *, const char *, int); 125#else
102void _thread_fd_unlock(int, int, const char *, int); 126#define _FD_LOCK(_fd,_type,_ts) _thread_fd_lock(_fd, _type, _ts)
127#define _FD_UNLOCK(_fd,_type) _thread_fd_unlock(_fd, _type)
128#endif
103 129
104/* 130int _thread_fd_lock(int, int, struct timespec *);
105 * malloc lock/unlock definitions 131int _thread_fd_lock_debug(int, int, struct timespec *, char *, int);
106 */ 132void _thread_fd_unlock(int, int);
107# define _MALLOC_LOCK() do { \ 133void _thread_fd_unlock_debug(int, int, char *, int);
108 if (__isthreaded) \
109 _thread_malloc_lock(); \
110 } while (0)
111# define _MALLOC_UNLOCK() do { \
112 if (__isthreaded) \
113 _thread_malloc_unlock();\
114 } while (0)
115# define _MALLOC_LOCK_INIT()do { \
116 if (__isthreaded) \
117 _thread_malloc_init();\
118 } while (0)
119
120
121void _thread_malloc_init(void);
122void _thread_malloc_lock(void);
123void _thread_malloc_unlock(void);
124 134
125#endif /* _THREAD_PRIVATE_H_ */ 135#endif /* _THREAD_PRIVATE_H_ */
diff --git a/src/lib/libc/stdlib/abort.c b/src/lib/libc/stdlib/abort.c
index 6fffe3df7c..7252cd8a8b 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.9 2002/11/03 20:36:43 marc Exp $"; 35static char *rcsid = "$OpenBSD: abort.c,v 1.10 2002/11/03 23:58:39 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,7 +55,11 @@ 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
58 (void)_thread_sys_sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL); 59 (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 */
59 63
60 /* 64 /*
61 * POSIX requires we flush stdio buffers on abort 65 * POSIX requires we flush stdio buffers on abort
@@ -76,7 +80,11 @@ abort()
76 * it again, only harder. 80 * it again, only harder.
77 */ 81 */
78 (void)signal(SIGABRT, SIG_DFL); 82 (void)signal(SIGABRT, SIG_DFL);
83#ifdef _THREAD_SAFE
79 (void)_thread_sys_sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL); 84 (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 */
80 (void)kill(getpid(), SIGABRT); 88 (void)kill(getpid(), SIGABRT);
81 exit(1); 89 exit(1);
82} 90}
diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c
index 3aff1bfb9c..2ebc1eff05 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.49 2002/11/03 20:36:43 marc Exp $"; 11static char rcsid[] = "$OpenBSD: malloc.c,v 1.50 2002/11/03 23:58:39 marc Exp $";
12#endif /* LIBC_SCCS and not lint */ 12#endif /* LIBC_SCCS and not lint */
13 13
14/* 14/*
@@ -48,8 +48,6 @@ static char rcsid[] = "$OpenBSD: malloc.c,v 1.49 2002/11/03 20:36:43 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
53/* 51/*
54 * The basic parameters you can tweak. 52 * The basic parameters you can tweak.
55 * 53 *
@@ -69,6 +67,39 @@ static char rcsid[] = "$OpenBSD: malloc.c,v 1.49 2002/11/03 20:36:43 marc Exp $"
69# define malloc_pageshift 13U 67# define malloc_pageshift 13U
70#endif /* __OpenBSD__ */ 68#endif /* __OpenBSD__ */
71 69
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
72/* 103/*
73 * No user serviceable parts behind this point. 104 * No user serviceable parts behind this point.
74 * 105 *
@@ -463,7 +494,7 @@ malloc_init ()
463 int i, j; 494 int i, j;
464 int save_errno = errno; 495 int save_errno = errno;
465 496
466 _MALLOC_LOCK_INIT(); 497 THREAD_LOCK_INIT();
467 498
468 INIT_MMAP(); 499 INIT_MMAP();
469 500
@@ -1213,17 +1244,17 @@ malloc(size_t size)
1213 register void *r; 1244 register void *r;
1214 1245
1215 malloc_func = " in malloc():"; 1246 malloc_func = " in malloc():";
1216 _MALLOC_LOCK(); 1247 THREAD_LOCK();
1217 if (malloc_active++) { 1248 if (malloc_active++) {
1218 wrtwarning("recursive call.\n"); 1249 wrtwarning("recursive call.\n");
1219 malloc_active--; 1250 malloc_active--;
1220 _MALLOC_UNLOCK(); 1251 THREAD_UNLOCK();
1221 return (0); 1252 return (0);
1222 } 1253 }
1223 r = imalloc(size); 1254 r = imalloc(size);
1224 UTRACE(0, size, r); 1255 UTRACE(0, size, r);
1225 malloc_active--; 1256 malloc_active--;
1226 _MALLOC_UNLOCK(); 1257 THREAD_UNLOCK();
1227 if (malloc_xmalloc && !r) 1258 if (malloc_xmalloc && !r)
1228 wrterror("out of memory.\n"); 1259 wrterror("out of memory.\n");
1229 return (r); 1260 return (r);
@@ -1233,17 +1264,17 @@ void
1233free(void *ptr) 1264free(void *ptr)
1234{ 1265{
1235 malloc_func = " in free():"; 1266 malloc_func = " in free():";
1236 _MALLOC_LOCK(); 1267 THREAD_LOCK();
1237 if (malloc_active++) { 1268 if (malloc_active++) {
1238 wrtwarning("recursive call.\n"); 1269 wrtwarning("recursive call.\n");
1239 malloc_active--; 1270 malloc_active--;
1240 _MALLOC_UNLOCK(); 1271 THREAD_UNLOCK();
1241 return; 1272 return;
1242 } 1273 }
1243 ifree(ptr); 1274 ifree(ptr);
1244 UTRACE(ptr, 0, 0); 1275 UTRACE(ptr, 0, 0);
1245 malloc_active--; 1276 malloc_active--;
1246 _MALLOC_UNLOCK(); 1277 THREAD_UNLOCK();
1247 return; 1278 return;
1248} 1279}
1249 1280
@@ -1253,11 +1284,11 @@ realloc(void *ptr, size_t size)
1253 register void *r; 1284 register void *r;
1254 1285
1255 malloc_func = " in realloc():"; 1286 malloc_func = " in realloc():";
1256 _MALLOC_LOCK(); 1287 THREAD_LOCK();
1257 if (malloc_active++) { 1288 if (malloc_active++) {
1258 wrtwarning("recursive call.\n"); 1289 wrtwarning("recursive call.\n");
1259 malloc_active--; 1290 malloc_active--;
1260 _MALLOC_UNLOCK(); 1291 THREAD_UNLOCK();
1261 return (0); 1292 return (0);
1262 } 1293 }
1263 if (!ptr) { 1294 if (!ptr) {
@@ -1267,7 +1298,7 @@ realloc(void *ptr, size_t size)
1267 } 1298 }
1268 UTRACE(ptr, size, r); 1299 UTRACE(ptr, size, r);
1269 malloc_active--; 1300 malloc_active--;
1270 _MALLOC_UNLOCK(); 1301 THREAD_UNLOCK();
1271 if (malloc_xmalloc && !r) 1302 if (malloc_xmalloc && !r)
1272 wrterror("out of memory.\n"); 1303 wrterror("out of memory.\n");
1273 return (r); 1304 return (r);