libc-lockP.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /* Private libc-internal interface for mutex locks. NPTL version.
  2. Copyright (C) 1996-2026 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
  14. not, see <https://www.gnu.org/licenses/>. */
  15. #ifndef _LIBC_LOCKP_H
  16. #define _LIBC_LOCKP_H 1
  17. #include <pthread.h>
  18. #define __need_NULL
  19. #include <stddef.h>
  20. /* Fortunately Linux now has a mean to do locking which is realtime
  21. safe without the aid of the thread library. We also need no fancy
  22. options like error checking mutexes etc. We only need simple
  23. locks, maybe recursive. This can be easily and cheaply implemented
  24. using futexes. We will use them everywhere except in ld.so since
  25. ld.so might be used on old kernels with a different libc.so. */
  26. #include <lowlevellock.h>
  27. #include <tls.h>
  28. #include <libc-lock-arch.h>
  29. /* Mutex type. */
  30. typedef int __libc_lock_t __LIBC_LOCK_ALIGNMENT;
  31. typedef struct { pthread_mutex_t mutex; } __rtld_lock_recursive_t;
  32. typedef pthread_rwlock_t __libc_rwlock_t;
  33. /* Define a lock variable NAME with storage class CLASS. The lock must be
  34. initialized with __libc_lock_init before it can be used (or define it
  35. with __libc_lock_define_initialized, below). Use `extern' for CLASS to
  36. declare a lock defined in another module. In public structure
  37. definitions you must use a pointer to the lock structure (i.e., NAME
  38. begins with a `*'), because its storage size will not be known outside
  39. of libc. */
  40. #define __libc_lock_define(CLASS,NAME) \
  41. CLASS __libc_lock_t NAME;
  42. #define __libc_rwlock_define(CLASS,NAME) \
  43. CLASS __libc_rwlock_t NAME;
  44. #define __rtld_lock_define_recursive(CLASS,NAME) \
  45. CLASS __rtld_lock_recursive_t NAME;
  46. /* Define an initialized lock variable NAME with storage class CLASS.
  47. For the C library we take a deeper look at the initializer. For
  48. this implementation all fields are initialized to zero. Therefore
  49. we don't initialize the variable which allows putting it into the
  50. BSS section. */
  51. _Static_assert (LLL_LOCK_INITIALIZER == 0, "LLL_LOCK_INITIALIZER != 0");
  52. #define _LIBC_LOCK_INITIALIZER LLL_LOCK_INITIALIZER
  53. #define __libc_lock_define_initialized(CLASS,NAME) \
  54. CLASS __libc_lock_t NAME;
  55. #define __libc_rwlock_define_initialized(CLASS,NAME) \
  56. CLASS __libc_rwlock_t NAME = PTHREAD_RWLOCK_INITIALIZER;
  57. #define __rtld_lock_define_initialized_recursive(CLASS,NAME) \
  58. CLASS __rtld_lock_recursive_t NAME = _RTLD_LOCK_RECURSIVE_INITIALIZER;
  59. #define _RTLD_LOCK_RECURSIVE_INITIALIZER \
  60. {PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP}
  61. #define __rtld_lock_initialize(NAME) \
  62. (void) ((NAME) = (__rtld_lock_recursive_t) _RTLD_LOCK_RECURSIVE_INITIALIZER)
  63. /* If we check for a weakly referenced symbol and then perform a
  64. normal jump to it te code generated for some platforms in case of
  65. PIC is unnecessarily slow. What would happen is that the function
  66. is first referenced as data and then it is called indirectly
  67. through the PLT. We can make this a direct jump. */
  68. #ifdef __PIC__
  69. # define __libc_maybe_call(FUNC, ARGS, ELSE) \
  70. (__extension__ ({ __typeof (FUNC) *_fn = (FUNC); \
  71. _fn != NULL ? (*_fn) ARGS : ELSE; }))
  72. #else
  73. # define __libc_maybe_call(FUNC, ARGS, ELSE) \
  74. (FUNC != NULL ? FUNC ARGS : ELSE)
  75. #endif
  76. /* Initialize the named lock variable, leaving it in a consistent, unlocked
  77. state. */
  78. #define __libc_lock_init(NAME) ((void) ((NAME) = LLL_LOCK_INITIALIZER))
  79. #define __libc_rwlock_init(NAME) __pthread_rwlock_init (&(NAME), NULL)
  80. /* Finalize the named lock variable, which must be locked. It cannot be
  81. used again until __libc_lock_init is called again on it. This must be
  82. called on a lock variable before the containing storage is reused. */
  83. #define __libc_lock_fini(NAME) ((void) 0)
  84. #define __libc_rwlock_fini(NAME) ((void) 0)
  85. /* Lock the named lock variable. */
  86. #define __libc_lock_lock(NAME) ({ lll_lock (NAME, LLL_PRIVATE); 0; })
  87. #define __libc_rwlock_rdlock(NAME) __pthread_rwlock_rdlock (&(NAME))
  88. #define __libc_rwlock_wrlock(NAME) __pthread_rwlock_wrlock (&(NAME))
  89. /* Try to lock the named lock variable. */
  90. #define __libc_lock_trylock(NAME) lll_trylock (NAME)
  91. /* Unlock the named lock variable. */
  92. #define __libc_lock_unlock(NAME) lll_unlock (NAME, LLL_PRIVATE)
  93. #define __libc_rwlock_unlock(NAME) __pthread_rwlock_unlock (&(NAME))
  94. #if IS_IN (rtld)
  95. # define __rtld_lock_lock_recursive(NAME) \
  96. ___rtld_mutex_lock (&(NAME).mutex)
  97. # define __rtld_lock_unlock_recursive(NAME) \
  98. ___rtld_mutex_unlock (&(NAME).mutex)
  99. #else /* Not in the dynamic loader. */
  100. # define __rtld_lock_lock_recursive(NAME) \
  101. __pthread_mutex_lock (&(NAME).mutex)
  102. # define __rtld_lock_unlock_recursive(NAME) \
  103. __pthread_mutex_unlock (&(NAME).mutex)
  104. #endif
  105. /* Define once control variable. */
  106. #if PTHREAD_ONCE_INIT == 0
  107. /* Special case for static variables where we can avoid the initialization
  108. if it is zero. */
  109. # define __libc_once_define(CLASS, NAME) \
  110. CLASS pthread_once_t NAME
  111. #else
  112. # define __libc_once_define(CLASS, NAME) \
  113. CLASS pthread_once_t NAME = PTHREAD_ONCE_INIT
  114. #endif
  115. /* Call handler iff the first call. Use a local call in libc, but the
  116. global pthread_once symbol elsewhere. */
  117. #if IS_IN (libc)
  118. # define __libc_once(ONCE_CONTROL, INIT_FUNCTION) \
  119. __pthread_once (&(ONCE_CONTROL), INIT_FUNCTION)
  120. #else
  121. # define __libc_once(ONCE_CONTROL, INIT_FUNCTION) \
  122. pthread_once (&(ONCE_CONTROL), INIT_FUNCTION)
  123. #endif
  124. /* Get once control variable. */
  125. #define __libc_once_get(ONCE_CONTROL) ((ONCE_CONTROL) != PTHREAD_ONCE_INIT)
  126. /* __libc_cleanup_push and __libc_cleanup_pop depend on exception
  127. handling and stack unwinding. */
  128. #ifdef __EXCEPTIONS
  129. /* Normal cleanup handling, based on C cleanup attribute. */
  130. static __always_inline void
  131. __libc_cleanup_routine (struct __pthread_cleanup_frame *f)
  132. {
  133. if (f->__do_it)
  134. f->__cancel_routine (f->__cancel_arg);
  135. }
  136. # define __libc_cleanup_push(fct, arg) \
  137. do { \
  138. struct __pthread_cleanup_frame __clframe \
  139. __attribute__ ((__cleanup__ (__libc_cleanup_routine))) \
  140. = { .__cancel_routine = (fct), .__cancel_arg = (arg), \
  141. .__do_it = 1 };
  142. # define __libc_cleanup_pop(execute) \
  143. __clframe.__do_it = (execute); \
  144. } while (0)
  145. #endif /* __EXCEPTIONS */
  146. /* Register handlers to execute before and after `fork'. Note that the
  147. last parameter is NULL. The handlers registered by the libc are
  148. never removed so this is OK. */
  149. extern int __register_atfork (void (*__prepare) (void),
  150. void (*__parent) (void),
  151. void (*__child) (void),
  152. void *__dso_handle);
  153. /* Functions that are used by this file and are internal to the GNU C
  154. library. */
  155. extern int __pthread_mutex_init (pthread_mutex_t *__mutex,
  156. const pthread_mutexattr_t *__mutex_attr);
  157. libc_hidden_proto (__pthread_mutex_init)
  158. extern int __pthread_mutex_destroy (pthread_mutex_t *__mutex);
  159. libc_hidden_proto (__pthread_mutex_destroy)
  160. extern int __pthread_mutex_trylock (pthread_mutex_t *__mutex);
  161. extern int __pthread_mutex_lock (pthread_mutex_t *__mutex);
  162. libc_hidden_proto (__pthread_mutex_lock)
  163. extern int __pthread_mutex_unlock (pthread_mutex_t *__mutex);
  164. libc_hidden_proto (__pthread_mutex_unlock)
  165. extern int __pthread_mutexattr_destroy (pthread_mutexattr_t *__attr);
  166. extern int __pthread_rwlock_init (pthread_rwlock_t *__rwlock,
  167. const pthread_rwlockattr_t *__attr);
  168. libc_hidden_proto (__pthread_rwlock_init)
  169. extern int __pthread_rwlock_destroy (pthread_rwlock_t *__rwlock);
  170. extern int __pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock);
  171. libc_hidden_proto (__pthread_rwlock_rdlock)
  172. extern int __pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock);
  173. libc_hidden_proto (__pthread_rwlock_wrlock)
  174. extern int __pthread_rwlock_unlock (pthread_rwlock_t *__rwlock);
  175. libc_hidden_proto (__pthread_rwlock_unlock)
  176. extern int __pthread_once (pthread_once_t *__once_control,
  177. void (*__init_routine) (void));
  178. libc_hidden_proto (__pthread_once)
  179. extern int __pthread_atfork (void (*__prepare) (void),
  180. void (*__parent) (void),
  181. void (*__child) (void));
  182. extern int __pthread_setcancelstate (int state, int *oldstate);
  183. libc_hidden_proto (__pthread_setcancelstate)
  184. /* Make the pthread functions weak so that we can elide them from
  185. single-threaded processes. */
  186. #ifndef __NO_WEAK_PTHREAD_ALIASES
  187. # ifdef weak_extern
  188. weak_extern (__pthread_mutex_trylock)
  189. weak_extern (__pthread_mutexattr_destroy)
  190. weak_extern (__pthread_initialize)
  191. weak_extern (__pthread_atfork)
  192. # else
  193. # pragma weak __pthread_mutex_trylock
  194. # pragma weak __pthread_mutexattr_destroy
  195. # pragma weak __pthread_initialize
  196. # pragma weak __pthread_atfork
  197. # endif
  198. #endif
  199. #endif /* libc-lockP.h */