hurd: Replace threadvars with TLS
[glibc.git] / sysdeps / mach / hurd / libc-lock.h
blob2dfade93eb1d5f2c5e844bf4ad93bc9b968818e7
1 /* libc-internal interface for mutex locks. Hurd version using Mach cthreads.
2 Copyright (C) 1996-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #ifndef _LIBC_LOCK_H
20 #define _LIBC_LOCK_H 1
22 #if (_LIBC - 0) || (_CTHREADS_ - 0)
23 #include <cthreads.h>
25 /* The locking here is very inexpensive, even for inlining. */
26 #define _IO_lock_inexpensive 1
28 typedef struct mutex __libc_lock_t;
29 typedef struct
31 struct mutex mutex;
32 void *owner;
33 int count;
34 } __libc_lock_recursive_t;
35 typedef __libc_lock_recursive_t __rtld_lock_recursive_t;
37 extern char __libc_lock_self0[0];
38 #define __libc_lock_owner_self() (__LIBC_NO_TLS() ? &__libc_lock_self0 : THREAD_SELF)
40 #else
41 typedef struct __libc_lock_opaque__ __libc_lock_t;
42 typedef struct __libc_lock_recursive_opaque__ __libc_lock_recursive_t;
43 #endif
45 /* Define a lock variable NAME with storage class CLASS. The lock must be
46 initialized with __libc_lock_init before it can be used (or define it
47 with __libc_lock_define_initialized, below). Use `extern' for CLASS to
48 declare a lock defined in another module. In public structure
49 definitions you must use a pointer to the lock structure (i.e., NAME
50 begins with a `*'), because its storage size will not be known outside
51 of libc. */
52 #define __libc_lock_define(CLASS,NAME) \
53 CLASS __libc_lock_t NAME;
55 /* Define an initialized lock variable NAME with storage class CLASS. */
56 #define _LIBC_LOCK_INITIALIZER MUTEX_INITIALIZER
57 #define __libc_lock_define_initialized(CLASS,NAME) \
58 CLASS __libc_lock_t NAME = _LIBC_LOCK_INITIALIZER;
60 /* Initialize the named lock variable, leaving it in a consistent, unlocked
61 state. */
62 #define __libc_lock_init(NAME) __mutex_init (&(NAME))
64 /* Finalize the named lock variable, which must be locked. It cannot be
65 used again until __libc_lock_init is called again on it. This must be
66 called on a lock variable before the containing storage is reused. */
67 #define __libc_lock_fini(NAME) __mutex_unlock (&(NAME))
68 #define __libc_lock_fini_recursive(NAME) __mutex_unlock (&(NAME).mutex)
69 #define __rtld_lock_fini_recursive(NAME) __mutex_unlock (&(NAME).mutex)
72 /* Lock the named lock variable. */
73 #define __libc_lock_lock(NAME) __mutex_lock (&(NAME))
75 /* Lock the named lock variable. */
76 #define __libc_lock_trylock(NAME) (!__mutex_trylock (&(NAME)))
78 /* Unlock the named lock variable. */
79 #define __libc_lock_unlock(NAME) __mutex_unlock (&(NAME))
82 #define __libc_lock_define_recursive(CLASS,NAME) \
83 CLASS __libc_lock_recursive_t NAME;
84 #define _LIBC_LOCK_RECURSIVE_INITIALIZER { MUTEX_INITIALIZER, 0, 0 }
85 #define __libc_lock_define_initialized_recursive(CLASS,NAME) \
86 CLASS __libc_lock_recursive_t NAME = _LIBC_LOCK_RECURSIVE_INITIALIZER;
88 #define __rtld_lock_define_recursive(CLASS,NAME) \
89 __libc_lock_define_recursive (CLASS, NAME)
90 #define _RTLD_LOCK_RECURSIVE_INITIALIZER \
91 _LIBC_LOCK_RECURSIVE_INITIALIZER
92 #define __rtld_lock_define_initialized_recursive(CLASS,NAME) \
93 __libc_lock_define_initialized_recursive (CLASS, NAME)
95 #define __libc_lock_init_recursive(NAME) \
96 ({ __libc_lock_recursive_t *const __lock = &(NAME); \
97 __lock->owner = 0; mutex_init (&__lock->mutex); })
99 #define __libc_lock_trylock_recursive(NAME) \
100 ({ __libc_lock_recursive_t *const __lock = &(NAME); \
101 void *__self = __libc_lock_owner_self (); \
102 __mutex_trylock (&__lock->mutex) \
103 ? (__lock->owner = __self, __lock->count = 1, 0) \
104 : __lock->owner == __self ? (++__lock->count, 0) : 1; })
106 #define __libc_lock_lock_recursive(NAME) \
107 ({ __libc_lock_recursive_t *const __lock = &(NAME); \
108 void *__self = __libc_lock_owner_self (); \
109 if (__mutex_trylock (&__lock->mutex) \
110 || (__lock->owner != __self \
111 && (__mutex_lock (&__lock->mutex), 1))) \
112 __lock->owner = __self, __lock->count = 1; \
113 else \
114 ++__lock->count; \
116 #define __libc_lock_unlock_recursive(NAME) \
117 ({ __libc_lock_recursive_t *const __lock = &(NAME); \
118 if (--__lock->count == 0) \
120 __lock->owner = 0; \
121 __mutex_unlock (&__lock->mutex); \
126 #define __rtld_lock_initialize(NAME) \
127 (void) ((NAME) = (__rtld_lock_recursive_t) _RTLD_LOCK_RECURSIVE_INITIALIZER)
128 #define __rtld_lock_trylock_recursive(NAME) \
129 __libc_lock_trylock_recursive (NAME)
130 #define __rtld_lock_lock_recursive(NAME) \
131 __libc_lock_lock_recursive(NAME)
132 #define __rtld_lock_unlock_recursive(NAME) \
133 __libc_lock_unlock_recursive (NAME)
136 /* XXX for now */
137 #define __libc_rwlock_define __libc_lock_define
138 #define __libc_rwlock_define_initialized __libc_lock_define_initialized
139 #define __libc_rwlock_init __libc_lock_init
140 #define __libc_rwlock_fini __libc_lock_fini
141 #define __libc_rwlock_rdlock __libc_lock_lock
142 #define __libc_rwlock_wrlock __libc_lock_lock
143 #define __libc_rwlock_tryrdlock __libc_lock_trylock
144 #define __libc_rwlock_trywrlock __libc_lock_trylock
145 #define __libc_rwlock_unlock __libc_lock_unlock
148 /* Start a critical region with a cleanup function */
149 #define __libc_cleanup_region_start(DOIT, FCT, ARG) \
151 typeof (***(FCT)) *__save_FCT = (DOIT) ? (FCT) : 0; \
152 typeof (ARG) __save_ARG = ARG; \
153 /* close brace is in __libc_cleanup_region_end below. */
155 /* End a critical region started with __libc_cleanup_region_start. */
156 #define __libc_cleanup_region_end(DOIT) \
157 if ((DOIT) && __save_FCT != 0) \
158 (*__save_FCT)(__save_ARG); \
161 /* Sometimes we have to exit the block in the middle. */
162 #define __libc_cleanup_end(DOIT) \
163 if ((DOIT) && __save_FCT != 0) \
164 (*__save_FCT)(__save_ARG); \
166 #define __libc_cleanup_push(fct, arg) __libc_cleanup_region_start (1, fct, arg)
167 #define __libc_cleanup_pop(execute) __libc_cleanup_region_end (execute)
169 #if (_CTHREADS_ - 0)
171 /* Use mutexes as once control variables. */
173 struct __libc_once
175 __libc_lock_t lock;
176 int done;
179 #define __libc_once_define(CLASS,NAME) \
180 CLASS struct __libc_once NAME = { MUTEX_INITIALIZER, 0 }
182 /* Call handler iff the first call. */
183 #define __libc_once(ONCE_CONTROL, INIT_FUNCTION) \
184 do { \
185 __libc_lock_lock (ONCE_CONTROL.lock); \
186 if (!ONCE_CONTROL.done) \
187 (INIT_FUNCTION) (); \
188 ONCE_CONTROL.done = 1; \
189 __libc_lock_unlock (ONCE_CONTROL.lock); \
190 } while (0)
192 /* Get once control variable. */
193 #define __libc_once_get(ONCE_CONTROL) ((ONCE_CONTROL).done != 0)
195 #ifdef _LIBC
196 /* We need portable names for some functions. E.g., when they are
197 used as argument to __libc_cleanup_region_start. */
198 #define __libc_mutex_unlock __mutex_unlock
199 #endif
201 /* Type for key of thread specific data. */
202 typedef cthread_key_t __libc_key_t;
204 #define __libc_key_create(KEY,DEST) cthread_keycreate (KEY)
205 #define __libc_setspecific(KEY,VAL) cthread_setspecific (KEY, VAL)
206 void *__libc_getspecific (__libc_key_t key);
208 #endif /* _CTHREADS_ */
210 /* Hide the definitions which are only supposed to be used inside libc in
211 a separate file. This file is not present in the installation! */
212 #ifdef _LIBC
213 # include <libc-lockP.h>
214 #endif
216 #endif /* libc-lock.h */