summaryrefslogtreecommitdiff
path: root/src/lib/libc/include/thread_private.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/include/thread_private.h')
-rw-r--r--src/lib/libc/include/thread_private.h138
1 files changed, 138 insertions, 0 deletions
diff --git a/src/lib/libc/include/thread_private.h b/src/lib/libc/include/thread_private.h
new file mode 100644
index 0000000000..5fbbf592a4
--- /dev/null
+++ b/src/lib/libc/include/thread_private.h
@@ -0,0 +1,138 @@
1/* $OpenBSD: thread_private.h,v 1.18 2006/02/22 07:16:31 otto Exp $ */
2
3/* PUBLIC DOMAIN: No Rights Reserved. Marco S Hyman <marc@snafu.org> */
4
5#ifndef _THREAD_PRIVATE_H_
6#define _THREAD_PRIVATE_H_
7
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 */
14
15/*
16 * This variable is 0 until a second thread is created.
17 */
18extern int __isthreaded;
19
20/*
21 * Weak symbols are used in libc so that the thread library can
22 * efficiently wrap libc functions.
23 *
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).
27 */
28#define WEAK_NAME(name) __CONCAT(_weak_,name)
29#define WEAK_ALIAS(name) __weak_alias(name, WEAK_NAME(name))
30#ifdef __GNUC__
31#define WEAK_PROTOTYPE(name) __typeof__(name) WEAK_NAME(name)
32#else
33#define WEAK_PROTOTYPE(name) /* typeof() only in gcc */
34#endif
35
36/*
37 * helper macro to make unique names in the thread namespace
38 */
39#define __THREAD_NAME(name) __CONCAT(_thread_tagname_,name)
40
41/*
42 * helper functions that exist as (weak) null functions in libc and
43 * (strong) functions in the thread library. These functions:
44 *
45 * _thread_tag_lock:
46 * lock the mutex associated with the given tag. If the given
47 * tag is NULL a tag is first allocated.
48 *
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.
57 */
58void _thread_tag_lock(void **);
59void _thread_tag_unlock(void **);
60void *_thread_tag_storage(void **, void *, size_t, void *);
61
62/*
63 * Macros used in libc to access thread mutex, keys, and per thread storage.
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.
68 */
69#define _THREAD_PRIVATE_KEY(name) \
70 static void *__THREAD_NAME(name)
71#define _THREAD_PRIVATE_MUTEX(name) \
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 */
83extern void *__THREAD_NAME(_res);
84extern void *__THREAD_NAME(_res_ext);
85extern void *__THREAD_NAME(serv_mutex);
86
87/*
88 * File descriptor locking definitions.
89 */
90#define FD_READ 0x1
91#define FD_WRITE 0x2
92#define FD_RDWR (FD_READ | FD_WRITE)
93
94struct timespec;
95int _thread_fd_lock(int, int, struct timespec *);
96void _thread_fd_unlock(int, int);
97
98/*
99 * Macros are used in libc code for historical (debug) reasons.
100 * Define them here.
101 */
102#define _FD_LOCK(_fd,_type,_ts) _thread_fd_lock(_fd, _type, _ts)
103#define _FD_UNLOCK(_fd,_type) _thread_fd_unlock(_fd, _type)
104
105
106/*
107 * malloc lock/unlock prototypes and definitions
108 */
109void _thread_malloc_init(void);
110void _thread_malloc_lock(void);
111void _thread_malloc_unlock(void);
112
113#define _MALLOC_LOCK() do { \
114 if (__isthreaded) \
115 _thread_malloc_lock(); \
116 } while (0)
117#define _MALLOC_UNLOCK() do { \
118 if (__isthreaded) \
119 _thread_malloc_unlock();\
120 } while (0)
121#define _MALLOC_LOCK_INIT() do { \
122 if (__isthreaded) \
123 _thread_malloc_init();\
124 } while (0)
125
126void _thread_atexit_lock(void);
127void _thread_atexit_unlock(void);
128
129#define _ATEXIT_LOCK() do { \
130 if (__isthreaded) \
131 _thread_atexit_lock(); \
132 } while (0)
133#define _ATEXIT_UNLOCK() do { \
134 if (__isthreaded) \
135 _thread_atexit_unlock();\
136 } while (0)
137
138#endif /* _THREAD_PRIVATE_H_ */