2.9
[glibc/nacl-glibc.git] / sysdeps / mach / hurd / bits / libc-lock.h
blob0fa90bcc3ecbfc8ad27f9b48f8cdaa2430a9c4aa
1 /* libc-internal interface for mutex locks. Hurd version using Mach cthreads.
2 Copyright (C) 1996,97,98,2000,01, 2002, 2003 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #ifndef _BITS_LIBC_LOCK_H
21 #define _BITS_LIBC_LOCK_H 1
23 #if (_LIBC - 0) || (_CTHREADS_ - 0)
24 #include <cthreads.h>
25 #include <hurd/threadvar.h>
27 typedef struct mutex __libc_lock_t;
28 typedef struct
30 struct mutex mutex;
31 void *owner;
32 int count;
33 } __libc_lock_recursive_t;
35 #define __libc_lock_owner_self() ((void *) __hurd_threadvar_location (0))
37 #else
38 typedef struct __libc_lock_opaque__ __libc_lock_t;
39 typedef struct __libc_lock_recursive_opaque__ __libc_lock_recursive_t;
40 #endif
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;
52 /* Define an initialized lock variable NAME with storage class CLASS. */
53 #define __libc_lock_define_initialized(CLASS,NAME) \
54 CLASS __libc_lock_t NAME = MUTEX_INITIALIZER;
56 /* Initialize the named lock variable, leaving it in a consistent, unlocked
57 state. */
58 #define __libc_lock_init(NAME) __mutex_init (&(NAME))
60 /* Finalize the named lock variable, which must be locked. It cannot be
61 used again until __libc_lock_init is called again on it. This must be
62 called on a lock variable before the containing storage is reused. */
63 #define __libc_lock_fini(NAME) __mutex_unlock (&(NAME))
64 #define __libc_lock_fini_recursive(NAME) __mutex_unlock (&(NAME).mutex)
65 #define __rtld_lock_fini_recursive(NAME) __mutex_unlock (&(NAME).mutex)
68 /* Lock the named lock variable. */
69 #define __libc_lock_lock(NAME) __mutex_lock (&(NAME))
71 /* Lock the named lock variable. */
72 #define __libc_lock_trylock(NAME) (!__mutex_trylock (&(NAME)))
74 /* Unlock the named lock variable. */
75 #define __libc_lock_unlock(NAME) __mutex_unlock (&(NAME))
78 #define __libc_lock_define_recursive(CLASS,NAME) \
79 CLASS __libc_lock_recursive_t NAME;
80 #define _LIBC_LOCK_RECURSIVE_INITIALIZER { MUTEX_INITIALIZER, 0, 0 }
81 #define __libc_lock_define_initialized_recursive(CLASS,NAME) \
82 CLASS __libc_lock_recursive_t NAME = _LIBC_LOCK_RECURSIVE_INITIALIZER;
84 #define __rtld_lock_define_recursive(CLASS,NAME) \
85 __libc_lock_define_recursive (CLASS, NAME)
86 #define _RTLD_LOCK_RECURSIVE_INITIALIZER \
87 _LIBC_LOCK_RECURSIVE_INITIALIZER
88 #define __rtld_lock_define_initialized_recursive(CLASS,NAME) \
89 __libc_lock_define_initialized_recursive (CLASS, NAME)
91 #define __libc_lock_init_recursive(NAME) \
92 ({ __libc_lock_recursive_t *const __lock = &(NAME); \
93 __lock->owner = 0; mutex_init (&__lock->mutex); })
95 #define __libc_lock_trylock_recursive(NAME) \
96 ({ __libc_lock_recursive_t *const __lock = &(NAME); \
97 void *__self = __libc_lock_owner_self (); \
98 __mutex_trylock (&__lock->mutex) \
99 ? (__lock->owner = __self, __lock->count = 1, 0) \
100 : __lock->owner == __self ? (++__lock->count, 0) : 1; })
102 #define __libc_lock_lock_recursive(NAME) \
103 ({ __libc_lock_recursive_t *const __lock = &(NAME); \
104 void *__self = __libc_lock_owner_self (); \
105 if (__mutex_trylock (&__lock->mutex) \
106 || (__lock->owner != __self \
107 && (__mutex_lock (&__lock->mutex), 1))) \
108 __lock->owner = __self, __lock->count = 1; \
109 else \
110 ++__lock->count; \
112 #define __libc_lock_unlock_recursive(NAME) \
113 ({ __libc_lock_recursive_t *const __lock = &(NAME); \
114 if (--__lock->count == 0) \
116 __lock->owner = 0; \
117 __mutex_unlock (&__lock->mutex); \
122 #define __rtld_lock_init_recursive(NAME) \
123 __libc_lock_init_recursive (NAME)
124 #define __rtld_lock_trylock_recursive(NAME) \
125 __libc_lock_trylock_recursive (NAME)
126 #define __rtld_lock_lock_recursive(NAME) \
127 __libc_lock_lock_recursive(NAME)
128 #define __rtld_lock_unlock_recursive(NAME) \
129 __libc_lock_unlock_recursive (NAME)
132 /* XXX for now */
133 #define __libc_rwlock_define __libc_lock_define
134 #define __libc_rwlock_define_initialized __libc_lock_define_initialized
135 #define __libc_rwlock_init __libc_lock_init
136 #define __libc_rwlock_fini __libc_lock_fini
137 #define __libc_rwlock_rdlock __libc_lock_lock
138 #define __libc_rwlock_wrlock __libc_lock_lock
139 #define __libc_rwlock_tryrdlock __libc_lock_trylock
140 #define __libc_rwlock_trywrlock __libc_lock_trylock
141 #define __libc_rwlock_unlock __libc_lock_unlock
144 /* Start a critical region with a cleanup function */
145 #define __libc_cleanup_region_start(DOIT, FCT, ARG) \
147 typeof (***(FCT)) *__save_FCT = (DOIT) ? (FCT) : 0; \
148 typeof (ARG) __save_ARG = ARG; \
149 /* close brace is in __libc_cleanup_region_end below. */
151 /* End a critical region started with __libc_cleanup_region_start. */
152 #define __libc_cleanup_region_end(DOIT) \
153 if ((DOIT) && __save_FCT != 0) \
154 (*__save_FCT)(__save_ARG); \
157 /* Sometimes we have to exit the block in the middle. */
158 #define __libc_cleanup_end(DOIT) \
159 if ((DOIT) && __save_FCT != 0) \
160 (*__save_FCT)(__save_ARG); \
162 #define __libc_cleanup_push(fct, arg) __libc_cleanup_region_start (1, fct, arg)
163 #define __libc_cleanup_pop(execute) __libc_cleanup_region_end (execute)
165 #if (_CTHREADS_ - 0)
167 /* Use mutexes as once control variables. */
169 struct __libc_once
171 __libc_lock_t lock;
172 int done;
175 #define __libc_once_define(CLASS,NAME) \
176 CLASS struct __libc_once NAME = { MUTEX_INITIALIZER, 0 }
178 /* Call handler iff the first call. */
179 #define __libc_once(ONCE_CONTROL, INIT_FUNCTION) \
180 do { \
181 __libc_lock_lock (ONCE_CONTROL.lock); \
182 if (!ONCE_CONTROL.done) \
183 (INIT_FUNCTION) (); \
184 ONCE_CONTROL.done = 1; \
185 __libc_lock_unlock (ONCE_CONTROL.lock); \
186 } while (0)
188 #ifdef _LIBC
189 /* We need portable names for some functions. E.g., when they are
190 used as argument to __libc_cleanup_region_start. */
191 #define __libc_mutex_unlock __mutex_unlock
192 #endif
194 /* Type for key of thread specific data. */
195 typedef cthread_key_t __libc_key_t;
197 #define __libc_key_create(KEY,DEST) cthread_keycreate (KEY)
198 #define __libc_setspecific(KEY,VAL) cthread_setspecific (KEY, VAL)
199 void *__libc_getspecific (__libc_key_t key);
201 #endif /* _CTHREADS_ */
203 #endif /* bits/libc-lock.h */