lowlevellock.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* Copyright (C) 2003, 2004, 2006, 2007, 2008 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
  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
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the 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 Libr \ary; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #ifndef _LOWLEVELLOCK_H
  17. #define _LOWLEVELLOCK_H 1
  18. #include <time.h>
  19. #include <sys/param.h>
  20. #include <bits/pthreadtypes.h>
  21. #include <atomic.h>
  22. #include <sysdep.h>
  23. #define FUTEX_WAIT 0
  24. #define FUTEX_WAKE 1
  25. #define FUTEX_REQUEUE 3
  26. #define FUTEX_CMP_REQUEUE 4
  27. #define FUTEX_WAKE_OP 5
  28. #define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE ((4 << 24) | 1)
  29. /* Initializer for compatibility lock. */
  30. #define LLL_MUTEX_LOCK_INITIALIZER (0)
  31. #define lll_futex_wait(futexp, val) \
  32. lll_futex_timed_wait (futexp, val, NULL)
  33. #define lll_futex_timed_wait(futexp, val, timespec) \
  34. ({ \
  35. INTERNAL_SYSCALL_DECL (__err); \
  36. long int __ret; \
  37. __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
  38. FUTEX_WAIT, (val), (timespec)); \
  39. __ret; \
  40. })
  41. #define lll_futex_wake(futexp, nr) \
  42. ({ \
  43. INTERNAL_SYSCALL_DECL (__err); \
  44. long int __ret; \
  45. __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp), \
  46. FUTEX_WAKE, (nr), 0); \
  47. __ret; \
  48. })
  49. /* Returns non-zero if error happened, zero if success. */
  50. #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val) \
  51. ({ \
  52. INTERNAL_SYSCALL_DECL (__err); \
  53. long int __ret; \
  54. __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
  55. FUTEX_CMP_REQUEUE, (nr_wake), (nr_move), (mutex), (val)); \
  56. INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
  57. })
  58. #define lll_robust_dead(futexv) \
  59. do \
  60. { \
  61. int *__futexp = &(futexv); \
  62. atomic_or (__futexp, FUTEX_OWNER_DIED); \
  63. lll_futex_wake (__futexp, 1); \
  64. } \
  65. while (0)
  66. /* Returns non-zero if error happened, zero if success. */
  67. #ifdef __sparc32_atomic_do_lock
  68. /* Avoid FUTEX_WAKE_OP if supporting pre-v9 CPUs. */
  69. # define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2) 1
  70. #else
  71. # define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2) \
  72. ({ \
  73. INTERNAL_SYSCALL_DECL (__err); \
  74. long int __ret; \
  75. \
  76. __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp), \
  77. FUTEX_WAKE_OP, \
  78. (nr_wake), (nr_wake2), (futexp2), \
  79. FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \
  80. INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
  81. })
  82. #endif
  83. static inline int
  84. __attribute__ ((always_inline))
  85. __lll_trylock (int *futex)
  86. {
  87. return atomic_compare_and_exchange_val_24_acq (futex, 1, 0) != 0;
  88. }
  89. #define lll_mutex_trylock(futex) __lll_trylock (&(futex))
  90. static inline int
  91. __attribute__ ((always_inline))
  92. __lll_cond_trylock (int *futex)
  93. {
  94. return atomic_compare_and_exchange_val_24_acq (futex, 2, 0) != 0;
  95. }
  96. #define lll_mutex_cond_trylock(futex) __lll_cond_trylock (&(futex))
  97. static inline int
  98. __attribute__ ((always_inline))
  99. __lll_robust_trylock (int *futex, int id)
  100. {
  101. return atomic_compare_and_exchange_val_acq (futex, id, 0) != 0;
  102. }
  103. #define lll_robust_trylock(futex, id) \
  104. __lll_robust_trylock (&(futex), id)
  105. extern void __lll_lock_wait (int *futex) attribute_hidden;
  106. extern int __lll_robust_lock_wait (int *futex) attribute_hidden;
  107. static inline void
  108. __attribute__ ((always_inline))
  109. __lll_lock (int *futex)
  110. {
  111. int val = atomic_compare_and_exchange_val_24_acq (futex, 1, 0);
  112. if (__builtin_expect (val != 0, 0))
  113. {
  114. __lll_lock_wait (futex);
  115. }
  116. }
  117. #define lll_mutex_lock(futex) __lll_lock (&(futex))
  118. static inline int
  119. __attribute__ ((always_inline))
  120. __lll_robust_lock (int *futex, int id)
  121. {
  122. int result = 0;
  123. if (atomic_compare_and_exchange_bool_acq (futex, id, 0) != 0)
  124. result = __lll_robust_lock_wait (futex);
  125. return result;
  126. }
  127. #define lll_robust_lock(futex, id) \
  128. __lll_robust_lock (&(futex), id)
  129. static inline void
  130. __attribute__ ((always_inline))
  131. __lll_cond_lock (int *futex)
  132. {
  133. int val = atomic_compare_and_exchange_val_24_acq (futex, 2, 0);
  134. if (__builtin_expect (val != 0, 0))
  135. __lll_lock_wait (futex);
  136. }
  137. #define lll_mutex_cond_lock(futex) __lll_cond_lock (&(futex))
  138. #define lll_robust_cond_lock(futex, id) \
  139. __lll_robust_lock (&(futex), (id) | FUTEX_WAITERS)
  140. extern int __lll_timedlock_wait (int *futex, const struct timespec *) attribute_hidden;
  141. extern int __lll_robust_timedlock_wait (int *futex, const struct timespec *) attribute_hidden;
  142. static inline int
  143. __attribute__ ((always_inline))
  144. __lll_timedlock (int *futex, const struct timespec *abstime)
  145. {
  146. int val = atomic_compare_and_exchange_val_24_acq (futex, 1, 0);
  147. int result = 0;
  148. if (__builtin_expect (val != 0, 0))
  149. result = __lll_timedlock_wait (futex, abstime);
  150. return result;
  151. }
  152. #define lll_mutex_timedlock(futex, abstime) \
  153. __lll_timedlock (&(futex), abstime)
  154. static inline int
  155. __attribute__ ((always_inline))
  156. __lll_robust_timedlock (int *futex, const struct timespec *abstime,
  157. int id)
  158. {
  159. int result = 0;
  160. if (atomic_compare_and_exchange_bool_acq (futex, id, 0) != 0)
  161. result = __lll_robust_timedlock_wait (futex, abstime);
  162. return result;
  163. }
  164. #define lll_robust_timedlock(futex, abstime, id) \
  165. __lll_robust_timedlock (&(futex), abstime, id)
  166. #define lll_mutex_unlock(lock) \
  167. ((void) ({ \
  168. int *__futex = &(lock); \
  169. int __val = atomic_exchange_24_rel (__futex, 0); \
  170. if (__builtin_expect (__val > 1, 0)) \
  171. lll_futex_wake (__futex, 1); \
  172. }))
  173. #define lll_robust_unlock(lock) \
  174. ((void) ({ \
  175. int *__futex = &(lock); \
  176. int __val = atomic_exchange_rel (__futex, 0); \
  177. if (__builtin_expect (__val & FUTEX_WAITERS, 0)) \
  178. lll_futex_wake (__futex, 1); \
  179. }))
  180. #define lll_mutex_islocked(futex) \
  181. (futex != 0)
  182. /* We have a separate internal lock implementation which is not tied
  183. to binary compatibility. */
  184. /* Type for lock object. */
  185. typedef int lll_lock_t;
  186. /* Initializers for lock. */
  187. #define LLL_LOCK_INITIALIZER (0)
  188. #define LLL_LOCK_INITIALIZER_LOCKED (1)
  189. extern int lll_unlock_wake_cb (int *__futex) attribute_hidden;
  190. #define lll_trylock(lock) lll_mutex_trylock (lock)
  191. #define lll_lock(lock) lll_mutex_lock (lock)
  192. #define lll_unlock(lock) lll_mutex_unlock (lock)
  193. #define lll_islocked(lock) lll_mutex_islocked (lock)
  194. /* The kernel notifies a process with uses CLONE_CLEARTID via futex
  195. wakeup when the clone terminates. The memory location contains the
  196. thread ID while the clone is running and is reset to zero
  197. afterwards. */
  198. #define lll_wait_tid(tid) \
  199. do \
  200. { \
  201. __typeof (tid) __tid; \
  202. while ((__tid = (tid)) != 0) \
  203. lll_futex_wait (&(tid), __tid); \
  204. } \
  205. while (0)
  206. extern int __lll_timedwait_tid (int *, const struct timespec *)
  207. attribute_hidden;
  208. #define lll_timedwait_tid(tid, abstime) \
  209. ({ \
  210. int __res = 0; \
  211. if ((tid) != 0) \
  212. __res = __lll_timedwait_tid (&(tid), (abstime)); \
  213. __res; \
  214. })
  215. #endif /* lowlevellock.h */