diff options
Diffstat (limited to 'src/lib/libc/include/thread_private.h')
| -rw-r--r-- | src/lib/libc/include/thread_private.h | 160 |
1 files changed, 76 insertions, 84 deletions
diff --git a/src/lib/libc/include/thread_private.h b/src/lib/libc/include/thread_private.h index 4212e7dcb2..edac6b008d 100644 --- a/src/lib/libc/include/thread_private.h +++ b/src/lib/libc/include/thread_private.h | |||
| @@ -1,13 +1,19 @@ | |||
| 1 | /* $OpenBSD: thread_private.h,v 1.15 2003/01/28 04:58:00 marc Exp $ */ | 1 | /* $OpenBSD: thread_private.h,v 1.16 2004/06/07 21:11:23 marc Exp $ */ |
| 2 | |||
| 3 | /* PUBLIC DOMAIN: No Rights Reserved. Marco S Hyman <marc@snafu.org> */ | ||
| 2 | 4 | ||
| 3 | #ifndef _THREAD_PRIVATE_H_ | 5 | #ifndef _THREAD_PRIVATE_H_ |
| 4 | #define _THREAD_PRIVATE_H_ | 6 | #define _THREAD_PRIVATE_H_ |
| 5 | 7 | ||
| 6 | #include <pthread.h> | 8 | /* |
| 9 | * This file defines the thread library interface to libc. Thread | ||
| 10 | * libraries must implement the functions described here for proper | ||
| 11 | * inter-operation with libc. libc contains weak versions of the | ||
| 12 | * described functions for operation in a non-threaded environment. | ||
| 13 | */ | ||
| 7 | 14 | ||
| 8 | /* | 15 | /* |
| 9 | * This variable is initially 0 when there is exactly one thread. | 16 | * This variable is 0 until a second thread is created. |
| 10 | * It should never decrease. | ||
| 11 | */ | 17 | */ |
| 12 | extern int __isthreaded; | 18 | extern int __isthreaded; |
| 13 | 19 | ||
| @@ -19,116 +25,102 @@ extern int __isthreaded; | |||
| 19 | * WEAK_ALIAS(n) to generate the weak symbol n pointing to _weak_n, | 25 | * 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). | 26 | * WEAK_PROTOTYPE(n) to generate a prototype for _weak_n (based on n). |
| 21 | */ | 27 | */ |
| 22 | #define WEAK_NAME(name) __CONCAT(_weak_,name) | 28 | #define WEAK_NAME(name) __CONCAT(_weak_,name) |
| 23 | #define WEAK_ALIAS(name) __weak_alias(name, WEAK_NAME(name)) | 29 | #define WEAK_ALIAS(name) __weak_alias(name, WEAK_NAME(name)) |
| 24 | #ifdef __GNUC__ | 30 | #ifdef __GNUC__ |
| 25 | #define WEAK_PROTOTYPE(name) __typeof__(name) WEAK_NAME(name) | 31 | #define WEAK_PROTOTYPE(name) __typeof__(name) WEAK_NAME(name) |
| 26 | #else | 32 | #else |
| 27 | #define WEAK_PROTOTYPE(name) /* typeof() only in gcc */ | 33 | #define WEAK_PROTOTYPE(name) /* typeof() only in gcc */ |
| 28 | #endif | 34 | #endif |
| 29 | 35 | ||
| 30 | /* | 36 | /* |
| 31 | * These macros help in making persistent storage thread-specific. | 37 | * helper macro to make unique names in the thread namespace |
| 32 | * Libc makes extensive use of private static data structures | ||
| 33 | * that hold state across function invocation, and these macros | ||
| 34 | * are no-ops when running single-threaded. | ||
| 35 | * | ||
| 36 | * Linking against the user-thread library causes these macros to | ||
| 37 | * allocate storage on a per-thread basis. | ||
| 38 | */ | 38 | */ |
| 39 | 39 | #define __THREAD_NAME(name) __CONCAT(_thread_tagname_,name) | |
| 40 | #define __THREAD_MUTEX_NAME(name) __CONCAT(_libc_storage_mutex_,name) | ||
| 41 | #define __THREAD_KEY_NAME(name) __CONCAT(_libc_storage_key_,name) | ||
| 42 | |||
| 43 | struct _thread_private_key_struct { | ||
| 44 | pthread_once_t once; | ||
| 45 | void (*cleanfn)(void *); | ||
| 46 | pthread_key_t key; | ||
| 47 | }; | ||
| 48 | |||
| 49 | void _libc_private_storage_lock(pthread_mutex_t *); | ||
| 50 | void _libc_private_storage_unlock(pthread_mutex_t *); | ||
| 51 | void * _libc_private_storage(volatile struct _thread_private_key_struct *, | ||
| 52 | void *, size_t, void *); | ||
| 53 | |||
| 54 | /* Declare a module mutex. */ | ||
| 55 | #define _THREAD_PRIVATE_MUTEX(name) \ | ||
| 56 | static pthread_mutex_t __THREAD_MUTEX_NAME(name) = \ | ||
| 57 | PTHREAD_MUTEX_INITIALIZER | ||
| 58 | |||
| 59 | /* Lock a module mutex against use by any other threads. */ | ||
| 60 | #define _THREAD_PRIVATE_MUTEX_LOCK(name) \ | ||
| 61 | _libc_private_storage_lock(&__THREAD_MUTEX_NAME(name)) | ||
| 62 | |||
| 63 | /* Unlock a module mutex. */ | ||
| 64 | #define _THREAD_PRIVATE_MUTEX_UNLOCK(name) \ | ||
| 65 | _libc_private_storage_unlock(&__THREAD_MUTEX_NAME(name)) | ||
| 66 | |||
| 67 | /* Declare a thread-private storage key. */ | ||
| 68 | #define _THREAD_PRIVATE_KEY(name) \ | ||
| 69 | static volatile struct _thread_private_key_struct \ | ||
| 70 | __THREAD_KEY_NAME(name) = { \ | ||
| 71 | PTHREAD_ONCE_INIT, \ | ||
| 72 | 0 \ | ||
| 73 | } | ||
| 74 | 40 | ||
| 75 | /* | 41 | /* |
| 76 | * In threaded mode, return a pointer to thread-private memory of | 42 | * helper functions that exist as (weak) null functions in libc and |
| 77 | * the same size as, and (initially) with the same contents as 'storage'. If | 43 | * (strong) functions in the thread library. These functions: |
| 78 | * an error occurs, the 'error' parameter is returned. | 44 | * |
| 79 | * In single-threaded mode, no storage is allocated. Instead, a pointer | 45 | * _thread_tag_lock: |
| 80 | * to storage is always returned. | 46 | * lock the mutex associated with the given tag. If the given |
| 81 | * The 'cleanfn' function of the key structure is called to free the storage. | 47 | * tag is NULL a tag is first allocated. |
| 82 | * If 'cleanfn' is NULL, then free() is used. This hook can be useful for | 48 | * |
| 83 | * getting rid of memory leaks. | 49 | * _thread_tag_unlock: |
| 50 | * unlock the mutex associated with the given tag. If the given | ||
| 51 | * tag is NULL a tag is first allocated. | ||
| 52 | * | ||
| 53 | * _thread_tag_storage: | ||
| 54 | * return a pointer to per thread instance of data associated | ||
| 55 | * with the given tag. If the given tag is NULL a tag is first | ||
| 56 | * allocated. | ||
| 84 | */ | 57 | */ |
| 85 | #define _THREAD_PRIVATE(keyname, storage, error) \ | 58 | void _thread_tag_lock(void **); |
| 86 | _libc_private_storage(&__THREAD_KEY_NAME(keyname), \ | 59 | void _thread_tag_unlock(void **); |
| 87 | &(storage), sizeof (storage), error) | 60 | void *_thread_tag_storage(void **, void *, size_t, void *); |
| 88 | 61 | ||
| 89 | /* | 62 | /* |
| 90 | * Keys used to access the per thread instances of resolver global data. | 63 | * Macros used in libc to access thread mutex, keys, and per thread storage. |
| 91 | * These are not static as they are referenced in several places. | 64 | * _THREAD_PRIVATE_KEY and _THREAD_PRIVATE_MUTEX are different macros for |
| 65 | * historical reasons. They do the same thing, define a static variable | ||
| 66 | * keyed by 'name' that identifies a mutex and a key to identify per thread | ||
| 67 | * data. | ||
| 92 | */ | 68 | */ |
| 93 | extern volatile struct _thread_private_key_struct __THREAD_KEY_NAME(_res); | 69 | #define _THREAD_PRIVATE_KEY(name) \ |
| 94 | #ifdef INET6 | 70 | static void *__THREAD_NAME(name) |
| 95 | extern volatile struct _thread_private_key_struct __THREAD_KEY_NAME(_res_ext); | 71 | #define _THREAD_PRIVATE_MUTEX(name) \ |
| 96 | #endif | 72 | static void *__THREAD_NAME(name) |
| 73 | #define _THREAD_PRIVATE_MUTEX_LOCK(name) \ | ||
| 74 | _thread_tag_lock(&(__THREAD_NAME(name))) | ||
| 75 | #define _THREAD_PRIVATE_MUTEX_UNLOCK(name) \ | ||
| 76 | _thread_tag_unlock(&(__THREAD_NAME(name))) | ||
| 77 | #define _THREAD_PRIVATE(keyname, storage, error) \ | ||
| 78 | _thread_tag_storage(&(__THREAD_NAME(keyname)), &(storage), \ | ||
| 79 | sizeof (storage), error) | ||
| 80 | /* | ||
| 81 | * Resolver code is special cased in that it uses global keys. | ||
| 82 | */ | ||
| 83 | extern void *__THREAD_NAME(_res); | ||
| 84 | extern void *__THREAD_NAME(_res_ext); | ||
| 85 | extern void *__THREAD_NAME(serv_mutex); | ||
| 97 | 86 | ||
| 98 | /* | 87 | /* |
| 99 | * File descriptor locking definitions. | 88 | * File descriptor locking definitions. |
| 100 | */ | 89 | */ |
| 101 | #define FD_READ 0x1 | 90 | #define FD_READ 0x1 |
| 102 | #define FD_WRITE 0x2 | 91 | #define FD_WRITE 0x2 |
| 103 | #define FD_RDWR (FD_READ | FD_WRITE) | 92 | #define FD_RDWR (FD_READ | FD_WRITE) |
| 93 | |||
| 94 | int _thread_fd_lock(int, int, struct timespec *); | ||
| 95 | void _thread_fd_unlock(int, int); | ||
| 104 | 96 | ||
| 105 | #define _FD_LOCK(_fd,_type,_ts) \ | 97 | /* |
| 106 | _thread_fd_lock(_fd, _type, _ts, __FILE__, __LINE__) | 98 | * Macros are used in libc code for historical (debug) reasons. |
| 107 | #define _FD_UNLOCK(_fd,_type) \ | 99 | * Define them here. |
| 108 | _thread_fd_unlock(_fd, _type, __FILE__, __LINE__) | 100 | */ |
| 101 | #define _FD_LOCK(_fd,_type,_ts) _thread_fd_lock(_fd, _type, _ts) | ||
| 102 | #define _FD_UNLOCK(_fd,_type) _thread_fd_unlock(_fd, _type) | ||
| 109 | 103 | ||
| 110 | int _thread_fd_lock(int, int, struct timespec *, const char *, int); | ||
| 111 | void _thread_fd_unlock(int, int, const char *, int); | ||
| 112 | 104 | ||
| 113 | /* | 105 | /* |
| 114 | * malloc lock/unlock definitions | 106 | * malloc lock/unlock prototypes and definitions |
| 115 | */ | 107 | */ |
| 116 | # define _MALLOC_LOCK() do { \ | 108 | void _thread_malloc_init(void); |
| 109 | void _thread_malloc_lock(void); | ||
| 110 | void _thread_malloc_unlock(void); | ||
| 111 | |||
| 112 | #define _MALLOC_LOCK() do { \ | ||
| 117 | if (__isthreaded) \ | 113 | if (__isthreaded) \ |
| 118 | _thread_malloc_lock(); \ | 114 | _thread_malloc_lock(); \ |
| 119 | } while (0) | 115 | } while (0) |
| 120 | # define _MALLOC_UNLOCK() do { \ | 116 | #define _MALLOC_UNLOCK() do { \ |
| 121 | if (__isthreaded) \ | 117 | if (__isthreaded) \ |
| 122 | _thread_malloc_unlock();\ | 118 | _thread_malloc_unlock();\ |
| 123 | } while (0) | 119 | } while (0) |
| 124 | # define _MALLOC_LOCK_INIT()do { \ | 120 | #define _MALLOC_LOCK_INIT() do { \ |
| 125 | if (__isthreaded) \ | 121 | if (__isthreaded) \ |
| 126 | _thread_malloc_init();\ | 122 | _thread_malloc_init();\ |
| 127 | } while (0) | 123 | } while (0) |
| 128 | 124 | ||
| 129 | 125 | ||
| 130 | void _thread_malloc_init(void); | ||
| 131 | void _thread_malloc_lock(void); | ||
| 132 | void _thread_malloc_unlock(void); | ||
| 133 | |||
| 134 | #endif /* _THREAD_PRIVATE_H_ */ | 126 | #endif /* _THREAD_PRIVATE_H_ */ |
