libc-lock.h 12 KB

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