Upload Tizen:Base source
[external/eglibc.git] / sysdeps / mach / bits / libc-lock.h
1 /* libc-internal interface for mutex locks.  Mach cthreads version.
2    Copyright (C) 1996,97,98,2000,01, 2002 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, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #ifndef _BITS_LIBC_LOCK_H
21 #define _BITS_LIBC_LOCK_H 1
22
23 #ifdef _LIBC
24 #include <cthreads.h>
25 #define __libc_lock_t struct mutex
26 #else
27 typedef struct __libc_lock_opaque__ __libc_lock_t;
28 #endif
29
30 /* Type for key of thread specific data.  */
31 typedef cthread_key_t __libc_key_t;
32
33 /* Define a lock variable NAME with storage class CLASS.  The lock must be
34    initialized with __libc_lock_init before it can be used (or define it
35    with __libc_lock_define_initialized, below).  Use `extern' for CLASS to
36    declare a lock defined in another module.  In public structure
37    definitions you must use a pointer to the lock structure (i.e., NAME
38    begins with a `*'), because its storage size will not be known outside
39    of libc.  */
40 #define __libc_lock_define(CLASS,NAME) \
41   CLASS __libc_lock_t NAME;
42
43 /* Define an initialized lock variable NAME with storage class CLASS.  */
44 #define __libc_lock_define_initialized(CLASS,NAME) \
45   CLASS __libc_lock_t NAME = MUTEX_INITIALIZER;
46
47 /* Initialize the named lock variable, leaving it in a consistent, unlocked
48    state.  */
49 #define __libc_lock_init(NAME) __mutex_init (&(NAME))
50
51 /* Finalize the named lock variable, which must be locked.  It cannot be
52    used again until __libc_lock_init is called again on it.  This must be
53    called on a lock variable before the containing storage is reused.  */
54 #define __libc_lock_fini(NAME) __mutex_unlock (&(NAME))
55
56 /* Lock the named lock variable.  */
57 #define __libc_lock_lock(NAME) __mutex_lock (&(NAME))
58
59 /* Lock the named lock variable.  */
60 #define __libc_lock_trylock(NAME) (!__mutex_trylock (&(NAME)))
61
62 /* Unlock the named lock variable.  */
63 #define __libc_lock_unlock(NAME) __mutex_unlock (&(NAME))
64
65
66 /* XXX for now */
67 #define __libc_rwlock_define            __libc_lock_define
68 #define __libc_rwlock_define_initialized __libc_lock_define_initialized
69 #define __libc_rwlock_init              __libc_lock_init
70 #define __libc_rwlock_fini              __libc_lock_fini
71 #define __libc_rwlock_rdlock            __libc_lock_lock
72 #define __libc_rwlock_wrlock            __libc_lock_lock
73 #define __libc_rwlock_tryrdlock         __libc_lock_trylock
74 #define __libc_rwlock_trywrlock         __libc_lock_trylock
75 #define __libc_rwlock_unlock            __libc_lock_unlock
76
77
78 /* Start a critical region with a cleanup function */
79 #define __libc_cleanup_region_start(DOIT, FCT, ARG)                         \
80 {                                                                           \
81   typeof (***(FCT)) *__save_FCT = (DOIT) ? (FCT) : 0;                       \
82   typeof (ARG) __save_ARG = ARG;                                            \
83   /* close brace is in __libc_cleanup_region_end below. */
84
85 /* End a critical region started with __libc_cleanup_region_start. */
86 #define __libc_cleanup_region_end(DOIT)                                     \
87   if ((DOIT) && __save_FCT != 0)                                            \
88     (*__save_FCT)(__save_ARG);                                              \
89 }
90
91 /* Sometimes we have to exit the block in the middle.  */
92 #define __libc_cleanup_end(DOIT)                                            \
93   if ((DOIT) && __save_FCT != 0)                                            \
94     (*__save_FCT)(__save_ARG);                                              \
95
96
97 /* Use mutexes as once control variables. */
98
99 struct __libc_once
100   {
101     __libc_lock_t lock;
102     int done;
103   };
104
105 #define __libc_once_define(CLASS,NAME) \
106   CLASS struct __libc_once NAME = { MUTEX_INITIALIZER, 0 }
107
108
109 /* Call handler iff the first call.  */
110 #define __libc_once(ONCE_CONTROL, INIT_FUNCTION) \
111   do {                                                                        \
112     __libc_lock_lock (ONCE_CONTROL.lock);                                     \
113     if (!ONCE_CONTROL.done)                                                   \
114       (INIT_FUNCTION) ();                                                     \
115     ONCE_CONTROL.done = 1;                                                    \
116     __libc_lock_unlock (ONCE_CONTROL.lock);                                   \
117   } while (0)
118
119 #ifdef _LIBC
120 /* We need portable names for some functions.  E.g., when they are
121    used as argument to __libc_cleanup_region_start.  */
122 #define __libc_mutex_unlock __mutex_unlock
123 #endif
124
125 #define __libc_key_create(KEY,DEST) cthread_keycreate (KEY)
126 #define __libc_setspecific(KEY,VAL) cthread_setspecific (KEY, VAL)
127 void *__libc_getspecific (__libc_key_t key);
128
129 /* XXX until cthreads supports recursive locks */
130 #define __libc_lock_define_initialized_recursive __libc_lock_define_initialized
131 #define __libc_lock_init_recursive __libc_lock_init
132 #define __libc_lock_fini_recursive __libc_lock_fini
133 #define __libc_lock_trylock_recursive __libc_lock_trylock
134 #define __libc_lock_unlock_recursive __libc_lock_unlock
135 #define __libc_lock_lock_recursive __libc_lock_lock
136
137 #define __rtld_lock_define_initialized_recursive __libc_lock_define_initialized
138 #define __rtld_lock_init_recursive __libc_lock_init
139 #define __rtld_lock_fini_recursive __libc_lock_fini
140 #define __rtld_lock_trylock_recursive __libc_lock_trylock
141 #define __rtld_lock_unlock_recursive __libc_lock_unlock
142 #define __rtld_lock_lock_recursive __libc_lock_lock
143
144 #endif  /* bits/libc-lock.h */