futextest.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /******************************************************************************
  2. *
  3. * Copyright © International Business Machines Corp., 2009
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * DESCRIPTION
  11. * Glibc independent futex library for testing kernel functionality.
  12. *
  13. * AUTHOR
  14. * Darren Hart <dvhart@linux.intel.com>
  15. *
  16. * HISTORY
  17. * 2009-Nov-6: Initial version by Darren Hart <dvhart@linux.intel.com>
  18. *
  19. *****************************************************************************/
  20. #ifndef _FUTEXTEST_H
  21. #define _FUTEXTEST_H
  22. #include <unistd.h>
  23. #include <sys/syscall.h>
  24. #include <sys/types.h>
  25. #include <linux/futex.h>
  26. typedef volatile u_int32_t futex_t;
  27. #define FUTEX_INITIALIZER 0
  28. /* Define the newer op codes if the system header file is not up to date. */
  29. #ifndef FUTEX_WAIT_BITSET
  30. #define FUTEX_WAIT_BITSET 9
  31. #endif
  32. #ifndef FUTEX_WAKE_BITSET
  33. #define FUTEX_WAKE_BITSET 10
  34. #endif
  35. #ifndef FUTEX_WAIT_REQUEUE_PI
  36. #define FUTEX_WAIT_REQUEUE_PI 11
  37. #endif
  38. #ifndef FUTEX_CMP_REQUEUE_PI
  39. #define FUTEX_CMP_REQUEUE_PI 12
  40. #endif
  41. #ifndef FUTEX_WAIT_REQUEUE_PI_PRIVATE
  42. #define FUTEX_WAIT_REQUEUE_PI_PRIVATE (FUTEX_WAIT_REQUEUE_PI | \
  43. FUTEX_PRIVATE_FLAG)
  44. #endif
  45. #ifndef FUTEX_REQUEUE_PI_PRIVATE
  46. #define FUTEX_CMP_REQUEUE_PI_PRIVATE (FUTEX_CMP_REQUEUE_PI | \
  47. FUTEX_PRIVATE_FLAG)
  48. #endif
  49. /**
  50. * futex() - SYS_futex syscall wrapper
  51. * @uaddr: address of first futex
  52. * @op: futex op code
  53. * @val: typically expected value of uaddr, but varies by op
  54. * @timeout: typically an absolute struct timespec (except where noted
  55. * otherwise). Overloaded by some ops
  56. * @uaddr2: address of second futex for some ops\
  57. * @val3: varies by op
  58. * @opflags: flags to be bitwise OR'd with op, such as FUTEX_PRIVATE_FLAG
  59. *
  60. * futex() is used by all the following futex op wrappers. It can also be
  61. * used for misuse and abuse testing. Generally, the specific op wrappers
  62. * should be used instead. It is a macro instead of an static inline function as
  63. * some of the types over overloaded (timeout is used for nr_requeue for
  64. * example).
  65. *
  66. * These argument descriptions are the defaults for all
  67. * like-named arguments in the following wrappers except where noted below.
  68. */
  69. #define futex(uaddr, op, val, timeout, uaddr2, val3, opflags) \
  70. syscall(SYS_futex, uaddr, op | opflags, val, timeout, uaddr2, val3)
  71. /**
  72. * futex_wait() - block on uaddr with optional timeout
  73. * @timeout: relative timeout
  74. */
  75. static inline int
  76. futex_wait(futex_t *uaddr, futex_t val, struct timespec *timeout, int opflags)
  77. {
  78. return futex(uaddr, FUTEX_WAIT, val, timeout, NULL, 0, opflags);
  79. }
  80. /**
  81. * futex_wake() - wake one or more tasks blocked on uaddr
  82. * @nr_wake: wake up to this many tasks
  83. */
  84. static inline int
  85. futex_wake(futex_t *uaddr, int nr_wake, int opflags)
  86. {
  87. return futex(uaddr, FUTEX_WAKE, nr_wake, NULL, NULL, 0, opflags);
  88. }
  89. /**
  90. * futex_wait_bitset() - block on uaddr with bitset
  91. * @bitset: bitset to be used with futex_wake_bitset
  92. */
  93. static inline int
  94. futex_wait_bitset(futex_t *uaddr, futex_t val, struct timespec *timeout,
  95. u_int32_t bitset, int opflags)
  96. {
  97. return futex(uaddr, FUTEX_WAIT_BITSET, val, timeout, NULL, bitset,
  98. opflags);
  99. }
  100. /**
  101. * futex_wake_bitset() - wake one or more tasks blocked on uaddr with bitset
  102. * @bitset: bitset to compare with that used in futex_wait_bitset
  103. */
  104. static inline int
  105. futex_wake_bitset(futex_t *uaddr, int nr_wake, u_int32_t bitset, int opflags)
  106. {
  107. return futex(uaddr, FUTEX_WAKE_BITSET, nr_wake, NULL, NULL, bitset,
  108. opflags);
  109. }
  110. /**
  111. * futex_lock_pi() - block on uaddr as a PI mutex
  112. * @detect: whether (1) or not (0) to perform deadlock detection
  113. */
  114. static inline int
  115. futex_lock_pi(futex_t *uaddr, struct timespec *timeout, int detect,
  116. int opflags)
  117. {
  118. return futex(uaddr, FUTEX_LOCK_PI, detect, timeout, NULL, 0, opflags);
  119. }
  120. /**
  121. * futex_unlock_pi() - release uaddr as a PI mutex, waking the top waiter
  122. */
  123. static inline int
  124. futex_unlock_pi(futex_t *uaddr, int opflags)
  125. {
  126. return futex(uaddr, FUTEX_UNLOCK_PI, 0, NULL, NULL, 0, opflags);
  127. }
  128. /**
  129. * futex_wake_op() - FIXME: COME UP WITH A GOOD ONE LINE DESCRIPTION
  130. */
  131. static inline int
  132. futex_wake_op(futex_t *uaddr, futex_t *uaddr2, int nr_wake, int nr_wake2,
  133. int wake_op, int opflags)
  134. {
  135. return futex(uaddr, FUTEX_WAKE_OP, nr_wake, nr_wake2, uaddr2, wake_op,
  136. opflags);
  137. }
  138. /**
  139. * futex_requeue() - requeue without expected value comparison, deprecated
  140. * @nr_wake: wake up to this many tasks
  141. * @nr_requeue: requeue up to this many tasks
  142. *
  143. * Due to its inherently racy implementation, futex_requeue() is deprecated in
  144. * favor of futex_cmp_requeue().
  145. */
  146. static inline int
  147. futex_requeue(futex_t *uaddr, futex_t *uaddr2, int nr_wake, int nr_requeue,
  148. int opflags)
  149. {
  150. return futex(uaddr, FUTEX_REQUEUE, nr_wake, nr_requeue, uaddr2, 0,
  151. opflags);
  152. }
  153. /**
  154. * futex_cmp_requeue() - requeue tasks from uaddr to uaddr2
  155. * @nr_wake: wake up to this many tasks
  156. * @nr_requeue: requeue up to this many tasks
  157. */
  158. static inline int
  159. futex_cmp_requeue(futex_t *uaddr, futex_t val, futex_t *uaddr2, int nr_wake,
  160. int nr_requeue, int opflags)
  161. {
  162. return futex(uaddr, FUTEX_CMP_REQUEUE, nr_wake, nr_requeue, uaddr2,
  163. val, opflags);
  164. }
  165. /**
  166. * futex_wait_requeue_pi() - block on uaddr and prepare to requeue to uaddr2
  167. * @uaddr: non-PI futex source
  168. * @uaddr2: PI futex target
  169. *
  170. * This is the first half of the requeue_pi mechanism. It shall always be
  171. * paired with futex_cmp_requeue_pi().
  172. */
  173. static inline int
  174. futex_wait_requeue_pi(futex_t *uaddr, futex_t val, futex_t *uaddr2,
  175. struct timespec *timeout, int opflags)
  176. {
  177. return futex(uaddr, FUTEX_WAIT_REQUEUE_PI, val, timeout, uaddr2, 0,
  178. opflags);
  179. }
  180. /**
  181. * futex_cmp_requeue_pi() - requeue tasks from uaddr to uaddr2 (PI aware)
  182. * @uaddr: non-PI futex source
  183. * @uaddr2: PI futex target
  184. * @nr_wake: wake up to this many tasks
  185. * @nr_requeue: requeue up to this many tasks
  186. */
  187. static inline int
  188. futex_cmp_requeue_pi(futex_t *uaddr, futex_t val, futex_t *uaddr2, int nr_wake,
  189. int nr_requeue, int opflags)
  190. {
  191. return futex(uaddr, FUTEX_CMP_REQUEUE_PI, nr_wake, nr_requeue, uaddr2,
  192. val, opflags);
  193. }
  194. /**
  195. * futex_cmpxchg() - atomic compare and exchange
  196. * @uaddr: The address of the futex to be modified
  197. * @oldval: The expected value of the futex
  198. * @newval: The new value to try and assign the futex
  199. *
  200. * Implement cmpxchg using gcc atomic builtins.
  201. * http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Atomic-Builtins.html
  202. *
  203. * Return the old futex value.
  204. */
  205. static inline u_int32_t
  206. futex_cmpxchg(futex_t *uaddr, u_int32_t oldval, u_int32_t newval)
  207. {
  208. return __sync_val_compare_and_swap(uaddr, oldval, newval);
  209. }
  210. /**
  211. * futex_dec() - atomic decrement of the futex value
  212. * @uaddr: The address of the futex to be modified
  213. *
  214. * Return the new futex value.
  215. */
  216. static inline u_int32_t
  217. futex_dec(futex_t *uaddr)
  218. {
  219. return __sync_sub_and_fetch(uaddr, 1);
  220. }
  221. /**
  222. * futex_inc() - atomic increment of the futex value
  223. * @uaddr: the address of the futex to be modified
  224. *
  225. * Return the new futex value.
  226. */
  227. static inline u_int32_t
  228. futex_inc(futex_t *uaddr)
  229. {
  230. return __sync_add_and_fetch(uaddr, 1);
  231. }
  232. /**
  233. * futex_set() - atomic decrement of the futex value
  234. * @uaddr: the address of the futex to be modified
  235. * @newval: New value for the atomic_t
  236. *
  237. * Return the new futex value.
  238. */
  239. static inline u_int32_t
  240. futex_set(futex_t *uaddr, u_int32_t newval)
  241. {
  242. *uaddr = newval;
  243. return newval;
  244. }
  245. #endif