libc-lock.h 12 KB

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