libc-lock.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* 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_LOCK_H
  16. #define _LIBC_LOCK_H 1
  17. #include <pthread.h>
  18. #define __need_NULL
  19. #include <stddef.h>
  20. #include <libc-lock-arch.h>
  21. /* Mutex type. */
  22. #if defined _LIBC || defined _IO_MTSAFE_IO
  23. # if (!IS_IN (libc) && !IS_IN (libpthread)) || !defined _LIBC
  24. typedef struct { pthread_mutex_t mutex; } __libc_lock_recursive_t;
  25. # else
  26. typedef struct
  27. {
  28. int lock __LIBC_LOCK_ALIGNMENT;
  29. int cnt;
  30. void *owner;
  31. } __libc_lock_recursive_t;
  32. # endif
  33. #else
  34. typedef struct __libc_lock_recursive_opaque__ __libc_lock_recursive_t;
  35. #endif
  36. /* Define a lock variable NAME with storage class CLASS. The lock must be
  37. initialized with __libc_lock_init before it can be used (or define it
  38. with __libc_lock_define_initialized, below). Use `extern' for CLASS to
  39. declare a lock defined in another module. In public structure
  40. definitions you must use a pointer to the lock structure (i.e., NAME
  41. begins with a `*'), because its storage size will not be known outside
  42. of libc. */
  43. #define __libc_lock_define_recursive(CLASS,NAME) \
  44. CLASS __libc_lock_recursive_t NAME;
  45. /* Define an initialized recursive lock variable NAME with storage
  46. class CLASS. */
  47. #if defined _LIBC && (IS_IN (libc) || IS_IN (libpthread))
  48. # define __libc_lock_define_initialized_recursive(CLASS, NAME) \
  49. CLASS __libc_lock_recursive_t NAME = _LIBC_LOCK_RECURSIVE_INITIALIZER;
  50. # define _LIBC_LOCK_RECURSIVE_INITIALIZER \
  51. { LLL_LOCK_INITIALIZER, 0, NULL }
  52. #else
  53. # define __libc_lock_define_initialized_recursive(CLASS,NAME) \
  54. CLASS __libc_lock_recursive_t NAME = _LIBC_LOCK_RECURSIVE_INITIALIZER;
  55. # define _LIBC_LOCK_RECURSIVE_INITIALIZER \
  56. {PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP}
  57. #endif
  58. /* Initialize a recursive mutex. */
  59. #if defined _LIBC && (IS_IN (libc) || IS_IN (libpthread))
  60. # define __libc_lock_init_recursive(NAME) \
  61. ((void) ((NAME) = (__libc_lock_recursive_t) _LIBC_LOCK_RECURSIVE_INITIALIZER))
  62. #else
  63. # define __libc_lock_init_recursive(NAME) \
  64. do { \
  65. if (__pthread_mutex_init != NULL) \
  66. { \
  67. pthread_mutexattr_t __attr; \
  68. __pthread_mutexattr_init (&__attr); \
  69. __pthread_mutexattr_settype (&__attr, PTHREAD_MUTEX_RECURSIVE_NP); \
  70. __pthread_mutex_init (&(NAME).mutex, &__attr); \
  71. __pthread_mutexattr_destroy (&__attr); \
  72. } \
  73. } while (0)
  74. #endif
  75. /* Finalize recursive named lock. */
  76. #if defined _LIBC && (IS_IN (libc) || IS_IN (libpthread))
  77. # define __libc_lock_fini_recursive(NAME) ((void) 0)
  78. #else
  79. # define __libc_lock_fini_recursive(NAME) \
  80. __libc_maybe_call (__pthread_mutex_destroy, (&(NAME).mutex), 0)
  81. #endif
  82. /* Lock the recursive named lock variable. */
  83. #if defined _LIBC && (IS_IN (libc) || IS_IN (libpthread))
  84. # define __libc_lock_lock_recursive(NAME) \
  85. do { \
  86. void *self = THREAD_SELF; \
  87. if ((NAME).owner != self) \
  88. { \
  89. lll_lock ((NAME).lock, LLL_PRIVATE); \
  90. (NAME).owner = self; \
  91. } \
  92. ++(NAME).cnt; \
  93. } while (0)
  94. #else
  95. # define __libc_lock_lock_recursive(NAME) \
  96. __libc_maybe_call (__pthread_mutex_lock, (&(NAME).mutex), 0)
  97. #endif
  98. /* Try to lock the recursive named lock variable. */
  99. #if defined _LIBC && (IS_IN (libc) || IS_IN (libpthread))
  100. # define __libc_lock_trylock_recursive(NAME) \
  101. ({ \
  102. int result = 0; \
  103. void *self = THREAD_SELF; \
  104. if ((NAME).owner != self) \
  105. { \
  106. if (lll_trylock ((NAME).lock) == 0) \
  107. { \
  108. (NAME).owner = self; \
  109. (NAME).cnt = 1; \
  110. } \
  111. else \
  112. result = EBUSY; \
  113. } \
  114. else \
  115. ++(NAME).cnt; \
  116. result; \
  117. })
  118. #else
  119. # define __libc_lock_trylock_recursive(NAME) \
  120. __libc_maybe_call (__pthread_mutex_trylock, (&(NAME).mutex), 0)
  121. #endif
  122. /* Unlock the recursive named lock variable. */
  123. #if defined _LIBC && (IS_IN (libc) || IS_IN (libpthread))
  124. /* We do no error checking here. */
  125. # define __libc_lock_unlock_recursive(NAME) \
  126. do { \
  127. if (--(NAME).cnt == 0) \
  128. { \
  129. (NAME).owner = NULL; \
  130. lll_unlock ((NAME).lock, LLL_PRIVATE); \
  131. } \
  132. } while (0)
  133. #else
  134. # define __libc_lock_unlock_recursive(NAME) \
  135. __libc_maybe_call (__pthread_mutex_unlock, (&(NAME).mutex), 0)
  136. #endif
  137. /* Put the unwind buffer BUFFER on the per-thread callback stack. The
  138. caller must fill BUFFER->__routine and BUFFER->__arg before calling
  139. this function. */
  140. void __libc_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer);
  141. libc_hidden_proto (__libc_cleanup_push_defer)
  142. /* Remove BUFFER from the unwind callback stack. The caller must invoke
  143. the callback if desired. */
  144. void __libc_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer);
  145. libc_hidden_proto (__libc_cleanup_pop_restore)
  146. /* Start critical region with cleanup. */
  147. #define __libc_cleanup_region_start(DOIT, FCT, ARG) \
  148. { bool _cleanup_start_doit; \
  149. struct _pthread_cleanup_buffer _buffer; \
  150. /* Non-addressable copy of FCT, so that we avoid indirect calls on \
  151. the non-unwinding path. */ \
  152. void (*_cleanup_routine) (void *) = (FCT); \
  153. _buffer.__arg = (ARG); \
  154. if (DOIT) \
  155. { \
  156. _cleanup_start_doit = true; \
  157. _buffer.__routine = _cleanup_routine; \
  158. __libc_cleanup_push_defer (&_buffer); \
  159. } \
  160. else \
  161. _cleanup_start_doit = false;
  162. /* End critical region with cleanup. */
  163. #define __libc_cleanup_region_end(DOIT) \
  164. if (_cleanup_start_doit) \
  165. __libc_cleanup_pop_restore (&_buffer); \
  166. if (DOIT) \
  167. _cleanup_routine (_buffer.__arg); \
  168. } /* matches __libc_cleanup_region_start */
  169. /* Hide the definitions which are only supposed to be used inside libc in
  170. a separate file. This file is not present in the installation! */
  171. #ifdef _LIBC
  172. # include "libc-lockP.h"
  173. #endif
  174. #endif /* libc-lock.h */