1 /* $OpenBSD: thread_private.h,v 1.24 2008/06/14 15:43:43 otto Exp $ */
3 /* PUBLIC DOMAIN: No Rights Reserved. Marco S Hyman <marc@snafu.org> */
5 #ifndef _THREAD_PRIVATE_H_
6 #define _THREAD_PRIVATE_H_
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.
16 * This variable is 0 until a second thread is created.
18 extern int __isthreaded;
21 * Weak symbols are used in libc so that the thread library can
22 * efficiently wrap libc functions.
24 * Use WEAK_NAME(n) to get a libc-private name for n (_weak_n),
25 * WEAK_ALIAS(n) to generate the weak symbol n pointing to _weak_n,
26 * WEAK_PROTOTYPE(n) to generate a prototype for _weak_n (based on n).
28 #define WEAK_NAME(name) __CONCAT(_weak_,name)
29 #define WEAK_ALIAS(name) __weak_alias(name, WEAK_NAME(name))
31 #define WEAK_PROTOTYPE(name) __typeof__(name) WEAK_NAME(name)
33 #define WEAK_PROTOTYPE(name) /* typeof() only in gcc */
37 * helper macro to make unique names in the thread namespace
39 #define __THREAD_NAME(name) __CONCAT(_thread_tagname_,name)
42 * helper functions that exist as (weak) null functions in libc and
43 * (strong) functions in the thread library. These functions:
46 * lock the mutex associated with the given tag. If the given
47 * tag is NULL a tag is first allocated.
50 * unlock the mutex associated with the given tag. If the given
51 * tag is NULL a tag is first allocated.
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
59 * lock the given mutex. If the given mutex is NULL,
60 * rely on rthreads/pthreads implementation to initialize
61 * the mutex before locking.
63 * _thread_mutex_unlock:
64 * unlock the given mutex.
66 * _thread_mutex_destroy:
67 * destroy the given mutex.
69 void _thread_tag_lock(void **);
70 void _thread_tag_unlock(void **);
71 void *_thread_tag_storage(void **, void *, size_t, void *);
72 void _thread_mutex_lock(void **);
73 void _thread_mutex_unlock(void **);
74 void _thread_mutex_destroy(void **);
77 * Macros used in libc to access thread mutex, keys, and per thread storage.
78 * _THREAD_PRIVATE_KEY and _THREAD_PRIVATE_MUTEX are different macros for
79 * historical reasons. They do the same thing, define a static variable
80 * keyed by 'name' that identifies a mutex and a key to identify per thread
83 #define _THREAD_PRIVATE_KEY(name) \
84 static void *__THREAD_NAME(name)
85 #define _THREAD_PRIVATE_MUTEX(name) \
86 static void *__THREAD_NAME(name)
87 #define _THREAD_PRIVATE_MUTEX_LOCK(name) \
88 _thread_tag_lock(&(__THREAD_NAME(name)))
89 #define _THREAD_PRIVATE_MUTEX_UNLOCK(name) \
90 _thread_tag_unlock(&(__THREAD_NAME(name)))
91 #define _THREAD_PRIVATE(keyname, storage, error) \
92 _thread_tag_storage(&(__THREAD_NAME(keyname)), &(storage), \
93 sizeof (storage), error)
96 * Macros used in libc to access mutexes.
98 #define _MUTEX_LOCK(mutex) \
101 _thread_mutex_lock(mutex); \
103 #define _MUTEX_UNLOCK(mutex) \
106 _thread_mutex_unlock(mutex); \
108 #define _MUTEX_DESTROY(mutex) \
111 _thread_mutex_destroy(mutex); \
115 * Resolver code is special cased in that it uses global keys.
117 extern void *__THREAD_NAME(_res);
118 extern void *__THREAD_NAME(_res_ext);
119 extern void *__THREAD_NAME(serv_mutex);
122 * malloc lock/unlock prototypes and definitions
124 void _thread_malloc_lock(void);
125 void _thread_malloc_unlock(void);
127 #define _MALLOC_LOCK() do { \
129 _thread_malloc_lock(); \
131 #define _MALLOC_UNLOCK() do { \
133 _thread_malloc_unlock();\
136 void _thread_atexit_lock(void);
137 void _thread_atexit_unlock(void);
139 #define _ATEXIT_LOCK() do { \
141 _thread_atexit_lock(); \
143 #define _ATEXIT_UNLOCK() do { \
145 _thread_atexit_unlock();\
148 void _thread_arc4_lock(void);
149 void _thread_arc4_unlock(void);
151 #define _ARC4_LOCK() do { \
153 _thread_arc4_lock(); \
155 #define _ARC4_UNLOCK() do { \
157 _thread_arc4_unlock();\
160 #endif /* _THREAD_PRIVATE_H_ */