libc-lock.h 6.7 KB

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