libc-lockP.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /* Private libc-internal interface for mutex locks. NPTL version.
  2. Copyright (C) 1996-2019 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 <http://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 <pthread-functions.h>
  29. #if IS_IN (libpthread)
  30. /* This gets us the declarations of the __pthread_* internal names,
  31. and hidden_proto for them. */
  32. # include <nptl/pthreadP.h>
  33. #endif
  34. /* Mutex type. */
  35. #if !IS_IN (libc) && !IS_IN (libpthread)
  36. typedef pthread_mutex_t __libc_lock_t;
  37. #else
  38. typedef int __libc_lock_t;
  39. #endif
  40. typedef struct { pthread_mutex_t mutex; } __rtld_lock_recursive_t;
  41. typedef pthread_rwlock_t __libc_rwlock_t;
  42. /* Type for key to thread-specific data. */
  43. typedef pthread_key_t __libc_key_t;
  44. /* Define a lock variable NAME with storage class CLASS. The lock must be
  45. initialized with __libc_lock_init before it can be used (or define it
  46. with __libc_lock_define_initialized, below). Use `extern' for CLASS to
  47. declare a lock defined in another module. In public structure
  48. definitions you must use a pointer to the lock structure (i.e., NAME
  49. begins with a `*'), because its storage size will not be known outside
  50. of libc. */
  51. #define __libc_lock_define(CLASS,NAME) \
  52. CLASS __libc_lock_t NAME;
  53. #define __libc_rwlock_define(CLASS,NAME) \
  54. CLASS __libc_rwlock_t NAME;
  55. #define __rtld_lock_define_recursive(CLASS,NAME) \
  56. CLASS __rtld_lock_recursive_t NAME;
  57. /* Define an initialized lock variable NAME with storage class CLASS.
  58. For the C library we take a deeper look at the initializer. For
  59. this implementation all fields are initialized to zero. Therefore
  60. we don't initialize the variable which allows putting it into the
  61. BSS section. (Except on PA-RISC and other odd architectures, where
  62. initialized locks must be set to one due to the lack of normal
  63. atomic operations.) */
  64. #define _LIBC_LOCK_INITIALIZER LLL_LOCK_INITIALIZER
  65. #if IS_IN (libc) || IS_IN (libpthread)
  66. # if LLL_LOCK_INITIALIZER == 0
  67. # define __libc_lock_define_initialized(CLASS,NAME) \
  68. CLASS __libc_lock_t NAME;
  69. # else
  70. # define __libc_lock_define_initialized(CLASS,NAME) \
  71. CLASS __libc_lock_t NAME = LLL_LOCK_INITIALIZER;
  72. # endif
  73. #else
  74. # define __libc_lock_define_initialized(CLASS,NAME) \
  75. CLASS __libc_lock_t NAME;
  76. #endif
  77. #define __libc_rwlock_define_initialized(CLASS,NAME) \
  78. CLASS __libc_rwlock_t NAME = PTHREAD_RWLOCK_INITIALIZER;
  79. #define __rtld_lock_define_initialized_recursive(CLASS,NAME) \
  80. CLASS __rtld_lock_recursive_t NAME = _RTLD_LOCK_RECURSIVE_INITIALIZER;
  81. #define _RTLD_LOCK_RECURSIVE_INITIALIZER \
  82. {PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP}
  83. #define __rtld_lock_initialize(NAME) \
  84. (void) ((NAME) = (__rtld_lock_recursive_t) _RTLD_LOCK_RECURSIVE_INITIALIZER)
  85. /* If we check for a weakly referenced symbol and then perform a
  86. normal jump to it te code generated for some platforms in case of
  87. PIC is unnecessarily slow. What would happen is that the function
  88. is first referenced as data and then it is called indirectly
  89. through the PLT. We can make this a direct jump. */
  90. #ifdef __PIC__
  91. # define __libc_maybe_call(FUNC, ARGS, ELSE) \
  92. (__extension__ ({ __typeof (FUNC) *_fn = (FUNC); \
  93. _fn != NULL ? (*_fn) ARGS : ELSE; }))
  94. #else
  95. # define __libc_maybe_call(FUNC, ARGS, ELSE) \
  96. (FUNC != NULL ? FUNC ARGS : ELSE)
  97. #endif
  98. /* Call thread functions through the function pointer table. */
  99. #if defined SHARED && IS_IN (libc)
  100. # define PTFAVAIL(NAME) __libc_pthread_functions_init
  101. # define __libc_ptf_call(FUNC, ARGS, ELSE) \
  102. (__libc_pthread_functions_init ? PTHFCT_CALL (ptr_##FUNC, ARGS) : ELSE)
  103. # define __libc_ptf_call_always(FUNC, ARGS) \
  104. PTHFCT_CALL (ptr_##FUNC, ARGS)
  105. #elif IS_IN (libpthread)
  106. # define PTFAVAIL(NAME) 1
  107. # define __libc_ptf_call(FUNC, ARGS, ELSE) \
  108. FUNC ARGS
  109. # define __libc_ptf_call_always(FUNC, ARGS) \
  110. FUNC ARGS
  111. #else
  112. # define PTFAVAIL(NAME) (NAME != NULL)
  113. # define __libc_ptf_call(FUNC, ARGS, ELSE) \
  114. __libc_maybe_call (FUNC, ARGS, ELSE)
  115. # define __libc_ptf_call_always(FUNC, ARGS) \
  116. FUNC ARGS
  117. #endif
  118. /* Initialize the named lock variable, leaving it in a consistent, unlocked
  119. state. */
  120. #if IS_IN (libc) || IS_IN (libpthread)
  121. # define __libc_lock_init(NAME) \
  122. ((void) ((NAME) = LLL_LOCK_INITIALIZER))
  123. #else
  124. # define __libc_lock_init(NAME) \
  125. __libc_maybe_call (__pthread_mutex_init, (&(NAME), NULL), 0)
  126. #endif
  127. #if defined SHARED && IS_IN (libc)
  128. /* ((NAME) = (__libc_rwlock_t) PTHREAD_RWLOCK_INITIALIZER) is inefficient. */
  129. # define __libc_rwlock_init(NAME) \
  130. ((void) __builtin_memset (&(NAME), '\0', sizeof (NAME)))
  131. #else
  132. # define __libc_rwlock_init(NAME) \
  133. __libc_maybe_call (__pthread_rwlock_init, (&(NAME), NULL), 0)
  134. #endif
  135. /* Finalize the named lock variable, which must be locked. It cannot be
  136. used again until __libc_lock_init is called again on it. This must be
  137. called on a lock variable before the containing storage is reused. */
  138. #if IS_IN (libc) || IS_IN (libpthread)
  139. # define __libc_lock_fini(NAME) ((void) 0)
  140. #else
  141. # define __libc_lock_fini(NAME) \
  142. __libc_maybe_call (__pthread_mutex_destroy, (&(NAME)), 0)
  143. #endif
  144. #if defined SHARED && IS_IN (libc)
  145. # define __libc_rwlock_fini(NAME) ((void) 0)
  146. #else
  147. # define __libc_rwlock_fini(NAME) \
  148. __libc_maybe_call (__pthread_rwlock_destroy, (&(NAME)), 0)
  149. #endif
  150. /* Lock the named lock variable. */
  151. #if IS_IN (libc) || IS_IN (libpthread)
  152. # ifndef __libc_lock_lock
  153. # define __libc_lock_lock(NAME) \
  154. ({ lll_lock (NAME, LLL_PRIVATE); 0; })
  155. # endif
  156. #else
  157. # undef __libc_lock_lock
  158. # define __libc_lock_lock(NAME) \
  159. __libc_maybe_call (__pthread_mutex_lock, (&(NAME)), 0)
  160. #endif
  161. #define __libc_rwlock_rdlock(NAME) \
  162. __libc_ptf_call (__pthread_rwlock_rdlock, (&(NAME)), 0)
  163. #define __libc_rwlock_wrlock(NAME) \
  164. __libc_ptf_call (__pthread_rwlock_wrlock, (&(NAME)), 0)
  165. /* Try to lock the named lock variable. */
  166. #if IS_IN (libc) || IS_IN (libpthread)
  167. # ifndef __libc_lock_trylock
  168. # define __libc_lock_trylock(NAME) \
  169. lll_trylock (NAME)
  170. # endif
  171. #else
  172. # undef __libc_lock_trylock
  173. # define __libc_lock_trylock(NAME) \
  174. __libc_maybe_call (__pthread_mutex_trylock, (&(NAME)), 0)
  175. #endif
  176. #define __libc_rwlock_tryrdlock(NAME) \
  177. __libc_maybe_call (__pthread_rwlock_tryrdlock, (&(NAME)), 0)
  178. #define __libc_rwlock_trywrlock(NAME) \
  179. __libc_maybe_call (__pthread_rwlock_trywrlock, (&(NAME)), 0)
  180. #define __rtld_lock_trylock_recursive(NAME) \
  181. __libc_maybe_call (__pthread_mutex_trylock, (&(NAME).mutex), 0)
  182. /* Unlock the named lock variable. */
  183. #if IS_IN (libc) || IS_IN (libpthread)
  184. # define __libc_lock_unlock(NAME) \
  185. lll_unlock (NAME, LLL_PRIVATE)
  186. #else
  187. # define __libc_lock_unlock(NAME) \
  188. __libc_maybe_call (__pthread_mutex_unlock, (&(NAME)), 0)
  189. #endif
  190. #define __libc_rwlock_unlock(NAME) \
  191. __libc_ptf_call (__pthread_rwlock_unlock, (&(NAME)), 0)
  192. #ifdef SHARED
  193. # define __rtld_lock_default_lock_recursive(lock) \
  194. ++((pthread_mutex_t *)(lock))->__data.__count;
  195. # define __rtld_lock_default_unlock_recursive(lock) \
  196. --((pthread_mutex_t *)(lock))->__data.__count;
  197. # define __rtld_lock_lock_recursive(NAME) \
  198. GL(dl_rtld_lock_recursive) (&(NAME).mutex)
  199. # define __rtld_lock_unlock_recursive(NAME) \
  200. GL(dl_rtld_unlock_recursive) (&(NAME).mutex)
  201. #else
  202. # define __rtld_lock_lock_recursive(NAME) \
  203. __libc_maybe_call (__pthread_mutex_lock, (&(NAME).mutex), 0)
  204. # define __rtld_lock_unlock_recursive(NAME) \
  205. __libc_maybe_call (__pthread_mutex_unlock, (&(NAME).mutex), 0)
  206. #endif
  207. /* Define once control variable. */
  208. #if PTHREAD_ONCE_INIT == 0
  209. /* Special case for static variables where we can avoid the initialization
  210. if it is zero. */
  211. # define __libc_once_define(CLASS, NAME) \
  212. CLASS pthread_once_t NAME
  213. #else
  214. # define __libc_once_define(CLASS, NAME) \
  215. CLASS pthread_once_t NAME = PTHREAD_ONCE_INIT
  216. #endif
  217. /* Call handler iff the first call. */
  218. #define __libc_once(ONCE_CONTROL, INIT_FUNCTION) \
  219. do { \
  220. if (PTFAVAIL (__pthread_once)) \
  221. __libc_ptf_call_always (__pthread_once, (&(ONCE_CONTROL), \
  222. INIT_FUNCTION)); \
  223. else if ((ONCE_CONTROL) == PTHREAD_ONCE_INIT) { \
  224. INIT_FUNCTION (); \
  225. (ONCE_CONTROL) |= 2; \
  226. } \
  227. } while (0)
  228. /* Get once control variable. */
  229. #define __libc_once_get(ONCE_CONTROL) ((ONCE_CONTROL) != PTHREAD_ONCE_INIT)
  230. /* Note that for I/O cleanup handling we are using the old-style
  231. cancel handling. It does not have to be integrated with C++ snce
  232. no C++ code is called in the middle. The old-style handling is
  233. faster and the support is not going away. */
  234. extern void _pthread_cleanup_push (struct _pthread_cleanup_buffer *buffer,
  235. void (*routine) (void *), void *arg);
  236. extern void _pthread_cleanup_pop (struct _pthread_cleanup_buffer *buffer,
  237. int execute);
  238. extern void _pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer,
  239. void (*routine) (void *), void *arg);
  240. extern void _pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer,
  241. int execute);
  242. /* Sometimes we have to exit the block in the middle. */
  243. #define __libc_cleanup_end(DOIT) \
  244. if (_avail) { \
  245. __libc_ptf_call_always (_pthread_cleanup_pop_restore, (&_buffer, DOIT));\
  246. } else if (DOIT) \
  247. _buffer.__routine (_buffer.__arg)
  248. /* Normal cleanup handling, based on C cleanup attribute. */
  249. __extern_inline void
  250. __libc_cleanup_routine (struct __pthread_cleanup_frame *f)
  251. {
  252. if (f->__do_it)
  253. f->__cancel_routine (f->__cancel_arg);
  254. }
  255. #define __libc_cleanup_push(fct, arg) \
  256. do { \
  257. struct __pthread_cleanup_frame __clframe \
  258. __attribute__ ((__cleanup__ (__libc_cleanup_routine))) \
  259. = { .__cancel_routine = (fct), .__cancel_arg = (arg), \
  260. .__do_it = 1 };
  261. #define __libc_cleanup_pop(execute) \
  262. __clframe.__do_it = (execute); \
  263. } while (0)
  264. /* Create thread-specific key. */
  265. #define __libc_key_create(KEY, DESTRUCTOR) \
  266. __libc_ptf_call (__pthread_key_create, (KEY, DESTRUCTOR), 1)
  267. /* Get thread-specific data. */
  268. #define __libc_getspecific(KEY) \
  269. __libc_ptf_call (__pthread_getspecific, (KEY), NULL)
  270. /* Set thread-specific data. */
  271. #define __libc_setspecific(KEY, VALUE) \
  272. __libc_ptf_call (__pthread_setspecific, (KEY, VALUE), 0)
  273. /* Register handlers to execute before and after `fork'. Note that the
  274. last parameter is NULL. The handlers registered by the libc are
  275. never removed so this is OK. */
  276. extern int __register_atfork (void (*__prepare) (void),
  277. void (*__parent) (void),
  278. void (*__child) (void),
  279. void *__dso_handle);
  280. /* Functions that are used by this file and are internal to the GNU C
  281. library. */
  282. extern int __pthread_mutex_init (pthread_mutex_t *__mutex,
  283. const pthread_mutexattr_t *__mutex_attr);
  284. extern int __pthread_mutex_destroy (pthread_mutex_t *__mutex);
  285. extern int __pthread_mutex_trylock (pthread_mutex_t *__mutex);
  286. extern int __pthread_mutex_lock (pthread_mutex_t *__mutex);
  287. extern int __pthread_mutex_unlock (pthread_mutex_t *__mutex);
  288. extern int __pthread_mutexattr_init (pthread_mutexattr_t *__attr);
  289. extern int __pthread_mutexattr_destroy (pthread_mutexattr_t *__attr);
  290. extern int __pthread_mutexattr_settype (pthread_mutexattr_t *__attr,
  291. int __kind);
  292. extern int __pthread_rwlock_init (pthread_rwlock_t *__rwlock,
  293. const pthread_rwlockattr_t *__attr);
  294. extern int __pthread_rwlock_destroy (pthread_rwlock_t *__rwlock);
  295. extern int __pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock);
  296. extern int __pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock);
  297. extern int __pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock);
  298. extern int __pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock);
  299. extern int __pthread_rwlock_unlock (pthread_rwlock_t *__rwlock);
  300. extern int __pthread_key_create (pthread_key_t *__key,
  301. void (*__destr_function) (void *));
  302. extern int __pthread_setspecific (pthread_key_t __key,
  303. const void *__pointer);
  304. extern void *__pthread_getspecific (pthread_key_t __key);
  305. extern int __pthread_once (pthread_once_t *__once_control,
  306. void (*__init_routine) (void));
  307. extern int __pthread_atfork (void (*__prepare) (void),
  308. void (*__parent) (void),
  309. void (*__child) (void));
  310. extern int __pthread_setcancelstate (int state, int *oldstate);
  311. /* Make the pthread functions weak so that we can elide them from
  312. single-threaded processes. */
  313. #ifndef __NO_WEAK_PTHREAD_ALIASES
  314. # ifdef weak_extern
  315. weak_extern (__pthread_mutex_init)
  316. weak_extern (__pthread_mutex_destroy)
  317. weak_extern (__pthread_mutex_lock)
  318. weak_extern (__pthread_mutex_trylock)
  319. weak_extern (__pthread_mutex_unlock)
  320. weak_extern (__pthread_mutexattr_init)
  321. weak_extern (__pthread_mutexattr_destroy)
  322. weak_extern (__pthread_mutexattr_settype)
  323. weak_extern (__pthread_rwlock_init)
  324. weak_extern (__pthread_rwlock_destroy)
  325. weak_extern (__pthread_rwlock_rdlock)
  326. weak_extern (__pthread_rwlock_tryrdlock)
  327. weak_extern (__pthread_rwlock_wrlock)
  328. weak_extern (__pthread_rwlock_trywrlock)
  329. weak_extern (__pthread_rwlock_unlock)
  330. weak_extern (__pthread_key_create)
  331. weak_extern (__pthread_setspecific)
  332. weak_extern (__pthread_getspecific)
  333. weak_extern (__pthread_once)
  334. weak_extern (__pthread_initialize)
  335. weak_extern (__pthread_atfork)
  336. weak_extern (__pthread_setcancelstate)
  337. weak_extern (_pthread_cleanup_push_defer)
  338. weak_extern (_pthread_cleanup_pop_restore)
  339. # else
  340. # pragma weak __pthread_mutex_init
  341. # pragma weak __pthread_mutex_destroy
  342. # pragma weak __pthread_mutex_lock
  343. # pragma weak __pthread_mutex_trylock
  344. # pragma weak __pthread_mutex_unlock
  345. # pragma weak __pthread_mutexattr_init
  346. # pragma weak __pthread_mutexattr_destroy
  347. # pragma weak __pthread_mutexattr_settype
  348. # pragma weak __pthread_rwlock_destroy
  349. # pragma weak __pthread_rwlock_rdlock
  350. # pragma weak __pthread_rwlock_tryrdlock
  351. # pragma weak __pthread_rwlock_wrlock
  352. # pragma weak __pthread_rwlock_trywrlock
  353. # pragma weak __pthread_rwlock_unlock
  354. # pragma weak __pthread_key_create
  355. # pragma weak __pthread_setspecific
  356. # pragma weak __pthread_getspecific
  357. # pragma weak __pthread_once
  358. # pragma weak __pthread_initialize
  359. # pragma weak __pthread_atfork
  360. # pragma weak __pthread_setcancelstate
  361. # pragma weak _pthread_cleanup_push_defer
  362. # pragma weak _pthread_cleanup_pop_restore
  363. # endif
  364. #endif
  365. #endif /* libc-lockP.h */