Upgrade to gcc-4.9.2
[akaros.git] / tools / compilers / gcc-glibc / glibc-2.14.1-ros / sysdeps / akaros / bits / libc-lock.h
1 /* libc-internal interface for mutex locks.  NPTL version.
2    Copyright (C) 1996-2003, 2005, 2007 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 License as
7    published by the Free Software Foundation; either version 2.1 of the
8    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; see the file COPYING.LIB.  If not,
17    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18    Boston, MA 02111-1307, USA.  */
19
20 #ifndef _BITS_LIBC_LOCK_H
21 #define _BITS_LIBC_LOCK_H 1
22
23 #define __need_NULL
24 #include <stddef.h>
25
26 #ifdef _LIBC
27 # include <lowlevellock.h>
28 # include <tls.h>
29 #endif
30
31 /* Lock types.  */
32 typedef int __libc_lock_t; 
33 #define _LIBC_LOCK_INITIALIZER LLL_LOCK_INITIALIZER
34
35 typedef struct __libc_lock_recursive { 
36         __libc_lock_t lock; 
37         int count; 
38         void* owner; 
39 } __libc_lock_recursive_t;
40 #define _LIBC_LOCK_RECURSIVE_INITIALIZER {_LIBC_LOCK_INITIALIZER,0,0}
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 #define __libc_lock_define_recursive(CLASS,NAME)\
52         CLASS __libc_lock_recursive_t NAME;
53
54 /* Define an initialized lock variable NAME with storage class CLASS.
55    For the C library we take a deeper look at the initializer.  For
56    this implementation all fields are initialized to zero.  Therefore
57    we don't initialize the variable which allows putting it into the
58    BSS section.  (Except on PA-RISC and other odd architectures, where
59    initialized locks must be set to one due to the lack of normal
60    atomic operations.) */
61
62 #if LLL_LOCK_INITIALIZER == 0
63 #define __libc_lock_define_initialized(CLASS,NAME)\
64         CLASS __libc_lock_t NAME;
65 #else
66 #define __libc_lock_define_initialized(CLASS,NAME)\
67         CLASS __libc_lock_t NAME = _LIBC_LOCK_INITIALIZER;
68 #endif
69
70 /* Define an initialized recursive lock variable NAME with storage
71    class CLASS.  */
72 #if LLL_LOCK_INITIALIZER == 0
73 #define __libc_lock_define_initialized_recursive(CLASS,NAME)\
74         CLASS __libc_lock_recursive_t NAME;
75 #else
76 #define __libc_lock_define_initialized_recursive(CLASS,NAME)\
77         CLASS __libc_lock_recursive_t NAME = _LIBC_LOCK_RECURSIVE_INITIALIZER;
78 #endif
79
80 /* Initialize the named lock variable, leaving it in a consistent, unlocked
81    state.  */
82 #define __libc_lock_init(NAME) ((NAME) = _LIBC_LOCK_INITIALIZER, 0)
83
84 /* Same as last but this time we initialize a recursive mutex.  */
85 #if defined _LIBC && (!defined NOT_IN_libc)
86 #define __libc_lock_init_recursive(NAME)\
87         ((NAME) = (__libc_lock_recursive_t) _LIBC_LOCK_RECURSIVE_INITIALIZER, 0)
88 #else
89 #define __libc_lock_init_recursive(NAME)\
90 do {\
91         NAME.lock = 0;\
92         NAME.count = 0;\
93         NAME.owner = 0;\
94 } while (0)
95 #endif
96
97 /* Finalize the named lock variable, which must be locked.  It cannot be
98    used again until __libc_lock_init is called again on it.  This must be
99    called on a lock variable before the containing storage is reused.  */
100 #define __libc_lock_fini(NAME) ((void) 0)
101
102 /* Finalize recursive named lock.  */
103 #define __libc_lock_fini_recursive(NAME) ((void) 0)
104
105 /* Lock the named lock variable.  */
106 # define __libc_lock_lock(NAME)\
107         ({ lll_lock (NAME, LLL_PRIVATE); 0; })
108
109 /* Lock the recursive named lock variable.  */
110 #ifndef IS_IN_rtld
111
112 # define __libc_lock_lock_recursive(NAME)\
113 do {\
114         void *self = THREAD_SELF;\
115         if((NAME).owner != self) {\
116                 lll_lock ((NAME).lock, LLL_PRIVATE);\
117                 (NAME).owner = self;\
118         }\
119         ++(NAME).count;\
120 } while (0)
121
122 /* Unlock the recursive named lock variable.  */
123 /* We do no error checking here.  */
124 # define __libc_lock_unlock_recursive(NAME)\
125         do {\
126                 if(--(NAME).count == 0) {\
127                         (NAME).owner = NULL;\
128                         lll_unlock((NAME).lock, LLL_PRIVATE);\
129                 }\
130         } while (0)
131
132 #else /* Ignore recursive locks within rtld */
133
134 # define __libc_lock_lock_recursive(NAME) do { } while(0)
135 # define __libc_lock_unlock_recursive(NAME) do { } while(0)
136
137 #endif
138
139 /* Try to lock the named lock variable.  */
140 #define __libc_lock_trylock(NAME)\
141         lll_trylock(NAME)
142
143 /* Try to lock the recursive named lock variable.  */
144 #define __libc_lock_trylock_recursive(NAME)\
145 ({\
146         int result = 0;\
147         void *self = THREAD_SELF;\
148         if((NAME).owner != self) {\
149                 if(lll_trylock((NAME).lock) == 0) {\
150                         (NAME).owner = self;\
151                         (NAME).count = 1;\
152                 }\
153                 else\
154                         result = EBUSY;\
155     }\
156         else\
157                 ++(NAME).count;\
158         result;\
159 })
160
161 /* Unlock the named lock variable.  */
162 #define __libc_lock_unlock(NAME)\
163         lll_unlock (NAME, LLL_PRIVATE)
164
165 #define __libc_lock_default_lock_recursive(lock)\
166         ++((__libc_lock_recursive_t *)(lock))->count;
167 #define __libc_lock_default_unlock_recursive(lock)\
168         --((__libc_lock_recursive_t *)(lock))->count;
169
170 /* libc's rwlocks are the same as regular locks for now... */
171 typedef __libc_lock_t __libc_rwlock_t; 
172 #define _LIBC_RWLOCK_INITIALIZER _LIBC_LOCK_INITIALIZER
173 typedef __libc_lock_recursive_t __libc_rwlock_recursive_t; 
174 #define _LIBC_RWLOCK_RECURSIVE_INITIALIZER _LIBC_LOCK_RECURSIVE_INITIALIZER
175 #define __libc_rwlock_define(CLASS,NAME)\
176         __libc_lock_define(CLASS,NAME)
177 #define __libc_rwlock_define_recursive(CLASS,NAME)\
178         __libc_lock_define_recursive(CLASS,NAME)
179 #define __libc_rwlock_define_initialized(CLASS,NAME)\
180         __libc_lock_define_initialized(CLASS,NAME)
181 #define __libc_rwlock_define_initialized_recursive(CLASS,NAME)\
182         __libc_lock_define_initialized_recursive(CLASS,NAME)
183 #define __libc_rwlock_init(NAME)\
184         __libc_lock_init(NAME)
185 #define __libc_rwlock_init_recursive(NAME)\
186         __libc_lock_init_recursive(NAME)
187 #define __libc_rwlock_fini(NAME)\
188         __libc_lock_fini(NAME)
189 #define __libc_rwlock_fini_recursive(NAME)\
190         __libc_lock_fini_recursive(NAME)
191 #define __libc_rwlock_rdlock(NAME)\
192         __libc_lock_lock(NAME)
193 #define __libc_rwlock_wrlock(NAME)\
194         __libc_lock_lock(NAME)
195 #define __libc_rwlock_rdlock_recursive(NAME)\
196         __libc_lock_lock_recursive(NAME)
197 #define __libc_rwlock_wrlock_recursive(NAME)\
198         __libc_lock_lock_recursive(NAME)
199 #define __libc_rwlock_tryrlock(NAME)\
200         __libc_lock_trylock(NAME)
201 #define __libc_rwlock_trywlock(NAME)\
202         __libc_lock_trylock(NAME)
203 #define __libc_rwlock_tryrlock_recursive(NAME)\
204         __libc_lock_trylock_recursive(NAME)
205 #define __libc_rwlock_trywlock_recursive(NAME)\
206         __libc_lock_trylock_recursive(NAME)
207 #define __libc_rwlock_unlock(NAME)\
208         __libc_lock_unlock(NAME) 
209 #define __libc_rwlock_unlock_recursive(NAME)\
210         __libc_lock_unlock_recursive(NAME) 
211 #define __libc_rwlock_default_rdlock_recursive(lock)\
212         __libc_lock_default_lock_recursive(lock)
213 #define __libc_rwlock_default_wrlock_recursive(lock)\
214         __libc_lock_default_lock_recursive(lock)
215 #define __libc_rwlock_default_unlock_recursive(lock)\
216         __libc_lock_default_unlock_recursive(lock)
217
218 /* rtld locks are the same as libc locks */
219 typedef __libc_lock_t __rtld_lock_t;
220 #define _RTLD_LOCK_INITIALIZER _LIBC_LOCK_INITIALIZER
221 typedef __libc_lock_recursive_t __rtld_lock_recursive_t;
222 #define _RTLD_LOCK_RECURSIVE_INITIALIZER _LIBC_LOCK_RECURSIVE_INITIALIZER
223 #define __rtld_lock_define(CLASS,NAME)\
224         __libc_lock_define_recursive(CLASS,NAME)
225 #define __rtld_lock_define_recursive(CLASS,NAME)\
226         __libc_lock_define_recursive(CLASS,NAME)
227 #define __rtld_lock_define_initialized(CLASS,NAME)\
228         __libc_lock_define_initialized_recursive(CLASS,NAME)
229 #define __rtld_lock_define_initialized_recursive(CLASS,NAME)\
230         __libc_lock_define_initialized_recursive(CLASS,NAME)
231 #define __rtld_lock_initialize(NAME)\
232         __libc_lock_init_recursive(NAME)
233 #define __rtld_lock_init_recursive(NAME)\
234         __libc_lock_init_recursive(NAME)
235 # define __rtld_lock_fini(NAME)\
236         __libc_lock_fini_recursive(NAME)
237 # define __rtld_lock_fini_recursive(NAME)\
238         __libc_lock_fini_recursive(NAME)
239 #define __rtld_lock_lock(NAME)\
240         __libc_lock_lock_recursive(NAME)
241 #define __rtld_lock_lock_recursive(NAME)\
242         __libc_lock_lock_recursive(NAME)
243 #define __rtld_lock_trylock(NAME)\
244         __libc_lock_trylock_recursive(NAME)
245 #define __rtld_lock_trylock_recursive(NAME)\
246         __libc_lock_trylock_recursive(NAME)
247 #define __rtld_lock_unlock(NAME)\
248         __libc_lock_unlock_recursive(NAME) 
249 #define __rtld_lock_unlock_recursive(NAME)\
250         __libc_lock_unlock_recursive(NAME) 
251 #define __rtld_lock_default_lock_recursive(lock)\
252         __libc_lock_default_lock_recursive(lock)
253 #define __rtld_lock_default_unlock_recursive(lock)\
254         __libc_lock_default_unlock_recursive(lock)
255 #define __rtld_rwlock_define(CLASS,NAME)\
256         __libc_rwlock_define_recursive(CLASS,NAME)
257 #define __rtld_rwlock_define_recursive(CLASS,NAME)\
258         __libc_rwlock_define_recursive(CLASS,NAME)
259 #define __rtld_rwlock_define_initialized(CLASS,NAME)\
260         __libc_rwlock_define_initialized_recursive(CLASS,NAME)
261 #define __rtld_rwlock_define_initialized_recursive(CLASS,NAME)\
262         __libc_rwlock_define_initialized_recursive(CLASS,NAME)
263 #define __rtld_rwlock_init(NAME)\
264         __libc_rwlock_init_recursive(NAME)
265 #define __rtld_rwlock_init_recursive(NAME)\
266         __libc_rwlock_init_recursive(NAME)
267 #define __rtld_rwlock_fini(NAME)\
268         __libc_rwlock_fini_recursive(NAME)
269 #define __rtld_rwlock_fini_recursive(NAME)\
270         __libc_rwlock_fini_recursive(NAME)
271 #define __rtld_rwlock_rdlock(NAME)\
272         __libc_rwlock_lock_recursive(NAME)
273 #define __rtld_rwlock_wrlock(NAME)\
274         __libc_rwlock_lock_recursive(NAME)
275 #define __rtld_rwlock_rdlock_recursive(NAME)\
276         __libc_rwlock_lock_recursive(NAME)
277 #define __rtld_rwlock_wrlock_recursive(NAME)\
278         __libc_rwlock_lock_recursive(NAME)
279 #define __rtld_rwlock_tryrlock(NAME)\
280         __libc_rwlock_trylock_recursive(NAME)
281 #define __rtld_rwlock_trywlock(NAME)\
282         __libc_rwlock_trylock_recursive(NAME)
283 #define __rtld_rwlock_tryrlock_recursive(NAME)\
284         __libc_rwlock_trylock_recursive(NAME)
285 #define __rtld_rwlock_trywlock_recursive(NAME)\
286         __libc_rwlock_trylock_recursive(NAME)
287 #define __rtld_rwlock_unlock(NAME)\
288         __libc_rwlock_unlock_recursive(NAME) 
289 #define __rtld_rwlock_unlock_recursive(NAME)\
290         __libc_rwlock_unlock_recursive(NAME) 
291 #define __rtld_rwlock_default_rdlock_recursive(lock)\
292         __libc_rwlock_default_lock_recursive(lock)
293 #define __rtld_rwlock_default_wrlock_recursive(lock)\
294         __libc_rwlock_default_lock_recursive(lock)
295 #define __rtld_rwlock_default_unlock_recursive(lock)\
296         __libc_rwlock_default_unlock_recursive(lock)
297
298 /* Define once control variable.  */
299 #define __libc_once_define(CLASS, NAME) CLASS int NAME = 0
300
301 /* Call handler iff the first call.  */
302 #define __libc_once(ONCE_CONTROL, INIT_FUNCTION)\
303 do {\
304         if((ONCE_CONTROL) == 0) {\
305                 INIT_FUNCTION ();\
306                 (ONCE_CONTROL) = 1;\
307         }\
308 } while (0)
309
310 /* Start a critical region with a cleanup function */
311 #define __libc_cleanup_region_start(DOIT, FCT, ARG)\
312 {\
313   typeof (***(FCT)) *__save_FCT = (DOIT) ? (FCT) : 0;\
314   typeof (ARG) __save_ARG = ARG;\
315   /* close brace is in __libc_cleanup_region_end below. */
316
317 /* End a critical region started with __libc_cleanup_region_start. */
318 #define __libc_cleanup_region_end(DOIT)\
319 if((DOIT) && __save_FCT != 0)\
320     (*__save_FCT)(__save_ARG);\
321 }
322
323 /* Sometimes we have to exit the block in the middle.  */
324 #define __libc_cleanup_end(DOIT)\
325 if ((DOIT) && __save_FCT != 0)\
326         (*__save_FCT)(__save_ARG);\
327
328 #define __libc_cleanup_push(fct, arg) __libc_cleanup_region_start (1, fct, arg)
329 #define __libc_cleanup_pop(execute) __libc_cleanup_region_end (execute)
330
331 /* We need portable names for some of the functions.  */
332 #define __libc_mutex_unlock
333
334 /* Type for key of thread specific data.  */
335 typedef int __libc_key_t;
336
337 /* Create key for thread specific data.  */
338 #define __libc_key_create(KEY,DEST) -1
339
340 /* Set thread-specific data associated with KEY to VAL.  */
341 #define __libc_setspecific(KEY,VAL) ((void)0)
342
343 /* Get thread-specific data associated with KEY.  */
344 #define __libc_getspecific(KEY) 0
345
346 #endif  /* bits/libc-lock.h */