lowlevellock.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* Copyright (C) 2003, 2004 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 Libr \ary; 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 __NR_futex 394
  23. #define FUTEX_WAIT 0
  24. #define FUTEX_WAKE 1
  25. #define FUTEX_REQUEUE 3
  26. #define FUTEX_CMP_REQUEUE 4
  27. /* Initializer for compatibility lock. */
  28. #define LLL_MUTEX_LOCK_INITIALIZER (0)
  29. #define lll_futex_wait(futexp, val) \
  30. ({ \
  31. INTERNAL_SYSCALL_DECL (__err); \
  32. long int __ret; \
  33. __ret = INTERNAL_SYSCALL (futex, __err, 4, \
  34. (futexp), FUTEX_WAIT, (val), 0); \
  35. INTERNAL_SYSCALL_ERROR_P (__ret, __err)? -__ret : __ret; \
  36. })
  37. #define lll_futex_timed_wait(futexp, val, timespec) \
  38. ({ \
  39. INTERNAL_SYSCALL_DECL (__err); \
  40. long int __ret; \
  41. __ret = INTERNAL_SYSCALL (futex, __err, 4, \
  42. (futexp), FUTEX_WAIT, (val), (timespec)); \
  43. INTERNAL_SYSCALL_ERROR_P (__ret, __err)? -__ret : __ret; \
  44. })
  45. #define lll_futex_wake(futexp, nr) \
  46. ({ \
  47. INTERNAL_SYSCALL_DECL (__err); \
  48. long int __ret; \
  49. __ret = INTERNAL_SYSCALL (futex, __err, 4, \
  50. (futexp), FUTEX_WAKE, (nr), 0); \
  51. INTERNAL_SYSCALL_ERROR_P (__ret, __err)? -__ret : __ret; \
  52. })
  53. /* Returns non-zero if error happened, zero if success. */
  54. #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val) \
  55. ({ \
  56. INTERNAL_SYSCALL_DECL (__err); \
  57. long int __ret; \
  58. __ret = INTERNAL_SYSCALL (futex, __err, 6, \
  59. (futexp), FUTEX_CMP_REQUEUE, (nr_wake), \
  60. (nr_move), (mutex), (val)); \
  61. INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
  62. })
  63. static inline int __attribute__((always_inline))
  64. __lll_mutex_trylock(int *futex)
  65. {
  66. return atomic_compare_and_exchange_val_acq (futex, 1, 0) != 0;
  67. }
  68. #define lll_mutex_trylock(lock) __lll_mutex_trylock (&(lock))
  69. static inline int __attribute__((always_inline))
  70. __lll_mutex_cond_trylock(int *futex)
  71. {
  72. return atomic_compare_and_exchange_val_acq (futex, 2, 0) != 0;
  73. }
  74. #define lll_mutex_cond_trylock(lock) __lll_mutex_cond_trylock (&(lock))
  75. extern void __lll_lock_wait (int *futex) attribute_hidden;
  76. static inline void __attribute__((always_inline))
  77. __lll_mutex_lock(int *futex)
  78. {
  79. if (atomic_compare_and_exchange_bool_acq (futex, 1, 0) != 0)
  80. __lll_lock_wait (futex);
  81. }
  82. #define lll_mutex_lock(futex) __lll_mutex_lock (&(futex))
  83. static inline void __attribute__ ((always_inline))
  84. __lll_mutex_cond_lock (int *futex)
  85. {
  86. if (atomic_compare_and_exchange_bool_acq (futex, 2, 0) != 0)
  87. __lll_lock_wait (futex);
  88. }
  89. #define lll_mutex_cond_lock(futex) __lll_mutex_cond_lock (&(futex))
  90. extern int __lll_timedlock_wait (int *futex, const struct timespec *)
  91. attribute_hidden;
  92. static inline int __attribute__ ((always_inline))
  93. __lll_mutex_timedlock (int *futex, const struct timespec *abstime)
  94. {
  95. int result = 0;
  96. if (atomic_compare_and_exchange_bool_acq (futex, 1, 0) != 0)
  97. result = __lll_timedlock_wait (futex, abstime);
  98. return result;
  99. }
  100. #define lll_mutex_timedlock(futex, abstime) \
  101. __lll_mutex_timedlock (&(futex), abstime)
  102. static inline void __attribute__ ((always_inline))
  103. __lll_mutex_unlock (int *futex)
  104. {
  105. int val = atomic_exchange_rel (futex, 0);
  106. if (__builtin_expect (val > 1, 0))
  107. lll_futex_wake (futex, 1);
  108. }
  109. #define lll_mutex_unlock(futex) __lll_mutex_unlock(&(futex))
  110. static inline void __attribute__ ((always_inline))
  111. __lll_mutex_unlock_force (int *futex)
  112. {
  113. (void) atomic_exchange_rel (futex, 0);
  114. lll_futex_wake (futex, 1);
  115. }
  116. #define lll_mutex_unlock_force(futex) __lll_mutex_unlock_force(&(futex))
  117. #define lll_mutex_islocked(futex) \
  118. (futex != 0)
  119. /* Our internal lock implementation is identical to the binary-compatible
  120. mutex implementation. */
  121. /* Type for lock object. */
  122. typedef int lll_lock_t;
  123. /* Initializers for lock. */
  124. #define LLL_LOCK_INITIALIZER (0)
  125. #define LLL_LOCK_INITIALIZER_LOCKED (1)
  126. extern int lll_unlock_wake_cb (int *__futex) attribute_hidden;
  127. /* The states of a lock are:
  128. 0 - untaken
  129. 1 - taken by one user
  130. >1 - taken by more users */
  131. #define lll_trylock(lock) lll_mutex_trylock (lock)
  132. #define lll_lock(lock) lll_mutex_lock (lock)
  133. #define lll_unlock(lock) lll_mutex_unlock (lock)
  134. #define lll_islocked(lock) lll_mutex_islocked (lock)
  135. /* The kernel notifies a process which uses CLONE_CLEARTID via futex
  136. wakeup when the clone terminates. The memory location contains the
  137. thread ID while the clone is running and is reset to zero
  138. afterwards. */
  139. #define lll_wait_tid(tid) \
  140. do { \
  141. __typeof (tid) __tid; \
  142. while ((__tid = (tid)) != 0) \
  143. lll_futex_wait (&(tid), __tid); \
  144. } while (0)
  145. extern int __lll_timedwait_tid (int *, const struct timespec *)
  146. attribute_hidden;
  147. #define lll_timedwait_tid(tid, abstime) \
  148. ({ \
  149. int __res = 0; \
  150. if ((tid) != 0) \
  151. __res = __lll_timedwait_tid (&(tid), (abstime)); \
  152. __res; \
  153. })
  154. /* Conditional variable handling. */
  155. extern void __lll_cond_wait (pthread_cond_t *cond)
  156. attribute_hidden;
  157. extern int __lll_cond_timedwait (pthread_cond_t *cond,
  158. const struct timespec *abstime)
  159. attribute_hidden;
  160. extern void __lll_cond_wake (pthread_cond_t *cond)
  161. attribute_hidden;
  162. extern void __lll_cond_broadcast (pthread_cond_t *cond)
  163. attribute_hidden;
  164. #define lll_cond_wait(cond) \
  165. __lll_cond_wait (cond)
  166. #define lll_cond_timedwait(cond, abstime) \
  167. __lll_cond_timedwait (cond, abstime)
  168. #define lll_cond_wake(cond) \
  169. __lll_cond_wake (cond)
  170. #define lll_cond_broadcast(cond) \
  171. __lll_cond_broadcast (cond)
  172. #endif /* lowlevellock.h */