libc-lock.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 not,
  15. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. Boston, MA 02111-1307, USA. */
  17. #ifndef _BITS_LIBC_LOCK_H
  18. #define _BITS_LIBC_LOCK_H 1
  19. #include <pthread.h>
  20. #if defined _LIBC && !defined NOT_IN_libc
  21. #include <linuxthreads/internals.h>
  22. #endif
  23. /* Mutex type. */
  24. #if defined(_LIBC) || defined(_IO_MTSAFE_IO)
  25. typedef pthread_mutex_t __libc_lock_t;
  26. typedef struct { pthread_mutex_t mutex; } __libc_lock_recursive_t;
  27. # ifdef __USE_UNIX98
  28. typedef pthread_rwlock_t __libc_rwlock_t;
  29. # else
  30. typedef struct __libc_rwlock_opaque__ __libc_rwlock_t;
  31. # endif
  32. typedef __libc_lock_recursive_t __rtld_lock_recursive_t;
  33. #else
  34. typedef struct __libc_lock_opaque__ __libc_lock_t;
  35. typedef struct __libc_lock_recursive_opaque__ __libc_lock_recursive_t;
  36. typedef struct __libc_rwlock_opaque__ __libc_rwlock_t;
  37. #endif
  38. /* Type for key to thread-specific data. */
  39. typedef pthread_key_t __libc_key_t;
  40. /* Define a lock variable NAME with storage class CLASS. The lock must be
  41. initialized with __libc_lock_init before it can be used (or define it
  42. with __libc_lock_define_initialized, below). Use `extern' for CLASS to
  43. declare a lock defined in another module. In public structure
  44. definitions you must use a pointer to the lock structure (i.e., NAME
  45. begins with a `*'), because its storage size will not be known outside
  46. of libc. */
  47. #define __libc_lock_define(CLASS,NAME) \
  48. CLASS __libc_lock_t NAME;
  49. #define __libc_rwlock_define(CLASS,NAME) \
  50. CLASS __libc_rwlock_t NAME;
  51. #define __libc_lock_define_recursive(CLASS,NAME) \
  52. CLASS __libc_lock_recursive_t NAME;
  53. #define __rtld_lock_define_recursive(CLASS,NAME) \
  54. CLASS __rtld_lock_recursive_t NAME;
  55. /* Define an initialized lock variable NAME with storage class CLASS.
  56. For the C library we take a deeper look at the initializer. For
  57. this implementation all fields are initialized to zero. Therefore
  58. we don't initialize the variable which allows putting it into the
  59. BSS section. (Except on PA-RISC and other odd architectures, where
  60. initialized locks must be set to one due to the lack of normal
  61. atomic operations.) */
  62. #if __LT_SPINLOCK_INIT == 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 = PTHREAD_MUTEX_INITIALIZER;
  68. #endif
  69. #define __libc_rwlock_define_initialized(CLASS,NAME) \
  70. CLASS __libc_rwlock_t NAME = PTHREAD_RWLOCK_INITIALIZER;
  71. /* Define an initialized recursive lock variable NAME with storage
  72. class CLASS. */
  73. #define __libc_lock_define_initialized_recursive(CLASS,NAME) \
  74. CLASS __libc_lock_recursive_t NAME = _LIBC_LOCK_RECURSIVE_INITIALIZER;
  75. #define _LIBC_LOCK_RECURSIVE_INITIALIZER \
  76. {PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP}
  77. #define __rtld_lock_define_initialized_recursive(CLASS,NAME) \
  78. CLASS __rtld_lock_recursive_t NAME = _RTLD_LOCK_RECURSIVE_INITIALIZER;
  79. #define _RTLD_LOCK_RECURSIVE_INITIALIZER \
  80. {PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP}
  81. #if defined _LIBC && defined IS_IN_libpthread
  82. # define __libc_maybe_call(FUNC, ARGS, ELSE) FUNC ARGS
  83. #else
  84. # if defined __PIC__ || (defined _LIBC && defined SHARED)
  85. # define __libc_maybe_call(FUNC, ARGS, ELSE) \
  86. (__extension__ ({ __typeof (FUNC) *_fn = (FUNC); \
  87. _fn != NULL ? (*_fn) ARGS : ELSE; }))
  88. # else
  89. # define __libc_maybe_call(FUNC, ARGS, ELSE) \
  90. (FUNC != NULL ? FUNC ARGS : ELSE)
  91. # endif
  92. #endif
  93. #if defined _LIBC && !defined NOT_IN_libc && defined SHARED
  94. # define __libc_maybe_call2(FUNC, ARGS, ELSE) \
  95. ({__builtin_expect (__libc_pthread_functions.ptr_##FUNC != NULL, 0) \
  96. ? __libc_pthread_functions.ptr_##FUNC ARGS : ELSE; })
  97. #else
  98. # define __libc_maybe_call2(FUNC, ARGS, ELSE) __libc_maybe_call (__##FUNC, ARGS, ELSE)
  99. #endif
  100. /* Initialize the named lock variable, leaving it in a consistent, unlocked
  101. state. */
  102. #if defined _LIBC && !defined NOT_IN_libc && defined SHARED
  103. #define __libc_lock_init(NAME) \
  104. ({ \
  105. (NAME).__m_count = 0; \
  106. (NAME).__m_owner = NULL; \
  107. (NAME).__m_kind = PTHREAD_MUTEX_TIMED_NP; \
  108. (NAME).__m_lock.__status = 0; \
  109. (NAME).__m_lock.__spinlock = __LT_SPINLOCK_INIT; \
  110. 0; })
  111. #else
  112. #define __libc_lock_init(NAME) \
  113. (__libc_maybe_call2 (pthread_mutex_init, (&(NAME), NULL), 0))
  114. #endif
  115. #define __libc_rwlock_init(NAME) \
  116. (__libc_maybe_call (__pthread_rwlock_init, (&(NAME), NULL), 0));
  117. /* Same as last but this time we initialize a recursive mutex. */
  118. #if defined _LIBC && !defined NOT_IN_libc && defined SHARED
  119. #define __libc_lock_init_recursive(NAME) \
  120. ({ \
  121. (NAME).mutex.__m_count = 0; \
  122. (NAME).mutex.__m_owner = NULL; \
  123. (NAME).mutex.__m_kind = PTHREAD_MUTEX_RECURSIVE_NP; \
  124. (NAME).mutex.__m_lock.__status = 0; \
  125. (NAME).mutex.__m_lock.__spinlock = __LT_SPINLOCK_INIT; \
  126. 0; })
  127. #else
  128. #define __libc_lock_init_recursive(NAME) \
  129. do { \
  130. if (__pthread_mutex_init != NULL) \
  131. { \
  132. pthread_mutexattr_t __attr; \
  133. __pthread_mutexattr_init (&__attr); \
  134. __pthread_mutexattr_settype (&__attr, PTHREAD_MUTEX_RECURSIVE_NP); \
  135. __pthread_mutex_init (&(NAME).mutex, &__attr); \
  136. __pthread_mutexattr_destroy (&__attr); \
  137. } \
  138. } while (0);
  139. #endif
  140. #define __rtld_lock_init_recursive(NAME) \
  141. __libc_lock_init_recursive (NAME)
  142. /* Finalize the named lock variable, which must be locked. It cannot be
  143. used again until __libc_lock_init is called again on it. This must be
  144. called on a lock variable before the containing storage is reused. */
  145. #define __libc_lock_fini(NAME) \
  146. (__libc_maybe_call2 (pthread_mutex_destroy, (&(NAME)), 0));
  147. #define __libc_rwlock_fini(NAME) \
  148. (__libc_maybe_call (__pthread_rwlock_destroy, (&(NAME)), 0));
  149. /* Finalize recursive named lock. */
  150. #define __libc_lock_fini_recursive(NAME) __libc_lock_fini ((NAME).mutex)
  151. #define __rtld_lock_fini_recursive(NAME) __libc_lock_fini_recursive (NAME)
  152. /* Lock the named lock variable. */
  153. #define __libc_lock_lock(NAME) \
  154. (__libc_maybe_call2 (pthread_mutex_lock, (&(NAME)), 0));
  155. #define __libc_rwlock_rdlock(NAME) \
  156. (__libc_maybe_call (__pthread_rwlock_rdlock, (&(NAME)), 0));
  157. #define __libc_rwlock_wrlock(NAME) \
  158. (__libc_maybe_call (__pthread_rwlock_wrlock, (&(NAME)), 0));
  159. /* Lock the recursive named lock variable. */
  160. #define __libc_lock_lock_recursive(NAME) __libc_lock_lock ((NAME).mutex)
  161. /* Try to lock the named lock variable. */
  162. #define __libc_lock_trylock(NAME) \
  163. (__libc_maybe_call2 (pthread_mutex_trylock, (&(NAME)), 0))
  164. #define __libc_rwlock_tryrdlock(NAME) \
  165. (__libc_maybe_call (__pthread_rwlock_tryrdlock, (&(NAME)), 0))
  166. #define __libc_rwlock_trywrlock(NAME) \
  167. (__libc_maybe_call (__pthread_rwlock_trywrlock, (&(NAME)), 0))
  168. /* Try to lock the recursive named lock variable. */
  169. #define __libc_lock_trylock_recursive(NAME) __libc_lock_trylock ((NAME).mutex)
  170. #define __rtld_lock_trylock_recursive(NAME) \
  171. __libc_lock_trylock_recursive (NAME)
  172. /* Unlock the named lock variable. */
  173. #define __libc_lock_unlock(NAME) \
  174. (__libc_maybe_call2 (pthread_mutex_unlock, (&(NAME)), 0));
  175. #define __libc_rwlock_unlock(NAME) \
  176. (__libc_maybe_call (__pthread_rwlock_unlock, (&(NAME)), 0));
  177. /* Unlock the recursive named lock variable. */
  178. #define __libc_lock_unlock_recursive(NAME) __libc_lock_unlock ((NAME).mutex)
  179. /* Define once control variable. */
  180. #if PTHREAD_ONCE_INIT == 0
  181. /* Special case for static variables where we can avoid the initialization
  182. if it is zero. */
  183. # define __libc_once_define(CLASS, NAME) \
  184. CLASS pthread_once_t NAME
  185. #else
  186. # define __libc_once_define(CLASS, NAME) \
  187. CLASS pthread_once_t NAME = PTHREAD_ONCE_INIT
  188. #endif
  189. /* Call handler iff the first call. */
  190. #define __libc_once(ONCE_CONTROL, INIT_FUNCTION) \
  191. do { \
  192. if (__pthread_once != NULL) \
  193. __pthread_once (&(ONCE_CONTROL), (INIT_FUNCTION)); \
  194. else if ((ONCE_CONTROL) == PTHREAD_ONCE_INIT) { \
  195. INIT_FUNCTION (); \
  196. (ONCE_CONTROL) = !PTHREAD_ONCE_INIT; \
  197. } \
  198. } while (0)
  199. /* Start critical region with cleanup. */
  200. #define __libc_cleanup_region_start(DOIT, FCT, ARG) \
  201. { struct _pthread_cleanup_buffer _buffer; \
  202. int _avail = (DOIT) && _pthread_cleanup_push_defer != NULL; \
  203. if (_avail) { \
  204. _pthread_cleanup_push_defer (&_buffer, (FCT), (ARG)); \
  205. }
  206. /* End critical region with cleanup. */
  207. #define __libc_cleanup_region_end(DOIT) \
  208. if (_avail) { \
  209. _pthread_cleanup_pop_restore (&_buffer, (DOIT)); \
  210. } \
  211. }
  212. /* Sometimes we have to exit the block in the middle. */
  213. #define __libc_cleanup_end(DOIT) \
  214. if (_avail) { \
  215. _pthread_cleanup_pop_restore (&_buffer, (DOIT)); \
  216. }
  217. #define __libc_cleanup_push(fct, arg) \
  218. { struct _pthread_cleanup_buffer _buffer; \
  219. __libc_maybe_call (_pthread_cleanup_push, (&_buffer, (fct), (arg)), 0)
  220. #define __libc_cleanup_pop(execute) \
  221. __libc_maybe_call (_pthread_cleanup_pop, (&_buffer, execute), 0); \
  222. }
  223. /* Create thread-specific key. */
  224. #define __libc_key_create(KEY, DESTRUCTOR) \
  225. (__libc_maybe_call (__pthread_key_create, (KEY, DESTRUCTOR), 1))
  226. /* Get thread-specific data. */
  227. #define __libc_getspecific(KEY) \
  228. (__libc_maybe_call (__pthread_getspecific, (KEY), NULL))
  229. /* Set thread-specific data. */
  230. #define __libc_setspecific(KEY, VALUE) \
  231. (__libc_maybe_call (__pthread_setspecific, (KEY, VALUE), 0))
  232. /* Register handlers to execute before and after `fork'. */
  233. #define __libc_atfork(PREPARE, PARENT, CHILD) \
  234. (__libc_maybe_call (__pthread_atfork, (PREPARE, PARENT, CHILD), 0))
  235. /* Functions that are used by this file and are internal to the GNU C
  236. library. */
  237. extern int __pthread_mutex_init (pthread_mutex_t *__mutex,
  238. __const pthread_mutexattr_t *__mutex_attr);
  239. extern int __pthread_mutex_destroy (pthread_mutex_t *__mutex);
  240. extern int __pthread_mutex_trylock (pthread_mutex_t *__mutex);
  241. extern int __pthread_mutex_lock (pthread_mutex_t *__mutex);
  242. extern int __pthread_mutex_unlock (pthread_mutex_t *__mutex);
  243. extern int __pthread_mutexattr_init (pthread_mutexattr_t *__attr);
  244. extern int __pthread_mutexattr_destroy (pthread_mutexattr_t *__attr);
  245. extern int __pthread_mutexattr_settype (pthread_mutexattr_t *__attr,
  246. int __kind);
  247. #ifdef __USE_UNIX98
  248. extern int __pthread_rwlock_init (pthread_rwlock_t *__rwlock,
  249. __const pthread_rwlockattr_t *__attr);
  250. extern int __pthread_rwlock_destroy (pthread_rwlock_t *__rwlock);
  251. extern int __pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock);
  252. extern int __pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock);
  253. extern int __pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock);
  254. extern int __pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock);
  255. extern int __pthread_rwlock_unlock (pthread_rwlock_t *__rwlock);
  256. #endif
  257. extern int __pthread_key_create (pthread_key_t *__key,
  258. void (*__destr_function) (void *));
  259. extern int __pthread_setspecific (pthread_key_t __key,
  260. __const void *__pointer);
  261. extern void *__pthread_getspecific (pthread_key_t __key);
  262. extern int __pthread_once (pthread_once_t *__once_control,
  263. void (*__init_routine) (void));
  264. extern int __pthread_atfork (void (*__prepare) (void),
  265. void (*__parent) (void),
  266. void (*__child) (void));
  267. /* Make the pthread functions weak so that we can elide them from
  268. single-threaded processes. */
  269. #ifndef __NO_WEAK_PTHREAD_ALIASES
  270. # ifdef weak_extern
  271. # define BP_SYM(sym) sym
  272. weak_extern (BP_SYM (__pthread_mutex_init))
  273. weak_extern (BP_SYM (__pthread_mutex_destroy))
  274. weak_extern (BP_SYM (__pthread_mutex_lock))
  275. weak_extern (BP_SYM (__pthread_mutex_trylock))
  276. weak_extern (BP_SYM (__pthread_mutex_unlock))
  277. weak_extern (BP_SYM (__pthread_mutexattr_init))
  278. weak_extern (BP_SYM (__pthread_mutexattr_destroy))
  279. weak_extern (BP_SYM (__pthread_mutexattr_settype))
  280. weak_extern (BP_SYM (__pthread_rwlock_init))
  281. weak_extern (BP_SYM (__pthread_rwlock_destroy))
  282. weak_extern (BP_SYM (__pthread_rwlock_rdlock))
  283. weak_extern (BP_SYM (__pthread_rwlock_tryrdlock))
  284. weak_extern (BP_SYM (__pthread_rwlock_wrlock))
  285. weak_extern (BP_SYM (__pthread_rwlock_trywrlock))
  286. weak_extern (BP_SYM (__pthread_rwlock_unlock))
  287. weak_extern (BP_SYM (__pthread_key_create))
  288. weak_extern (BP_SYM (__pthread_setspecific))
  289. weak_extern (BP_SYM (__pthread_getspecific))
  290. weak_extern (BP_SYM (__pthread_once))
  291. weak_extern (__pthread_initialize)
  292. weak_extern (__pthread_atfork)
  293. weak_extern (BP_SYM (_pthread_cleanup_push))
  294. weak_extern (BP_SYM (_pthread_cleanup_pop))
  295. weak_extern (BP_SYM (_pthread_cleanup_push_defer))
  296. weak_extern (BP_SYM (_pthread_cleanup_pop_restore))
  297. # else
  298. # pragma weak __pthread_mutex_init
  299. # pragma weak __pthread_mutex_destroy
  300. # pragma weak __pthread_mutex_lock
  301. # pragma weak __pthread_mutex_trylock
  302. # pragma weak __pthread_mutex_unlock
  303. # pragma weak __pthread_mutexattr_init
  304. # pragma weak __pthread_mutexattr_destroy
  305. # pragma weak __pthread_mutexattr_settype
  306. # pragma weak __pthread_rwlock_destroy
  307. # pragma weak __pthread_rwlock_rdlock
  308. # pragma weak __pthread_rwlock_tryrdlock
  309. # pragma weak __pthread_rwlock_wrlock
  310. # pragma weak __pthread_rwlock_trywrlock
  311. # pragma weak __pthread_rwlock_unlock
  312. # pragma weak __pthread_key_create
  313. # pragma weak __pthread_setspecific
  314. # pragma weak __pthread_getspecific
  315. # pragma weak __pthread_once
  316. # pragma weak __pthread_initialize
  317. # pragma weak __pthread_atfork
  318. # pragma weak _pthread_cleanup_push_defer
  319. # pragma weak _pthread_cleanup_pop_restore
  320. # pragma weak _pthread_cleanup_push
  321. # pragma weak _pthread_cleanup_pop
  322. # endif
  323. #endif
  324. /* We need portable names for some functions. E.g., when they are
  325. used as argument to __libc_cleanup_region_start. */
  326. #define __libc_mutex_unlock __pthread_mutex_unlock
  327. #endif /* bits/libc-lock.h */