lowlevellock.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, write to the Free
  13. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  14. 02111-1307 USA. */
  15. #ifndef _LOWLEVELLOCK_H
  16. #define _LOWLEVELLOCK_H 1
  17. #include <time.h>
  18. #include <sys/param.h>
  19. #include <bits/pthreadtypes.h>
  20. #include <atomic.h>
  21. #include <sysdep.h>
  22. #define FUTEX_WAIT 0
  23. #define FUTEX_WAKE 1
  24. #define FUTEX_REQUEUE 3
  25. #define FUTEX_CMP_REQUEUE 4
  26. /* Initializer for compatibility lock. */
  27. #define LLL_MUTEX_LOCK_INITIALIZER (0)
  28. #define lll_futex_wait(futexp, val) \
  29. ({ \
  30. INTERNAL_SYSCALL_DECL (__err); \
  31. long int __ret; \
  32. __ret = INTERNAL_SYSCALL (futex, __err, 4, \
  33. (futexp), FUTEX_WAIT, (val), 0); \
  34. INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
  35. })
  36. #define lll_futex_timed_wait(futexp, val, timespec) \
  37. ({ \
  38. INTERNAL_SYSCALL_DECL (__err); \
  39. long int __ret; \
  40. __ret = INTERNAL_SYSCALL (futex, __err, 4, \
  41. (futexp), FUTEX_WAIT, (val), (timespec)); \
  42. INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
  43. })
  44. #define lll_futex_wake(futexp, nr) \
  45. ({ \
  46. INTERNAL_SYSCALL_DECL (__err); \
  47. long int __ret; \
  48. __ret = INTERNAL_SYSCALL (futex, __err, 4, \
  49. (futexp), FUTEX_WAKE, (nr), 0); \
  50. INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
  51. })
  52. /* Returns non-zero if error happened, zero if success. */
  53. #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val) \
  54. ({ \
  55. INTERNAL_SYSCALL_DECL (__err); \
  56. long int __ret; \
  57. __ret = INTERNAL_SYSCALL (futex, __err, 6, \
  58. (futexp), FUTEX_CMP_REQUEUE, (nr_wake), \
  59. (nr_move), (mutex), (val)); \
  60. INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
  61. })
  62. static inline int __attribute__((always_inline))
  63. __lll_mutex_trylock(int *futex)
  64. {
  65. return atomic_compare_and_exchange_val_acq (futex, 1, 0) != 0;
  66. }
  67. #define lll_mutex_trylock(lock) __lll_mutex_trylock (&(lock))
  68. static inline int __attribute__((always_inline))
  69. __lll_mutex_cond_trylock(int *futex)
  70. {
  71. return atomic_compare_and_exchange_val_acq (futex, 2, 0) != 0;
  72. }
  73. #define lll_mutex_cond_trylock(lock) __lll_mutex_cond_trylock (&(lock))
  74. extern void __lll_lock_wait (int *futex) attribute_hidden;
  75. static inline void __attribute__((always_inline))
  76. __lll_mutex_lock(int *futex)
  77. {
  78. if (atomic_compare_and_exchange_bool_acq (futex, 1, 0) != 0)
  79. __lll_lock_wait (futex);
  80. }
  81. #define lll_mutex_lock(futex) __lll_mutex_lock (&(futex))
  82. static inline void __attribute__ ((always_inline))
  83. __lll_mutex_cond_lock (int *futex)
  84. {
  85. if (atomic_compare_and_exchange_bool_acq (futex, 2, 0) != 0)
  86. __lll_lock_wait (futex);
  87. }
  88. #define lll_mutex_cond_lock(futex) __lll_mutex_cond_lock (&(futex))
  89. extern int __lll_timedlock_wait (int *futex, const struct timespec *)
  90. attribute_hidden;
  91. static inline int __attribute__ ((always_inline))
  92. __lll_mutex_timedlock (int *futex, const struct timespec *abstime)
  93. {
  94. int result = 0;
  95. if (atomic_compare_and_exchange_bool_acq (futex, 1, 0) != 0)
  96. result = __lll_timedlock_wait (futex, abstime);
  97. return result;
  98. }
  99. #define lll_mutex_timedlock(futex, abstime) \
  100. __lll_mutex_timedlock (&(futex), abstime)
  101. static inline void __attribute__ ((always_inline))
  102. __lll_mutex_unlock (int *futex)
  103. {
  104. int val = atomic_exchange_rel (futex, 0);
  105. if (__builtin_expect (val > 1, 0))
  106. lll_futex_wake (futex, 1);
  107. }
  108. #define lll_mutex_unlock(futex) __lll_mutex_unlock(&(futex))
  109. static inline void __attribute__ ((always_inline))
  110. __lll_mutex_unlock_force (int *futex)
  111. {
  112. (void) atomic_exchange_rel (futex, 0);
  113. lll_futex_wake (futex, 1);
  114. }
  115. #define lll_mutex_unlock_force(futex) __lll_mutex_unlock_force(&(futex))
  116. #define lll_mutex_islocked(futex) \
  117. (futex != 0)
  118. /* Our internal lock implementation is identical to the binary-compatible
  119. mutex implementation. */
  120. /* Type for lock object. */
  121. typedef int lll_lock_t;
  122. /* Initializers for lock. */
  123. #define LLL_LOCK_INITIALIZER (0)
  124. #define LLL_LOCK_INITIALIZER_LOCKED (1)
  125. extern int lll_unlock_wake_cb (int *__futex) attribute_hidden;
  126. /* The states of a lock are:
  127. 0 - untaken
  128. 1 - taken by one user
  129. >1 - taken by more users */
  130. #define lll_trylock(lock) lll_mutex_trylock (lock)
  131. #define lll_lock(lock) lll_mutex_lock (lock)
  132. #define lll_unlock(lock) lll_mutex_unlock (lock)
  133. #define lll_islocked(lock) lll_mutex_islocked (lock)
  134. /* The kernel notifies a process which uses CLONE_CLEARTID via futex
  135. wakeup when the clone terminates. The memory location contains the
  136. thread ID while the clone is running and is reset to zero
  137. afterwards. */
  138. #define lll_wait_tid(tid) \
  139. do { \
  140. __typeof (tid) __tid; \
  141. while ((__tid = (tid)) != 0) \
  142. lll_futex_wait (&(tid), __tid); \
  143. } while (0)
  144. extern int __lll_timedwait_tid (int *, const struct timespec *)
  145. attribute_hidden;
  146. #define lll_timedwait_tid(tid, abstime) \
  147. ({ \
  148. int __res = 0; \
  149. if ((tid) != 0) \
  150. __res = __lll_timedwait_tid (&(tid), (abstime)); \
  151. __res; \
  152. })
  153. /* Conditional variable handling. */
  154. extern void __lll_cond_wait (pthread_cond_t *cond)
  155. attribute_hidden;
  156. extern int __lll_cond_timedwait (pthread_cond_t *cond,
  157. const struct timespec *abstime)
  158. attribute_hidden;
  159. extern void __lll_cond_wake (pthread_cond_t *cond)
  160. attribute_hidden;
  161. extern void __lll_cond_broadcast (pthread_cond_t *cond)
  162. attribute_hidden;
  163. #define lll_cond_wait(cond) \
  164. __lll_cond_wait (cond)
  165. #define lll_cond_timedwait(cond, abstime) \
  166. __lll_cond_timedwait (cond, abstime)
  167. #define lll_cond_wake(cond) \
  168. __lll_cond_wake (cond)
  169. #define lll_cond_broadcast(cond) \
  170. __lll_cond_broadcast (cond)
  171. #endif /* lowlevellock.h */