lowlevellock.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /* Copyright (C) 2005, 2006 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 FUTEX_WAIT 0
  23. #define FUTEX_WAKE 1
  24. #define FUTEX_REQUEUE 3
  25. #define FUTEX_CMP_REQUEUE 4
  26. #define FUTEX_WAKE_OP 5
  27. #define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE ((4 << 24) | 1)
  28. /* Initializer for compatibility lock. */
  29. #define LLL_MUTEX_LOCK_INITIALIZER (0)
  30. #define lll_futex_wait(futexp, val) \
  31. ({ \
  32. INTERNAL_SYSCALL_DECL (__err); \
  33. long int __ret; \
  34. __ret = INTERNAL_SYSCALL (futex, __err, 4, \
  35. (futexp), FUTEX_WAIT, (val), 0); \
  36. __ret; \
  37. })
  38. #define lll_futex_timed_wait(futexp, val, timespec) \
  39. ({ \
  40. INTERNAL_SYSCALL_DECL (__err); \
  41. long int __ret; \
  42. __ret = INTERNAL_SYSCALL (futex, __err, 4, \
  43. (futexp), FUTEX_WAIT, (val), (timespec)); \
  44. __ret; \
  45. })
  46. #define lll_futex_wake(futexp, nr) \
  47. ({ \
  48. INTERNAL_SYSCALL_DECL (__err); \
  49. long int __ret; \
  50. __ret = INTERNAL_SYSCALL (futex, __err, 4, \
  51. (futexp), FUTEX_WAKE, (nr), 0); \
  52. __ret; \
  53. })
  54. #define lll_robust_mutex_dead(futexv) \
  55. do \
  56. { \
  57. int *__futexp = &(futexv); \
  58. atomic_or (__futexp, FUTEX_OWNER_DIED); \
  59. lll_futex_wake (__futexp, 1); \
  60. } \
  61. while (0)
  62. /* Returns non-zero if error happened, zero if success. */
  63. #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val) \
  64. ({ \
  65. INTERNAL_SYSCALL_DECL (__err); \
  66. long int __ret; \
  67. __ret = INTERNAL_SYSCALL (futex, __err, 6, \
  68. (futexp), FUTEX_CMP_REQUEUE, (nr_wake), \
  69. (nr_move), (mutex), (val)); \
  70. __ret; \
  71. })
  72. /* Returns non-zero if error happened, zero if success. */
  73. #define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2) \
  74. ({ \
  75. INTERNAL_SYSCALL_DECL (__err); \
  76. long int __ret; \
  77. __ret = INTERNAL_SYSCALL (futex, __err, 6, \
  78. (futexp), FUTEX_WAKE_OP, (nr_wake), \
  79. (nr_wake2), (futexp2), \
  80. FUTEX_OP_CLEAR_WAKE_IF_GT_ONE); \
  81. __ret; \
  82. })
  83. static inline int __attribute__((always_inline))
  84. __lll_mutex_trylock (int *futex)
  85. {
  86. int flag = 1, old;
  87. #ifdef __thumb__
  88. old = atomic_exchange_acq (futex, flag);
  89. if (old < 1)
  90. flag = 0;
  91. else if (old > 1)
  92. flag = atomic_exchange_acq (futex, old);
  93. #else
  94. __asm__ __volatile__ (
  95. "\tswp %[old], %[flag], [%[futex]] @ try to take the lock\n"
  96. "\tcmp %[old], #1 @ check old lock value\n"
  97. "\tmovlo %[flag], #0 @ if we got it, return 0\n"
  98. "\tswphi %[flag], %[old], [%[futex]] @ if it was contested,\n"
  99. " @ restore the contested flag,\n"
  100. " @ and check whether that won."
  101. : [futex] "+&r" (futex), [flag] "+&r" (flag), [old] "=&r" (old)
  102. : : "memory" );
  103. #endif
  104. return flag;
  105. }
  106. #define lll_mutex_trylock(lock) __lll_mutex_trylock (&(lock))
  107. static inline int __attribute__((always_inline))
  108. __lll_mutex_cond_trylock (int *futex)
  109. {
  110. int flag = 2, old;
  111. #ifdef __thumb__
  112. old = atomic_exchange_acq (futex, flag);
  113. if (old < 1)
  114. flag = 0;
  115. else if (old > 1)
  116. flag = atomic_exchange_acq (futex, old);
  117. #else
  118. __asm__ __volatile__ (
  119. "\tswp %[old], %[flag], [%[futex]] @ try to take the lock\n"
  120. "\tcmp %[old], #1 @ check old lock value\n"
  121. "\tmovlo %[flag], #0 @ if we got it, return 0\n"
  122. "\tswphi %[flag], %[old], [%[futex]] @ if it was contested,\n"
  123. " @ restore the contested flag,\n"
  124. " @ and check whether that won."
  125. : [futex] "+&r" (futex), [flag] "+&r" (flag), [old] "=&r" (old)
  126. : : "memory" );
  127. #endif
  128. return flag;
  129. }
  130. #define lll_mutex_cond_trylock(lock) __lll_mutex_cond_trylock (&(lock))
  131. static inline int __attribute__((always_inline))
  132. __lll_robust_mutex_trylock(int *futex, int id)
  133. {
  134. return atomic_compare_and_exchange_val_acq (futex, id, 0) != 0;
  135. }
  136. #define lll_robust_mutex_trylock(lock, id) \
  137. __lll_robust_mutex_trylock (&(lock), id)
  138. extern int __lll_robust_lock_wait (int *futex) attribute_hidden;
  139. static inline void __attribute__((always_inline))
  140. __lll_mutex_lock (int *futex)
  141. {
  142. int val = atomic_exchange_acq (futex, 1);
  143. if (__builtin_expect (val != 0, 0))
  144. {
  145. while (atomic_exchange_acq (futex, 2) != 0)
  146. lll_futex_wait (futex, 2);
  147. }
  148. }
  149. #define lll_mutex_lock(futex) __lll_mutex_lock (&(futex))
  150. static inline int __attribute__ ((always_inline))
  151. __lll_robust_mutex_lock (int *futex, int id)
  152. {
  153. int result = 0;
  154. if (atomic_compare_and_exchange_bool_acq (futex, id, 0) != 0)
  155. result = __lll_robust_lock_wait (futex);
  156. return result;
  157. }
  158. #define lll_robust_mutex_lock(futex, id) \
  159. __lll_robust_mutex_lock (&(futex), id)
  160. static inline void __attribute__ ((always_inline))
  161. __lll_mutex_cond_lock (int *futex)
  162. {
  163. int val = atomic_exchange_acq (futex, 2);
  164. if (__builtin_expect (val != 0, 0))
  165. {
  166. while (atomic_exchange_acq (futex, 2) != 0)
  167. lll_futex_wait (futex, 2);
  168. }
  169. }
  170. #define lll_mutex_cond_lock(futex) __lll_mutex_cond_lock (&(futex))
  171. #define lll_robust_mutex_cond_lock(futex, id) \
  172. __lll_robust_mutex_lock (&(futex), (id) | FUTEX_WAITERS)
  173. extern int __lll_timedlock_wait (int *futex, const struct timespec *)
  174. attribute_hidden;
  175. extern int __lll_robust_timedlock_wait (int *futex, const struct timespec *)
  176. attribute_hidden;
  177. static inline int __attribute__ ((always_inline))
  178. __lll_mutex_timedlock (int *futex, const struct timespec *abstime)
  179. {
  180. int result = 0;
  181. int val = atomic_exchange_acq (futex, 1);
  182. if (__builtin_expect (val != 0, 0))
  183. result = __lll_timedlock_wait (futex, abstime);
  184. return result;
  185. }
  186. #define lll_mutex_timedlock(futex, abstime) \
  187. __lll_mutex_timedlock (&(futex), abstime)
  188. static inline int __attribute__ ((always_inline))
  189. __lll_robust_mutex_timedlock (int *futex, const struct timespec *abstime,
  190. int id)
  191. {
  192. int result = 0;
  193. if (atomic_compare_and_exchange_bool_acq (futex, id, 0) != 0)
  194. result = __lll_robust_timedlock_wait (futex, abstime);
  195. return result;
  196. }
  197. #define lll_robust_mutex_timedlock(futex, abstime, id) \
  198. __lll_robust_mutex_timedlock (&(futex), abstime, id)
  199. static inline void __attribute__ ((always_inline))
  200. __lll_mutex_unlock (int *futex)
  201. {
  202. int val = atomic_exchange_rel (futex, 0);
  203. if (__builtin_expect (val > 1, 0))
  204. lll_futex_wake (futex, 1);
  205. }
  206. #define lll_mutex_unlock(futex) __lll_mutex_unlock(&(futex))
  207. static inline void __attribute__ ((always_inline))
  208. __lll_robust_mutex_unlock (int *futex, int mask)
  209. {
  210. int val = atomic_exchange_rel (futex, 0);
  211. if (__builtin_expect (val & mask, 0))
  212. lll_futex_wake (futex, 1);
  213. }
  214. #define lll_robust_mutex_unlock(futex) \
  215. __lll_robust_mutex_unlock(&(futex), FUTEX_WAITERS)
  216. static inline void __attribute__ ((always_inline))
  217. __lll_mutex_unlock_force (int *futex)
  218. {
  219. (void) atomic_exchange_rel (futex, 0);
  220. lll_futex_wake (futex, 1);
  221. }
  222. #define lll_mutex_unlock_force(futex) __lll_mutex_unlock_force(&(futex))
  223. #define lll_mutex_islocked(futex) \
  224. (futex != 0)
  225. /* Our internal lock implementation is identical to the binary-compatible
  226. mutex implementation. */
  227. /* Type for lock object. */
  228. typedef int lll_lock_t;
  229. /* Initializers for lock. */
  230. #define LLL_LOCK_INITIALIZER (0)
  231. #define LLL_LOCK_INITIALIZER_LOCKED (1)
  232. extern int lll_unlock_wake_cb (int *__futex) attribute_hidden;
  233. /* The states of a lock are:
  234. 0 - untaken
  235. 1 - taken by one user
  236. >1 - taken by more users */
  237. #define lll_trylock(lock) lll_mutex_trylock (lock)
  238. #define lll_lock(lock) lll_mutex_lock (lock)
  239. #define lll_unlock(lock) lll_mutex_unlock (lock)
  240. #define lll_islocked(lock) lll_mutex_islocked (lock)
  241. /* The kernel notifies a process which uses CLONE_CLEARTID via futex
  242. wakeup when the clone terminates. The memory location contains the
  243. thread ID while the clone is running and is reset to zero
  244. afterwards. */
  245. #define lll_wait_tid(tid) \
  246. do { \
  247. __typeof (tid) __tid; \
  248. while ((__tid = (tid)) != 0) \
  249. lll_futex_wait (&(tid), __tid); \
  250. } while (0)
  251. extern int __lll_timedwait_tid (int *, const struct timespec *)
  252. attribute_hidden;
  253. #define lll_timedwait_tid(tid, abstime) \
  254. ({ \
  255. int __res = 0; \
  256. if ((tid) != 0) \
  257. __res = __lll_timedwait_tid (&(tid), (abstime)); \
  258. __res; \
  259. })
  260. /* Conditional variable handling. */
  261. extern void __lll_cond_wait (pthread_cond_t *cond)
  262. attribute_hidden;
  263. extern int __lll_cond_timedwait (pthread_cond_t *cond,
  264. const struct timespec *abstime)
  265. attribute_hidden;
  266. extern void __lll_cond_wake (pthread_cond_t *cond)
  267. attribute_hidden;
  268. extern void __lll_cond_broadcast (pthread_cond_t *cond)
  269. attribute_hidden;
  270. #define lll_cond_wait(cond) \
  271. __lll_cond_wait (cond)
  272. #define lll_cond_timedwait(cond, abstime) \
  273. __lll_cond_timedwait (cond, abstime)
  274. #define lll_cond_wake(cond) \
  275. __lll_cond_wake (cond)
  276. #define lll_cond_broadcast(cond) \
  277. __lll_cond_broadcast (cond)
  278. #endif /* lowlevellock.h */