Warning: That file was not part of the compilation database. It may have many parsing errors.

1/* libc-internal interface for mutex locks. Mach cthreads version.
2 Copyright (C) 1996-2019 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#ifdef _LIBC
23
24#include <tls.h>
25#include <lowlevellock.h>
26
27/* The locking here is very inexpensive, even for inlining. */
28#define _IO_lock_inexpensive 1
29
30typedef unsigned int __libc_lock_t;
31typedef struct
32{
33 __libc_lock_t lock;
34 int cnt;
35 void *owner;
36} __libc_lock_recursive_t;
37
38typedef __libc_lock_recursive_t __rtld_lock_recursive_t;
39
40extern char __libc_lock_self0[0];
41#define __libc_lock_owner_self() \
42 (__LIBC_NO_TLS () ? (void *)&__libc_lock_self0 : THREAD_SELF)
43
44#else
45typedef struct __libc_lock_opaque__ __libc_lock_t;
46typedef struct __libc_lock_recursive_opaque__ __libc_lock_recursive_t;
47#endif
48
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;
58
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;
63
64/* Initialize the named lock variable, leaving it in a consistent, unlocked
65 state. */
66#define __libc_lock_init(NAME) (NAME) = LLL_INITIALIZER
67
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
74
75/* Lock the named lock variable. */
76#define __libc_lock_lock(NAME) \
77 ({ lll_lock (&(NAME), 0); 0; })
78
79/* Lock the named lock variable. */
80#define __libc_lock_trylock(NAME) lll_trylock (&(NAME))
81
82/* Unlock the named lock variable. */
83#define __libc_lock_unlock(NAME) \
84 ({ lll_unlock (&(NAME), 0); 0; })
85
86#define __libc_lock_define_recursive(CLASS,NAME) \
87 CLASS __libc_lock_recursive_t NAME;
88
89#define _LIBC_LOCK_RECURSIVE_INITIALIZER { LLL_INITIALIZER, 0, 0 }
90
91#define __libc_lock_define_initialized_recursive(CLASS,NAME) \
92 CLASS __libc_lock_recursive_t NAME = _LIBC_LOCK_RECURSIVE_INITIALIZER;
93
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)
100
101#define __libc_lock_init_recursive(NAME) \
102 ({ \
103 (NAME) = (__libc_lock_recursive_t)_LIBC_LOCK_RECURSIVE_INITIALIZER; \
104 0; \
105 })
106
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; \
117 })
118
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) \
124 { \
125 lll_lock (&__lock->lock, 0); \
126 __lock->owner = __self; \
127 } \
128 ++__lock->cnt; \
129 (void)0; \
130 })
131
132#define __libc_lock_unlock_recursive(NAME) \
133 ({ \
134 __libc_lock_recursive_t *const __lock = &(NAME); \
135 if (--__lock->cnt == 0) \
136 { \
137 __lock->owner = 0; \
138 lll_unlock (&__lock->lock, 0); \
139 } \
140 })
141
142
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)
151
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
162
163struct __libc_cleanup_frame
164{
165 void (*__fct) (void *);
166 void *__argp;
167 int __doit;
168};
169
170__extern_inline void
171__libc_cleanup_fct (struct __libc_cleanup_frame *framep)
172{
173 if (framep->__doit)
174 framep->__fct (framep->__argp);
175}
176
177/* Start a critical region with a cleanup function */
178#define __libc_cleanup_region_start(DOIT, FCT, ARG) \
179 do \
180 { \
181 struct __libc_cleanup_frame __cleanup \
182 __attribute__ ((__cleanup__ (__libc_cleanup_fct))) = \
183 { .__fct = (FCT), .__argp = (ARG), .__doit = (DOIT) };
184
185/* This one closes the brace above. */
186#define __libc_cleanup_region_end(DOIT) \
187 __cleanup.__doit = (DOIT); \
188 } \
189 while (0)
190
191#define __libc_cleanup_end(DOIT) __cleanup.__doit = (DOIT);
192
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)
195
196/* Use mutexes as once control variables. */
197
198struct __libc_once
199 {
200 __libc_lock_t lock;
201 int done;
202 };
203
204#define __libc_once_define(CLASS,NAME) \
205 CLASS struct __libc_once NAME = { _LIBC_LOCK_INITIALIZER, 0 }
206
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)
216
217/* Get once control variable. */
218#define __libc_once_get(ONCE_CONTROL) ((ONCE_CONTROL).done != 0)
219
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
224
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
229
230#endif /* libc-lock.h */
231

Warning: That file was not part of the compilation database. It may have many parsing errors.