libc-lock.h 12 KB

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