libc-lock.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /* libc-internal interface for mutex locks. LinuxThreads version.
  2. Copyright (C) 1996,1997,1998,1999,2000,2001,2002,2003
  3. Free Software Foundation, Inc.
  4. 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 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. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; see the file COPYING.LIB. If
  15. not, see <http://www.gnu.org/licenses/>. */
  16. #ifndef _BITS_LIBC_LOCK_H
  17. #define _BITS_LIBC_LOCK_H 1
  18. #include <pthread.h>
  19. #if defined _LIBC && !defined NOT_IN_libc
  20. #include <linuxthreads/internals.h>
  21. #endif
  22. /* Mutex type. */
  23. #if defined(_LIBC) || defined(_IO_MTSAFE_IO)
  24. typedef pthread_mutex_t __libc_lock_t;
  25. typedef pthread_mutex_t __libc_lock_recursive_t;
  26. # ifdef __USE_UNIX98
  27. typedef pthread_rwlock_t __libc_rwlock_t;
  28. # else
  29. typedef struct __libc_rwlock_opaque__ __libc_rwlock_t;
  30. # endif
  31. typedef __libc_lock_recursive_t __rtld_lock_recursive_t;
  32. #else
  33. typedef struct __libc_lock_opaque__ __libc_lock_t;
  34. typedef struct __libc_lock_recursive_opaque__ __libc_lock_recursive_t;
  35. typedef struct __libc_rwlock_opaque__ __libc_rwlock_t;
  36. #endif
  37. /* Type for key to thread-specific data. */
  38. typedef pthread_key_t __libc_key_t;
  39. /* Define a lock variable NAME with storage class CLASS. The lock must be
  40. initialized with __libc_lock_init before it can be used (or define it
  41. with __libc_lock_define_initialized, below). Use `extern' for CLASS to
  42. declare a lock defined in another module. In public structure
  43. definitions you must use a pointer to the lock structure (i.e., NAME
  44. begins with a `*'), because its storage size will not be known outside
  45. of libc. */
  46. #define __libc_lock_define(CLASS,NAME) \
  47. CLASS __libc_lock_t NAME;
  48. #define __libc_rwlock_define(CLASS,NAME) \
  49. CLASS __libc_rwlock_t NAME;
  50. #define __libc_lock_define_recursive(CLASS,NAME) \
  51. CLASS __libc_lock_recursive_t NAME;
  52. #define __rtld_lock_define_recursive(CLASS,NAME) \
  53. CLASS __rtld_lock_recursive_t NAME;
  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. #if __LT_SPINLOCK_INIT == 0
  62. # define __libc_lock_define_initialized(CLASS,NAME) \
  63. CLASS __libc_lock_t NAME;
  64. #else
  65. # define __libc_lock_define_initialized(CLASS,NAME) \
  66. CLASS __libc_lock_t NAME = PTHREAD_MUTEX_INITIALIZER;
  67. #endif
  68. #define __libc_rwlock_define_initialized(CLASS,NAME) \
  69. CLASS __libc_rwlock_t NAME = PTHREAD_RWLOCK_INITIALIZER;
  70. /* Define an initialized recursive lock variable NAME with storage
  71. class CLASS. */
  72. #define __libc_lock_define_initialized_recursive(CLASS,NAME) \
  73. CLASS __libc_lock_recursive_t NAME = _LIBC_LOCK_RECURSIVE_INITIALIZER;
  74. #define _LIBC_LOCK_RECURSIVE_INITIALIZER \
  75. {PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP}
  76. #define __rtld_lock_define_initialized_recursive(CLASS,NAME) \
  77. CLASS __rtld_lock_recursive_t NAME = _RTLD_LOCK_RECURSIVE_INITIALIZER;
  78. #define _RTLD_LOCK_RECURSIVE_INITIALIZER \
  79. {PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP}
  80. #if defined _LIBC && defined IS_IN_libpthread
  81. # define __libc_maybe_call(FUNC, ARGS, ELSE) FUNC ARGS
  82. #else
  83. # if defined __PIC__ || (defined _LIBC && defined SHARED)
  84. # define __libc_maybe_call(FUNC, ARGS, ELSE) \
  85. (__extension__ ({ __typeof (FUNC) *_fn = (FUNC); \
  86. _fn != NULL ? (*_fn) ARGS : ELSE; }))
  87. # else
  88. # define __libc_maybe_call(FUNC, ARGS, ELSE) \
  89. (FUNC != NULL ? FUNC ARGS : ELSE)
  90. # endif
  91. #endif
  92. #define __libc_maybe_call2(FUNC, ARGS, ELSE) __libc_maybe_call (__##FUNC, ARGS, ELSE)
  93. /* Initialize the named lock variable, leaving it in a consistent, unlocked
  94. state. */
  95. #if defined _LIBC && !defined NOT_IN_libc && defined SHARED
  96. #define __libc_lock_init(NAME) \
  97. ({ \
  98. (NAME).__m_count = 0; \
  99. (NAME).__m_owner = NULL; \
  100. (NAME).__m_kind = PTHREAD_MUTEX_TIMED_NP; \
  101. (NAME).__m_lock.__status = 0; \
  102. (NAME).__m_lock.__spinlock = __LT_SPINLOCK_INIT; \
  103. 0; })
  104. #else
  105. #define __libc_lock_init(NAME) \
  106. (__libc_maybe_call2 (pthread_mutex_init, (&(NAME), NULL), 0))
  107. #endif
  108. #define __libc_rwlock_init(NAME) \
  109. (__libc_maybe_call (__pthread_rwlock_init, (&(NAME), NULL), 0));
  110. /* Same as last but this time we initialize an adaptive mutex. */
  111. #if defined _LIBC && !defined NOT_IN_libc && defined SHARED
  112. #define __libc_lock_init_adaptive(NAME) \
  113. ({ \
  114. (NAME).__m_count = 0; \
  115. (NAME).__m_owner = NULL; \
  116. (NAME).__m_kind = PTHREAD_MUTEX_ADAPTIVE_NP; \
  117. (NAME).__m_lock.__status = 0; \
  118. (NAME).__m_lock.__spinlock = __LT_SPINLOCK_INIT; \
  119. 0; })
  120. #else
  121. #define __libc_lock_init_adaptive(NAME) \
  122. do { \
  123. if (__pthread_mutex_init != NULL) \
  124. { \
  125. pthread_mutexattr_t __attr; \
  126. __pthread_mutexattr_init (&__attr); \
  127. __pthread_mutexattr_settype (&__attr, PTHREAD_MUTEX_ADAPTIVE_NP); \
  128. __pthread_mutex_init (&(NAME), &__attr); \
  129. __pthread_mutexattr_destroy (&__attr); \
  130. } \
  131. } while (0);
  132. #endif
  133. /* Same as last but this time we initialize a recursive mutex. */
  134. #if defined _LIBC && !defined NOT_IN_libc && defined SHARED
  135. #define __libc_lock_init_recursive(NAME) \
  136. ({ \
  137. (NAME).__m_count = 0; \
  138. (NAME).__m_owner = NULL; \
  139. (NAME).__m_kind = PTHREAD_MUTEX_RECURSIVE_NP; \
  140. (NAME).__m_lock.__status = 0; \
  141. (NAME).__m_lock.__spinlock = __LT_SPINLOCK_INIT; \
  142. 0; })
  143. #else
  144. #define __libc_lock_init_recursive(NAME) \
  145. do { \
  146. if (__pthread_mutex_init != NULL) \
  147. { \
  148. pthread_mutexattr_t __attr; \
  149. __pthread_mutexattr_init (&__attr); \
  150. __pthread_mutexattr_settype (&__attr, PTHREAD_MUTEX_RECURSIVE_NP); \
  151. __pthread_mutex_init (&(NAME), &__attr); \
  152. __pthread_mutexattr_destroy (&__attr); \
  153. } \
  154. } while (0);
  155. #endif
  156. #define __rtld_lock_init_recursive(NAME) \
  157. __libc_lock_init_recursive (NAME)
  158. /* Finalize the named lock variable, which must be locked. It cannot be
  159. used again until __libc_lock_init is called again on it. This must be
  160. called on a lock variable before the containing storage is reused. */
  161. #define __libc_lock_fini(NAME) \
  162. (__libc_maybe_call2 (pthread_mutex_destroy, (&(NAME)), 0));
  163. #define __libc_rwlock_fini(NAME) \
  164. (__libc_maybe_call (__pthread_rwlock_destroy, (&(NAME)), 0));
  165. /* Finalize recursive named lock. */
  166. #define __libc_lock_fini_recursive(NAME) __libc_lock_fini ((NAME).mutex)
  167. #define __rtld_lock_fini_recursive(NAME) __libc_lock_fini_recursive (NAME)
  168. /* Lock the named lock variable. */
  169. #define __libc_lock_lock(NAME) \
  170. (__libc_maybe_call2 (pthread_mutex_lock, (&(NAME)), 0));
  171. #define __libc_rwlock_rdlock(NAME) \
  172. (__libc_maybe_call (__pthread_rwlock_rdlock, (&(NAME)), 0));
  173. #define __libc_rwlock_wrlock(NAME) \
  174. (__libc_maybe_call (__pthread_rwlock_wrlock, (&(NAME)), 0));
  175. /* Lock the recursive named lock variable. */
  176. #define __libc_lock_lock_recursive(NAME) __libc_lock_lock ((NAME).mutex)
  177. /* Try to lock the named lock variable. */
  178. #define __libc_lock_trylock(NAME) \
  179. (__libc_maybe_call2 (pthread_mutex_trylock, (&(NAME)), 0))
  180. #define __libc_rwlock_tryrdlock(NAME) \
  181. (__libc_maybe_call (__pthread_rwlock_tryrdlock, (&(NAME)), 0))
  182. #define __libc_rwlock_trywrlock(NAME) \
  183. (__libc_maybe_call (__pthread_rwlock_trywrlock, (&(NAME)), 0))
  184. /* Try to lock the recursive named lock variable. */
  185. #define __libc_lock_trylock_recursive(NAME) __libc_lock_trylock ((NAME).mutex)
  186. #define __rtld_lock_trylock_recursive(NAME) \
  187. __libc_lock_trylock_recursive (NAME)
  188. /* Unlock the named lock variable. */
  189. #define __libc_lock_unlock(NAME) \
  190. (__libc_maybe_call2 (pthread_mutex_unlock, (&(NAME)), 0));
  191. #define __libc_rwlock_unlock(NAME) \
  192. (__libc_maybe_call (__pthread_rwlock_unlock, (&(NAME)), 0));
  193. /* Unlock the recursive named lock variable. */
  194. #define __libc_lock_unlock_recursive(NAME) __libc_lock_unlock ((NAME).mutex)
  195. /* Define once control variable. */
  196. #if PTHREAD_ONCE_INIT == 0
  197. /* Special case for static variables where we can avoid the initialization
  198. if it is zero. */
  199. # define __libc_once_define(CLASS, NAME) \
  200. CLASS pthread_once_t NAME
  201. #else
  202. # define __libc_once_define(CLASS, NAME) \
  203. CLASS pthread_once_t NAME = PTHREAD_ONCE_INIT
  204. #endif
  205. /* Call handler iff the first call. */
  206. #define __libc_once(ONCE_CONTROL, INIT_FUNCTION) \
  207. do { \
  208. if (__pthread_once != NULL) \
  209. __pthread_once (&(ONCE_CONTROL), (INIT_FUNCTION)); \
  210. else if ((ONCE_CONTROL) == PTHREAD_ONCE_INIT) { \
  211. INIT_FUNCTION (); \
  212. (ONCE_CONTROL) = !PTHREAD_ONCE_INIT; \
  213. } \
  214. } while (0)
  215. /* Start critical region with cleanup. */
  216. #define __libc_cleanup_region_start(DOIT, FCT, ARG) \
  217. { struct _pthread_cleanup_buffer _buffer; \
  218. int _avail = (DOIT) && _pthread_cleanup_push_defer != NULL; \
  219. if (_avail) { \
  220. _pthread_cleanup_push_defer (&_buffer, (FCT), (ARG)); \
  221. }
  222. /* End critical region with cleanup. */
  223. #define __libc_cleanup_region_end(DOIT) \
  224. if (_avail) { \
  225. _pthread_cleanup_pop_restore (&_buffer, (DOIT)); \
  226. } \
  227. }
  228. /* Sometimes we have to exit the block in the middle. */
  229. #define __libc_cleanup_end(DOIT) \
  230. if (_avail) { \
  231. _pthread_cleanup_pop_restore (&_buffer, (DOIT)); \
  232. }
  233. #if 0
  234. #define __libc_cleanup_push(fct, arg) \
  235. { struct _pthread_cleanup_buffer _buffer; \
  236. __libc_maybe_call (_pthread_cleanup_push, (&_buffer, (fct), (arg)), 0)
  237. #define __libc_cleanup_pop(execute) \
  238. __libc_maybe_call (_pthread_cleanup_pop, (&_buffer, execute), 0); \
  239. }
  240. #endif
  241. /* Create thread-specific key. */
  242. #define __libc_key_create(KEY, DESTRUCTOR) \
  243. (__libc_maybe_call (__pthread_key_create, (KEY, DESTRUCTOR), 1))
  244. /* Get thread-specific data. */
  245. #define __libc_getspecific(KEY) \
  246. (__libc_maybe_call (__pthread_getspecific, (KEY), NULL))
  247. /* Set thread-specific data. */
  248. #define __libc_setspecific(KEY, VALUE) \
  249. (__libc_maybe_call (__pthread_setspecific, (KEY, VALUE), 0))
  250. /* Register handlers to execute before and after `fork'. */
  251. #define __libc_atfork(PREPARE, PARENT, CHILD) \
  252. (__libc_maybe_call (__pthread_atfork, (PREPARE, PARENT, CHILD), 0))
  253. /* Functions that are used by this file and are internal to the GNU C
  254. library. */
  255. extern int __pthread_mutex_init (pthread_mutex_t *__mutex,
  256. const pthread_mutexattr_t *__mutex_attr);
  257. extern int __pthread_mutex_destroy (pthread_mutex_t *__mutex);
  258. extern int __pthread_mutex_trylock (pthread_mutex_t *__mutex);
  259. extern int __pthread_mutex_lock (pthread_mutex_t *__mutex);
  260. extern int __pthread_mutex_unlock (pthread_mutex_t *__mutex);
  261. extern int __pthread_mutexattr_init (pthread_mutexattr_t *__attr);
  262. extern int __pthread_mutexattr_destroy (pthread_mutexattr_t *__attr);
  263. extern int __pthread_mutexattr_settype (pthread_mutexattr_t *__attr,
  264. int __kind);
  265. #ifdef __USE_UNIX98
  266. extern int __pthread_rwlock_init (pthread_rwlock_t *__rwlock,
  267. const pthread_rwlockattr_t *__attr);
  268. extern int __pthread_rwlock_destroy (pthread_rwlock_t *__rwlock);
  269. extern int __pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock);
  270. extern int __pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock);
  271. extern int __pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock);
  272. extern int __pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock);
  273. extern int __pthread_rwlock_unlock (pthread_rwlock_t *__rwlock);
  274. #endif
  275. extern int __pthread_key_create (pthread_key_t *__key,
  276. void (*__destr_function) (void *));
  277. extern int __pthread_setspecific (pthread_key_t __key,
  278. const void *__pointer);
  279. extern void *__pthread_getspecific (pthread_key_t __key);
  280. extern int __pthread_once (pthread_once_t *__once_control,
  281. void (*__init_routine) (void));
  282. extern int __pthread_atfork (void (*__prepare) (void),
  283. void (*__parent) (void),
  284. void (*__child) (void));
  285. /* We need portable names for some functions. E.g., when they are
  286. used as argument to __libc_cleanup_region_start. */
  287. #define __libc_mutex_unlock __pthread_mutex_unlock
  288. #endif /* bits/libc-lock.h */