lowlevellock.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* Copyright (C) 2003, 2004 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Paul Mackerras <paulus@au.ibm.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 Library; 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. #ifndef __NR_futex
  23. # define __NR_futex 221
  24. #endif
  25. #define FUTEX_WAIT 0
  26. #define FUTEX_WAKE 1
  27. #define FUTEX_REQUEUE 3
  28. #define FUTEX_CMP_REQUEUE 4
  29. /* Initializer for compatibility lock. */
  30. #define LLL_MUTEX_LOCK_INITIALIZER (0)
  31. #define lll_futex_wait(futexp, val) \
  32. ({ \
  33. INTERNAL_SYSCALL_DECL (__err); \
  34. long int __ret; \
  35. \
  36. __ret = INTERNAL_SYSCALL (futex, __err, 4, \
  37. (futexp), FUTEX_WAIT, (val), 0); \
  38. INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
  39. })
  40. #define lll_futex_timed_wait(futexp, val, timespec) \
  41. ({ \
  42. INTERNAL_SYSCALL_DECL (__err); \
  43. long int __ret; \
  44. \
  45. __ret = INTERNAL_SYSCALL (futex, __err, 4, \
  46. (futexp), FUTEX_WAIT, (val), (timespec)); \
  47. INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
  48. })
  49. #define lll_futex_wake(futexp, nr) \
  50. ({ \
  51. INTERNAL_SYSCALL_DECL (__err); \
  52. long int __ret; \
  53. \
  54. __ret = INTERNAL_SYSCALL (futex, __err, 4, \
  55. (futexp), FUTEX_WAKE, (nr), 0); \
  56. INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret; \
  57. })
  58. /* Returns non-zero if error happened, zero if success. */
  59. #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val) \
  60. ({ \
  61. INTERNAL_SYSCALL_DECL (__err); \
  62. long int __ret; \
  63. \
  64. __ret = INTERNAL_SYSCALL (futex, __err, 6, \
  65. (futexp), FUTEX_CMP_REQUEUE, (nr_wake), \
  66. (nr_move), (mutex), (val)); \
  67. INTERNAL_SYSCALL_ERROR_P (__ret, __err); \
  68. })
  69. #ifdef UP
  70. # define __lll_acq_instr ""
  71. # define __lll_rel_instr ""
  72. #else
  73. # define __lll_acq_instr "isync"
  74. # define __lll_rel_instr "sync"
  75. #endif
  76. /* Set *futex to 1 if it is 0, atomically. Returns the old value */
  77. #define __lll_trylock(futex) \
  78. ({ int __val; \
  79. __asm __volatile ("1: lwarx %0,0,%2\n" \
  80. " cmpwi 0,%0,0\n" \
  81. " bne 2f\n" \
  82. " stwcx. %3,0,%2\n" \
  83. " bne- 1b\n" \
  84. "2: " __lll_acq_instr \
  85. : "=&r" (__val), "=m" (*futex) \
  86. : "r" (futex), "r" (1), "m" (*futex) \
  87. : "cr0", "memory"); \
  88. __val; \
  89. })
  90. #define lll_mutex_trylock(lock) __lll_trylock (&(lock))
  91. /* Set *futex to 2 if it is 0, atomically. Returns the old value */
  92. #define __lll_cond_trylock(futex) \
  93. ({ int __val; \
  94. __asm __volatile ("1: lwarx %0,0,%2\n" \
  95. " cmpwi 0,%0,0\n" \
  96. " bne 2f\n" \
  97. " stwcx. %3,0,%2\n" \
  98. " bne- 1b\n" \
  99. "2: " __lll_acq_instr \
  100. : "=&r" (__val), "=m" (*futex) \
  101. : "r" (futex), "r" (2), "m" (*futex) \
  102. : "cr0", "memory"); \
  103. __val; \
  104. })
  105. #define lll_mutex_cond_trylock(lock) __lll_cond_trylock (&(lock))
  106. extern void __lll_lock_wait (int *futex) attribute_hidden;
  107. #define lll_mutex_lock(lock) \
  108. (void) ({ \
  109. int *__futex = &(lock); \
  110. if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0),\
  111. 0) != 0) \
  112. __lll_lock_wait (__futex); \
  113. })
  114. #define lll_mutex_cond_lock(lock) \
  115. (void) ({ \
  116. int *__futex = &(lock); \
  117. if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 2, 0),\
  118. 0) != 0) \
  119. __lll_lock_wait (__futex); \
  120. })
  121. extern int __lll_timedlock_wait
  122. (int *futex, const struct timespec *) attribute_hidden;
  123. #define lll_mutex_timedlock(lock, abstime) \
  124. ({ \
  125. int *__futex = &(lock); \
  126. int __val = 0; \
  127. if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0),\
  128. 0) != 0) \
  129. __val = __lll_timedlock_wait (__futex, abstime); \
  130. __val; \
  131. })
  132. #define lll_mutex_unlock(lock) \
  133. ((void) ({ \
  134. int *__futex = &(lock); \
  135. int __val = atomic_exchange_rel (__futex, 0); \
  136. if (__builtin_expect (__val > 1, 0)) \
  137. lll_futex_wake (__futex, 1); \
  138. }))
  139. #define lll_mutex_unlock_force(lock) \
  140. ((void) ({ \
  141. int *__futex = &(lock); \
  142. *__futex = 0; \
  143. __asm __volatile (__lll_rel_instr ::: "memory"); \
  144. lll_futex_wake (__futex, 1); \
  145. }))
  146. #define lll_mutex_islocked(futex) \
  147. (futex != 0)
  148. /* Our internal lock implementation is identical to the binary-compatible
  149. mutex implementation. */
  150. /* Type for lock object. */
  151. typedef int lll_lock_t;
  152. /* Initializers for lock. */
  153. #define LLL_LOCK_INITIALIZER (0)
  154. #define LLL_LOCK_INITIALIZER_LOCKED (1)
  155. extern int lll_unlock_wake_cb (int *__futex) attribute_hidden;
  156. /* The states of a lock are:
  157. 0 - untaken
  158. 1 - taken by one user
  159. >1 - taken by more users */
  160. #define lll_trylock(lock) lll_mutex_trylock (lock)
  161. #define lll_lock(lock) lll_mutex_lock (lock)
  162. #define lll_unlock(lock) lll_mutex_unlock (lock)
  163. #define lll_islocked(lock) lll_mutex_islocked (lock)
  164. /* The kernel notifies a process which uses CLONE_CLEARTID via futex
  165. wakeup when the clone terminates. The memory location contains the
  166. thread ID while the clone is running and is reset to zero
  167. afterwards. */
  168. #define lll_wait_tid(tid) \
  169. do { \
  170. __typeof (tid) __tid; \
  171. while ((__tid = (tid)) != 0) \
  172. lll_futex_wait (&(tid), __tid); \
  173. } while (0)
  174. extern int __lll_timedwait_tid (int *, const struct timespec *)
  175. attribute_hidden;
  176. #define lll_timedwait_tid(tid, abstime) \
  177. ({ \
  178. int __res = 0; \
  179. if ((tid) != 0) \
  180. __res = __lll_timedwait_tid (&(tid), (abstime)); \
  181. __res; \
  182. })
  183. /* Conditional variable handling. */
  184. extern void __lll_cond_wait (pthread_cond_t *cond)
  185. attribute_hidden;
  186. extern int __lll_cond_timedwait (pthread_cond_t *cond,
  187. const struct timespec *abstime)
  188. attribute_hidden;
  189. extern void __lll_cond_wake (pthread_cond_t *cond)
  190. attribute_hidden;
  191. extern void __lll_cond_broadcast (pthread_cond_t *cond)
  192. attribute_hidden;
  193. #define lll_cond_wait(cond) \
  194. __lll_cond_wait (cond)
  195. #define lll_cond_timedwait(cond, abstime) \
  196. __lll_cond_timedwait (cond, abstime)
  197. #define lll_cond_wake(cond) \
  198. __lll_cond_wake (cond)
  199. #define lll_cond_broadcast(cond) \
  200. __lll_cond_broadcast (cond)
  201. #endif /* lowlevellock.h */