hurd: Avoid PLT ref for __pthread_get_cleanup_stack
[glibc.git] / sysdeps / mach / libc-lock.h
blob2c64019b33074418f9c1260eb90edf696b44c501
1 /* libc-internal interface for mutex locks. Mach cthreads version.
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 #ifdef _LIBC
24 #include <tls.h>
25 #include <lowlevellock.h>
27 /* The locking here is very inexpensive, even for inlining. */
28 #define _IO_lock_inexpensive 1
30 typedef unsigned int __libc_lock_t;
31 typedef struct
33 __libc_lock_t lock;
34 int cnt;
35 void *owner;
36 } __libc_lock_recursive_t;
38 typedef __libc_lock_recursive_t __rtld_lock_recursive_t;
40 extern char __libc_lock_self0[0];
41 #define __libc_lock_owner_self() \
42 (__LIBC_NO_TLS () ? (void *)&__libc_lock_self0 : THREAD_SELF)
44 #else
45 typedef struct __libc_lock_opaque__ __libc_lock_t;
46 typedef struct __libc_lock_recursive_opaque__ __libc_lock_recursive_t;
47 #endif
49 /* Define a lock variable NAME with storage class CLASS. The lock must be
50 initialized with __libc_lock_init before it can be used (or define it
51 with __libc_lock_define_initialized, below). Use `extern' for CLASS to
52 declare a lock defined in another module. In public structure
53 definitions you must use a pointer to the lock structure (i.e., NAME
54 begins with a `*'), because its storage size will not be known outside
55 of libc. */
56 #define __libc_lock_define(CLASS,NAME) \
57 CLASS __libc_lock_t NAME;
59 /* Define an initialized lock variable NAME with storage class CLASS. */
60 #define _LIBC_LOCK_INITIALIZER LLL_INITIALIZER
61 #define __libc_lock_define_initialized(CLASS,NAME) \
62 CLASS __libc_lock_t NAME = LLL_INITIALIZER;
64 /* Initialize the named lock variable, leaving it in a consistent, unlocked
65 state. */
66 #define __libc_lock_init(NAME) (NAME) = LLL_INITIALIZER
68 /* Finalize the named lock variable, which must be locked. It cannot be
69 used again until __libc_lock_init is called again on it. This must be
70 called on a lock variable before the containing storage is reused. */
71 #define __libc_lock_fini __libc_lock_unlock
72 #define __libc_lock_fini_recursive __libc_lock_unlock_recursive
73 #define __rtld_lock_fini_recursive __rtld_lock_unlock_recursive
75 /* Lock the named lock variable. */
76 #define __libc_lock_lock(NAME) \
77 ({ lll_lock (&(NAME), 0); 0; })
79 /* Lock the named lock variable. */
80 #define __libc_lock_trylock(NAME) lll_trylock (&(NAME))
82 /* Unlock the named lock variable. */
83 #define __libc_lock_unlock(NAME) \
84 ({ lll_unlock (&(NAME), 0); 0; })
86 #define __libc_lock_define_recursive(CLASS,NAME) \
87 CLASS __libc_lock_recursive_t NAME;
89 #define _LIBC_LOCK_RECURSIVE_INITIALIZER { LLL_INITIALIZER, 0, 0 }
91 #define __libc_lock_define_initialized_recursive(CLASS,NAME) \
92 CLASS __libc_lock_recursive_t NAME = _LIBC_LOCK_RECURSIVE_INITIALIZER;
94 #define __rtld_lock_define_recursive(CLASS,NAME) \
95 __libc_lock_define_recursive (CLASS, NAME)
96 #define _RTLD_LOCK_RECURSIVE_INITIALIZER \
97 _LIBC_LOCK_RECURSIVE_INITIALIZER
98 #define __rtld_lock_define_initialized_recursive(CLASS,NAME) \
99 __libc_lock_define_initialized_recursive (CLASS, NAME)
101 #define __libc_lock_init_recursive(NAME) \
102 ({ \
103 (NAME) = (__libc_lock_recursive_t)_LIBC_LOCK_RECURSIVE_INITIALIZER; \
104 0; \
107 #define __libc_lock_trylock_recursive(NAME) \
108 ({ \
109 __libc_lock_recursive_t *const __lock = &(NAME); \
110 void *__self = __libc_lock_owner_self (); \
111 int __r = 0; \
112 if (__self == __lock->owner) \
113 ++__lock->cnt; \
114 else if ((__r = lll_trylock (&__lock->lock)) == 0) \
115 __lock->owner = __self, __lock->cnt = 1; \
116 __r; \
119 #define __libc_lock_lock_recursive(NAME) \
120 ({ \
121 __libc_lock_recursive_t *const __lock = &(NAME); \
122 void *__self = __libc_lock_owner_self (); \
123 if (__self != __lock->owner) \
125 lll_lock (&__lock->lock, 0); \
126 __lock->owner = __self; \
128 ++__lock->cnt; \
129 (void)0; \
132 #define __libc_lock_unlock_recursive(NAME) \
133 ({ \
134 __libc_lock_recursive_t *const __lock = &(NAME); \
135 if (--__lock->cnt == 0) \
137 __lock->owner = 0; \
138 lll_unlock (&__lock->lock, 0); \
143 #define __rtld_lock_initialize(NAME) \
144 (void) ((NAME) = (__rtld_lock_recursive_t) _RTLD_LOCK_RECURSIVE_INITIALIZER)
145 #define __rtld_lock_trylock_recursive(NAME) \
146 __libc_lock_trylock_recursive (NAME)
147 #define __rtld_lock_lock_recursive(NAME) \
148 __libc_lock_lock_recursive(NAME)
149 #define __rtld_lock_unlock_recursive(NAME) \
150 __libc_lock_unlock_recursive (NAME)
152 /* XXX for now */
153 #define __libc_rwlock_define __libc_lock_define
154 #define __libc_rwlock_define_initialized __libc_lock_define_initialized
155 #define __libc_rwlock_init __libc_lock_init
156 #define __libc_rwlock_fini __libc_lock_fini
157 #define __libc_rwlock_rdlock __libc_lock_lock
158 #define __libc_rwlock_wrlock __libc_lock_lock
159 #define __libc_rwlock_tryrdlock __libc_lock_trylock
160 #define __libc_rwlock_trywrlock __libc_lock_trylock
161 #define __libc_rwlock_unlock __libc_lock_unlock
163 struct __libc_cleanup_frame
165 void (*__fct) (void *);
166 void *__argp;
167 int __doit;
170 __extern_inline void
171 __libc_cleanup_fct (struct __libc_cleanup_frame *framep)
173 if (framep->__doit)
174 framep->__fct (framep->__argp);
177 /* Start a critical region with a cleanup function */
178 #define __libc_cleanup_region_start(DOIT, FCT, ARG) \
179 do \
181 struct __libc_cleanup_frame __cleanup \
182 __attribute__ ((__cleanup__ (__libc_cleanup_fct))) = \
183 { .__fct = (FCT), .__argp = (ARG), .__doit = (DOIT) };
185 /* This one closes the brace above. */
186 #define __libc_cleanup_region_end(DOIT) \
187 __cleanup.__doit = (DOIT); \
189 while (0)
191 #define __libc_cleanup_end(DOIT) __cleanup.__doit = (DOIT);
193 #define __libc_cleanup_push(fct, arg) __libc_cleanup_region_start (1, fct, arg)
194 #define __libc_cleanup_pop(execute) __libc_cleanup_region_end (execute)
196 /* Use mutexes as once control variables. */
198 struct __libc_once
200 __libc_lock_t lock;
201 int done;
204 #define __libc_once_define(CLASS,NAME) \
205 CLASS struct __libc_once NAME = { _LIBC_LOCK_INITIALIZER, 0 }
207 /* Call handler iff the first call. */
208 #define __libc_once(ONCE_CONTROL, INIT_FUNCTION) \
209 do { \
210 __libc_lock_lock (ONCE_CONTROL.lock); \
211 if (!ONCE_CONTROL.done) \
212 (INIT_FUNCTION) (); \
213 ONCE_CONTROL.done = 1; \
214 __libc_lock_unlock (ONCE_CONTROL.lock); \
215 } while (0)
217 /* Get once control variable. */
218 #define __libc_once_get(ONCE_CONTROL) ((ONCE_CONTROL).done != 0)
220 #ifdef _LIBC
221 /* We need portable names for some functions. E.g., when they are
222 used as argument to __libc_cleanup_region_start. */
223 #define __libc_mutex_unlock __libc_lock_unlock
225 /* Hide the definitions which are only supposed to be used inside libc in
226 a separate file. This file is not present in the installation! */
227 # include <libc-lockP.h>
228 #endif
230 #endif /* libc-lock.h */