]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/mach/hurd/libc-lock.h
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / mach / hurd / libc-lock.h
1 /* libc-internal interface for mutex locks. Hurd version using Mach cthreads.
2 Copyright (C) 1996-2016 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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/>. */
18
19 #ifndef _LIBC_LOCK_H
20 #define _LIBC_LOCK_H 1
21
22 #if (_LIBC - 0) || (_CTHREADS_ - 0)
23 #include <cthreads.h>
24 #include <hurd/threadvar.h>
25
26 typedef struct mutex __libc_lock_t;
27 typedef struct
28 {
29 struct mutex mutex;
30 void *owner;
31 int count;
32 } __libc_lock_recursive_t;
33 typedef __libc_lock_recursive_t __rtld_lock_recursive_t;
34
35 #define __libc_lock_owner_self() ((void *) __hurd_threadvar_location (0))
36
37 #else
38 typedef struct __libc_lock_opaque__ __libc_lock_t;
39 typedef struct __libc_lock_recursive_opaque__ __libc_lock_recursive_t;
40 #endif
41
42 /* Define a lock variable NAME with storage class CLASS. The lock must be
43 initialized with __libc_lock_init before it can be used (or define it
44 with __libc_lock_define_initialized, below). Use `extern' for CLASS to
45 declare a lock defined in another module. In public structure
46 definitions you must use a pointer to the lock structure (i.e., NAME
47 begins with a `*'), because its storage size will not be known outside
48 of libc. */
49 #define __libc_lock_define(CLASS,NAME) \
50 CLASS __libc_lock_t NAME;
51
52 /* Define an initialized lock variable NAME with storage class CLASS. */
53 #define _LIBC_LOCK_INITIALIZER MUTEX_INITIALIZER
54 #define __libc_lock_define_initialized(CLASS,NAME) \
55 CLASS __libc_lock_t NAME = _LIBC_LOCK_INITIALIZER;
56
57 /* Initialize the named lock variable, leaving it in a consistent, unlocked
58 state. */
59 #define __libc_lock_init(NAME) __mutex_init (&(NAME))
60
61 /* Finalize the named lock variable, which must be locked. It cannot be
62 used again until __libc_lock_init is called again on it. This must be
63 called on a lock variable before the containing storage is reused. */
64 #define __libc_lock_fini(NAME) __mutex_unlock (&(NAME))
65 #define __libc_lock_fini_recursive(NAME) __mutex_unlock (&(NAME).mutex)
66 #define __rtld_lock_fini_recursive(NAME) __mutex_unlock (&(NAME).mutex)
67
68
69 /* Lock the named lock variable. */
70 #define __libc_lock_lock(NAME) __mutex_lock (&(NAME))
71
72 /* Lock the named lock variable. */
73 #define __libc_lock_trylock(NAME) (!__mutex_trylock (&(NAME)))
74
75 /* Unlock the named lock variable. */
76 #define __libc_lock_unlock(NAME) __mutex_unlock (&(NAME))
77
78
79 #define __libc_lock_define_recursive(CLASS,NAME) \
80 CLASS __libc_lock_recursive_t NAME;
81 #define _LIBC_LOCK_RECURSIVE_INITIALIZER { MUTEX_INITIALIZER, 0, 0 }
82 #define __libc_lock_define_initialized_recursive(CLASS,NAME) \
83 CLASS __libc_lock_recursive_t NAME = _LIBC_LOCK_RECURSIVE_INITIALIZER;
84
85 #define __rtld_lock_define_recursive(CLASS,NAME) \
86 __libc_lock_define_recursive (CLASS, NAME)
87 #define _RTLD_LOCK_RECURSIVE_INITIALIZER \
88 _LIBC_LOCK_RECURSIVE_INITIALIZER
89 #define __rtld_lock_define_initialized_recursive(CLASS,NAME) \
90 __libc_lock_define_initialized_recursive (CLASS, NAME)
91
92 #define __libc_lock_init_recursive(NAME) \
93 ({ __libc_lock_recursive_t *const __lock = &(NAME); \
94 __lock->owner = 0; mutex_init (&__lock->mutex); })
95
96 #define __libc_lock_trylock_recursive(NAME) \
97 ({ __libc_lock_recursive_t *const __lock = &(NAME); \
98 void *__self = __libc_lock_owner_self (); \
99 __mutex_trylock (&__lock->mutex) \
100 ? (__lock->owner = __self, __lock->count = 1, 0) \
101 : __lock->owner == __self ? (++__lock->count, 0) : 1; })
102
103 #define __libc_lock_lock_recursive(NAME) \
104 ({ __libc_lock_recursive_t *const __lock = &(NAME); \
105 void *__self = __libc_lock_owner_self (); \
106 if (__mutex_trylock (&__lock->mutex) \
107 || (__lock->owner != __self \
108 && (__mutex_lock (&__lock->mutex), 1))) \
109 __lock->owner = __self, __lock->count = 1; \
110 else \
111 ++__lock->count; \
112 })
113 #define __libc_lock_unlock_recursive(NAME) \
114 ({ __libc_lock_recursive_t *const __lock = &(NAME); \
115 if (--__lock->count == 0) \
116 { \
117 __lock->owner = 0; \
118 __mutex_unlock (&__lock->mutex); \
119 } \
120 })
121
122
123 #define __rtld_lock_initialize(NAME) \
124 (void) ((NAME) = (__rtld_lock_recursive_t) _RTLD_LOCK_RECURSIVE_INITIALIZER)
125 #define __rtld_lock_trylock_recursive(NAME) \
126 __libc_lock_trylock_recursive (NAME)
127 #define __rtld_lock_lock_recursive(NAME) \
128 __libc_lock_lock_recursive(NAME)
129 #define __rtld_lock_unlock_recursive(NAME) \
130 __libc_lock_unlock_recursive (NAME)
131
132
133 /* XXX for now */
134 #define __libc_rwlock_define __libc_lock_define
135 #define __libc_rwlock_define_initialized __libc_lock_define_initialized
136 #define __libc_rwlock_init __libc_lock_init
137 #define __libc_rwlock_fini __libc_lock_fini
138 #define __libc_rwlock_rdlock __libc_lock_lock
139 #define __libc_rwlock_wrlock __libc_lock_lock
140 #define __libc_rwlock_tryrdlock __libc_lock_trylock
141 #define __libc_rwlock_trywrlock __libc_lock_trylock
142 #define __libc_rwlock_unlock __libc_lock_unlock
143
144
145 /* Start a critical region with a cleanup function */
146 #define __libc_cleanup_region_start(DOIT, FCT, ARG) \
147 { \
148 typeof (***(FCT)) *__save_FCT = (DOIT) ? (FCT) : 0; \
149 typeof (ARG) __save_ARG = ARG; \
150 /* close brace is in __libc_cleanup_region_end below. */
151
152 /* End a critical region started with __libc_cleanup_region_start. */
153 #define __libc_cleanup_region_end(DOIT) \
154 if ((DOIT) && __save_FCT != 0) \
155 (*__save_FCT)(__save_ARG); \
156 }
157
158 /* Sometimes we have to exit the block in the middle. */
159 #define __libc_cleanup_end(DOIT) \
160 if ((DOIT) && __save_FCT != 0) \
161 (*__save_FCT)(__save_ARG); \
162
163 #define __libc_cleanup_push(fct, arg) __libc_cleanup_region_start (1, fct, arg)
164 #define __libc_cleanup_pop(execute) __libc_cleanup_region_end (execute)
165
166 #if (_CTHREADS_ - 0)
167
168 /* Use mutexes as once control variables. */
169
170 struct __libc_once
171 {
172 __libc_lock_t lock;
173 int done;
174 };
175
176 #define __libc_once_define(CLASS,NAME) \
177 CLASS struct __libc_once NAME = { MUTEX_INITIALIZER, 0 }
178
179 /* Call handler iff the first call. */
180 #define __libc_once(ONCE_CONTROL, INIT_FUNCTION) \
181 do { \
182 __libc_lock_lock (ONCE_CONTROL.lock); \
183 if (!ONCE_CONTROL.done) \
184 (INIT_FUNCTION) (); \
185 ONCE_CONTROL.done = 1; \
186 __libc_lock_unlock (ONCE_CONTROL.lock); \
187 } while (0)
188
189 /* Get once control variable. */
190 #define __libc_once_get(ONCE_CONTROL) ((ONCE_CONTROL).done != 0)
191
192 #ifdef _LIBC
193 /* We need portable names for some functions. E.g., when they are
194 used as argument to __libc_cleanup_region_start. */
195 #define __libc_mutex_unlock __mutex_unlock
196 #endif
197
198 /* Type for key of thread specific data. */
199 typedef cthread_key_t __libc_key_t;
200
201 #define __libc_key_create(KEY,DEST) cthread_keycreate (KEY)
202 #define __libc_setspecific(KEY,VAL) cthread_setspecific (KEY, VAL)
203 void *__libc_getspecific (__libc_key_t key);
204
205 #endif /* _CTHREADS_ */
206
207 /* Hide the definitions which are only supposed to be used inside libc in
208 a separate file. This file is not present in the installation! */
209 #ifdef _LIBC
210 # include <libc-lockP.h>
211 #endif
212
213 #endif /* libc-lock.h */