lowlevellock-futex.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* Low-level locking access to futex facilities. Linux version.
  2. Copyright (C) 2005-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  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, see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef _LOWLEVELLOCK_FUTEX_H
  16. #define _LOWLEVELLOCK_FUTEX_H 1
  17. #ifndef __ASSEMBLER__
  18. #include <sysdep.h>
  19. #include <tls.h>
  20. #include <kernel-features.h>
  21. #endif
  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. #define FUTEX_LOCK_PI 6
  29. #define FUTEX_UNLOCK_PI 7
  30. #define FUTEX_TRYLOCK_PI 8
  31. #define FUTEX_WAIT_BITSET 9
  32. #define FUTEX_WAKE_BITSET 10
  33. #define FUTEX_WAIT_REQUEUE_PI 11
  34. #define FUTEX_CMP_REQUEUE_PI 12
  35. #define FUTEX_PRIVATE_FLAG 128
  36. #define FUTEX_CLOCK_REALTIME 256
  37. #define FUTEX_BITSET_MATCH_ANY 0xffffffff
  38. /* Values for 'private' parameter of locking macros. Yes, the
  39. definition seems to be backwards. But it is not. The bit will be
  40. reversed before passing to the system call. */
  41. #define LLL_PRIVATE 0
  42. #define LLL_SHARED FUTEX_PRIVATE_FLAG
  43. #ifndef __ASSEMBLER__
  44. #if IS_IN (libc) || IS_IN (rtld)
  45. /* In libc.so or ld.so all futexes are private. */
  46. # define __lll_private_flag(fl, private) \
  47. ({ \
  48. /* Prevent warnings in callers of this macro. */ \
  49. int __lll_private_flag_priv __attribute__ ((unused)); \
  50. __lll_private_flag_priv = (private); \
  51. ((fl) | FUTEX_PRIVATE_FLAG); \
  52. })
  53. #else
  54. # define __lll_private_flag(fl, private) \
  55. (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
  56. #endif
  57. #define lll_futex_syscall(nargs, futexp, op, ...) \
  58. ({ \
  59. INTERNAL_SYSCALL_DECL (__err); \
  60. long int __ret = INTERNAL_SYSCALL (futex, __err, nargs, futexp, op, \
  61. __VA_ARGS__); \
  62. (__glibc_unlikely (INTERNAL_SYSCALL_ERROR_P (__ret, __err)) \
  63. ? -INTERNAL_SYSCALL_ERRNO (__ret, __err) : 0); \
  64. })
  65. #define lll_futex_wait(futexp, val, private) \
  66. lll_futex_timed_wait (futexp, val, NULL, private)
  67. #define lll_futex_timed_wait(futexp, val, timeout, private) \
  68. lll_futex_syscall (4, futexp, \
  69. __lll_private_flag (FUTEX_WAIT, private), \
  70. val, timeout)
  71. #define lll_futex_timed_wait_bitset(futexp, val, timeout, clockbit, private) \
  72. lll_futex_syscall (6, futexp, \
  73. __lll_private_flag (FUTEX_WAIT_BITSET | (clockbit), \
  74. private), \
  75. val, timeout, NULL /* Unused. */, \
  76. FUTEX_BITSET_MATCH_ANY)
  77. #define lll_futex_wake(futexp, nr, private) \
  78. lll_futex_syscall (4, futexp, \
  79. __lll_private_flag (FUTEX_WAKE, private), nr, 0)
  80. /* Returns non-zero if error happened, zero if success. */
  81. #define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val, private) \
  82. lll_futex_syscall (6, futexp, \
  83. __lll_private_flag (FUTEX_CMP_REQUEUE, private), \
  84. nr_wake, nr_move, mutex, val)
  85. /* Returns non-zero if error happened, zero if success. */
  86. #define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2, private) \
  87. lll_futex_syscall (6, futexp, \
  88. __lll_private_flag (FUTEX_WAKE_OP, private), \
  89. nr_wake, nr_wake2, futexp2, \
  90. FUTEX_OP_CLEAR_WAKE_IF_GT_ONE)
  91. /* Priority Inheritance support. */
  92. #define lll_futex_wait_requeue_pi(futexp, val, mutex, private) \
  93. lll_futex_timed_wait_requeue_pi (futexp, val, NULL, 0, mutex, private)
  94. #define lll_futex_timed_wait_requeue_pi(futexp, val, timeout, clockbit, \
  95. mutex, private) \
  96. lll_futex_syscall (5, futexp, \
  97. __lll_private_flag (FUTEX_WAIT_REQUEUE_PI \
  98. | (clockbit), private), \
  99. val, timeout, mutex)
  100. #define lll_futex_cmp_requeue_pi(futexp, nr_wake, nr_move, mutex, \
  101. val, private) \
  102. lll_futex_syscall (6, futexp, \
  103. __lll_private_flag (FUTEX_CMP_REQUEUE_PI, \
  104. private), \
  105. nr_wake, nr_move, mutex, val)
  106. /* Cancellable futex macros. */
  107. #define lll_futex_wait_cancel(futexp, val, private) \
  108. ({ \
  109. int __oldtype = CANCEL_ASYNC (); \
  110. long int __err = lll_futex_wait (futexp, val, LLL_SHARED); \
  111. CANCEL_RESET (__oldtype); \
  112. __err; \
  113. })
  114. #endif /* !__ASSEMBLER__ */
  115. #endif /* lowlevellock-futex.h */