libc-lock.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* libc-internal interface for mutex locks. LinuxThreads version.
  2. Copyright (C) 1996, 1997, 1998, 1999 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. #else
  23. typedef struct __libc_lock_opaque__ __libc_lock_t;
  24. #endif
  25. /* Type for key to thread-specific data. */
  26. typedef pthread_key_t __libc_key_t;
  27. /* Define a lock variable NAME with storage class CLASS. The lock must be
  28. initialized with __libc_lock_init before it can be used (or define it
  29. with __libc_lock_define_initialized, below). Use `extern' for CLASS to
  30. declare a lock defined in another module. In public structure
  31. definitions you must use a pointer to the lock structure (i.e., NAME
  32. begins with a `*'), because its storage size will not be known outside
  33. of libc. */
  34. #define __libc_lock_define(CLASS,NAME) \
  35. CLASS __libc_lock_t NAME;
  36. /* Define an initialized lock variable NAME with storage class CLASS.
  37. For the C library we take a deeper look at the initializer. For this
  38. implementation all fields are initialized to zero. Therefore we
  39. don't initialize the variable which allows putting it into the BSS
  40. section. */
  41. #define __libc_lock_define_initialized(CLASS,NAME) \
  42. CLASS __libc_lock_t NAME;
  43. /* Define an initialized recursive lock variable NAME with storage
  44. class CLASS. */
  45. #define __libc_lock_define_initialized_recursive(CLASS,NAME) \
  46. CLASS __libc_lock_t NAME = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
  47. /* Initialize the named lock variable, leaving it in a consistent, unlocked
  48. state. */
  49. #define __libc_lock_init(NAME) \
  50. (__pthread_mutex_init != NULL ? __pthread_mutex_init (&(NAME), NULL) : 0);
  51. /* Same as last but this time we initialize a recursive mutex. */
  52. #define __libc_lock_init_recursive(NAME) \
  53. do { \
  54. if (__pthread_mutex_init != NULL) \
  55. { \
  56. pthread_mutexattr_t __attr; \
  57. __pthread_mutexattr_init (&__attr); \
  58. __pthread_mutexattr_settype (&__attr, PTHREAD_MUTEX_RECURSIVE_NP); \
  59. __pthread_mutex_init (&(NAME), &__attr); \
  60. __pthread_mutexattr_destroy (&__attr); \
  61. } \
  62. } while (0);
  63. /* Finalize the named lock variable, which must be locked. It cannot be
  64. used again until __libc_lock_init is called again on it. This must be
  65. called on a lock variable before the containing storage is reused. */
  66. #define __libc_lock_fini(NAME) \
  67. (__pthread_mutex_destroy != NULL ? __pthread_mutex_destroy (&(NAME)) : 0);
  68. /* Finalize recursive named lock. */
  69. #define __libc_lock_fini_recursive(NAME) __libc_lock_fini (NAME)
  70. /* Lock the named lock variable. */
  71. #define __libc_lock_lock(NAME) \
  72. (__pthread_mutex_lock != NULL ? __pthread_mutex_lock (&(NAME)) : 0);
  73. /* Lock the recursive named lock variable. */
  74. #define __libc_lock_lock_recursive(NAME) __libc_lock_lock (NAME)
  75. /* Try to lock the named lock variable. */
  76. #define __libc_lock_trylock(NAME) \
  77. (__pthread_mutex_trylock != NULL ? __pthread_mutex_trylock (&(NAME)) : 0)
  78. /* Try to lock the recursive named lock variable. */
  79. #define __libc_lock_trylock_recursive(NAME) __libc_lock_trylock (NAME)
  80. /* Unlock the named lock variable. */
  81. #define __libc_lock_unlock(NAME) \
  82. (__pthread_mutex_unlock != NULL ? __pthread_mutex_unlock (&(NAME)) : 0);
  83. /* Unlock the recursive named lock variable. */
  84. #define __libc_lock_unlock_recursive(NAME) __libc_lock_unlock (NAME)
  85. /* Define once control variable. */
  86. #if PTHREAD_ONCE_INIT == 0
  87. /* Special case for static variables where we can avoid the initialization
  88. if it is zero. */
  89. # define __libc_once_define(CLASS, NAME) \
  90. CLASS pthread_once_t NAME
  91. #else
  92. # define __libc_once_define(CLASS, NAME) \
  93. CLASS pthread_once_t NAME = PTHREAD_ONCE_INIT
  94. #endif
  95. /* Call handler iff the first call. */
  96. #define __libc_once(ONCE_CONTROL, INIT_FUNCTION) \
  97. do { \
  98. if (__pthread_once != NULL) \
  99. __pthread_once (&(ONCE_CONTROL), (INIT_FUNCTION)); \
  100. else if ((ONCE_CONTROL) == 0) { \
  101. INIT_FUNCTION (); \
  102. (ONCE_CONTROL) = 1; \
  103. } \
  104. } while (0)
  105. /* Start critical region with cleanup. */
  106. #define __libc_cleanup_region_start(FCT, ARG) \
  107. { struct _pthread_cleanup_buffer _buffer; \
  108. int _avail = _pthread_cleanup_push_defer != NULL; \
  109. if (_avail) { \
  110. _pthread_cleanup_push_defer (&_buffer, (FCT), (ARG)); \
  111. }
  112. /* End critical region with cleanup. */
  113. #define __libc_cleanup_region_end(DOIT) \
  114. if (_avail) { \
  115. _pthread_cleanup_pop_restore (&_buffer, (DOIT)); \
  116. } \
  117. }
  118. /* Sometimes we have to exit the block in the middle. */
  119. #define __libc_cleanup_end(DOIT) \
  120. if (_avail) { \
  121. _pthread_cleanup_pop_restore (&_buffer, (DOIT)); \
  122. }
  123. /* Create thread-specific key. */
  124. #define __libc_key_create(KEY, DESTRUCTOR) \
  125. (__pthread_key_create != NULL ? __pthread_key_create (KEY, DESTRUCTOR) : 1)
  126. /* Get thread-specific data. */
  127. #define __libc_getspecific(KEY) \
  128. (__pthread_getspecific != NULL ? __pthread_getspecific (KEY) : NULL)
  129. /* Set thread-specific data. */
  130. #define __libc_setspecific(KEY, VALUE) \
  131. (__pthread_setspecific != NULL ? __pthread_setspecific (KEY, VALUE) : 0)
  132. /* Register handlers to execute before and after `fork'. */
  133. #define __libc_atfork(PREPARE, PARENT, CHILD) \
  134. (__pthread_atfork != NULL ? __pthread_atfork (PREPARE, PARENT, CHILD) : 0)
  135. /* Make the pthread functions weak so that we can elide them from
  136. single-threaded processes. */
  137. #ifndef __NO_WEAK_PTHREAD_ALIASES
  138. # ifdef weak_extern
  139. weak_extern (__pthread_mutex_init)
  140. weak_extern (__pthread_mutex_destroy)
  141. weak_extern (__pthread_mutex_lock)
  142. weak_extern (__pthread_mutex_trylock)
  143. weak_extern (__pthread_mutex_unlock)
  144. weak_extern (__pthread_mutexattr_init)
  145. weak_extern (__pthread_mutexattr_destroy)
  146. weak_extern (__pthread_mutexattr_settype)
  147. weak_extern (__pthread_key_create)
  148. weak_extern (__pthread_setspecific)
  149. weak_extern (__pthread_getspecific)
  150. weak_extern (__pthread_once)
  151. weak_extern (__pthread_initialize)
  152. weak_extern (__pthread_atfork)
  153. weak_extern (_pthread_cleanup_push_defer)
  154. weak_extern (_pthread_cleanup_pop_restore)
  155. # else
  156. # pragma weak __pthread_mutex_init
  157. # pragma weak __pthread_mutex_destroy
  158. # pragma weak __pthread_mutex_lock
  159. # pragma weak __pthread_mutex_trylock
  160. # pragma weak __pthread_mutex_unlock
  161. # pragma weak __pthread_mutexattr_init
  162. # pragma weak __pthread_mutexattr_destroy
  163. # pragma weak __pthread_mutexattr_settype
  164. # pragma weak __pthread_key_create
  165. # pragma weak __pthread_setspecific
  166. # pragma weak __pthread_getspecific
  167. # pragma weak __pthread_once
  168. # pragma weak __pthread_initialize
  169. # pragma weak __pthread_atfork
  170. # pragma weak _pthread_cleanup_push_defer
  171. # pragma weak _pthread_cleanup_pop_restore
  172. # endif
  173. #endif
  174. /* We need portable names for some functions. E.g., when they are
  175. used as argument to __libc_cleanup_region_start. */
  176. #define __libc_mutex_unlock __pthread_mutex_unlock
  177. #endif /* bits/libc-lock.h */