hurdlock.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* Low-level lock implementation. High-level Hurd helpers.
  2. Copyright (C) 1999-2026 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. <https://www.gnu.org/licenses/>. */
  15. #ifndef _HURD_LOCK_H
  16. #define _HURD_LOCK_H 1
  17. #include <mach/lowlevellock.h>
  18. struct timespec;
  19. /* Flags for robust locks. */
  20. #define LLL_WAITERS (1U << 31)
  21. #define LLL_DEAD_OWNER (1U << 30)
  22. #define LLL_OWNER_MASK ~(LLL_WAITERS | LLL_DEAD_OWNER)
  23. /* Wait on 64-bit address PTR, without blocking if its contents
  24. are different from the pair <LO, HI>. */
  25. #define __lll_xwait(ptr, lo, hi, flags) \
  26. __gsync_wait (__mach_task_self (), \
  27. (vm_offset_t)ptr, lo, hi, 0, flags | GSYNC_QUAD)
  28. /* Same as '__lll_wait', but only block for MLSEC milliseconds. */
  29. #define __lll_timed_wait(ptr, val, mlsec, flags) \
  30. __gsync_wait (__mach_task_self (), \
  31. (vm_offset_t)ptr, val, 0, mlsec, flags | GSYNC_TIMED)
  32. /* Interruptible version. */
  33. #define __lll_timed_wait_intr(ptr, val, mlsec, flags) \
  34. __gsync_wait_intr (__mach_task_self (), \
  35. (vm_offset_t)ptr, val, 0, mlsec, flags | GSYNC_TIMED)
  36. /* Same as '__lll_xwait', but only block for MLSEC milliseconds. */
  37. #define __lll_timed_xwait(ptr, lo, hi, mlsec, flags) \
  38. __gsync_wait (__mach_task_self (), (vm_offset_t)ptr, \
  39. lo, hi, mlsec, flags | GSYNC_TIMED | GSYNC_QUAD)
  40. /* Same as '__lll_wait', but only block until TSP elapses,
  41. using clock CLK. */
  42. extern int __lll_abstimed_wait (void *__ptr, int __val,
  43. const struct timespec *__tsp, int __flags, int __clk);
  44. /* Interruptible version. */
  45. extern int __lll_abstimed_wait_intr (void *__ptr, int __val,
  46. const struct timespec *__tsp, int __flags, int __clk);
  47. libc_hidden_proto (__lll_abstimed_wait_intr)
  48. /* Same as 'lll_xwait', but only block until TSP elapses,
  49. using clock CLK. */
  50. extern int __lll_abstimed_xwait (void *__ptr, int __lo, int __hi,
  51. const struct timespec *__tsp, int __flags, int __clk);
  52. /* Same as 'lll_lock', but return with an error if TSP elapses,
  53. using clock CLK. */
  54. extern int __lll_abstimed_lock (void *__ptr,
  55. const struct timespec *__tsp, int __flags, int __clk);
  56. libc_hidden_proto (__lll_abstimed_lock)
  57. /* Acquire the lock at PTR, but return with an error if
  58. the process containing the owner thread dies. */
  59. extern int __lll_robust_lock (void *__ptr, int __flags);
  60. #define lll_robust_lock(var, flags) \
  61. __lll_robust_lock (&(var), flags)
  62. libc_hidden_proto (__lll_robust_lock)
  63. /* Same as '__lll_robust_lock', but only block until TSP
  64. elapses, using clock CLK. */
  65. extern int __lll_robust_abstimed_lock (void *__ptr,
  66. const struct timespec *__tsp, int __flags, int __clk);
  67. libc_hidden_proto (__lll_robust_abstimed_lock)
  68. /* Same as '__lll_robust_lock', but return with an error
  69. if the lock cannot be acquired without blocking. */
  70. extern int __lll_robust_trylock (void *__ptr);
  71. #define lll_robust_trylock(var) \
  72. __lll_robust_trylock (&(var))
  73. libc_hidden_proto (__lll_robust_trylock)
  74. /* Wake one or more threads waiting on address PTR,
  75. setting its value to VAL before doing so. */
  76. #define __lll_set_wake(ptr, val, flags) \
  77. __gsync_wake (__mach_task_self (), \
  78. (vm_offset_t)ptr, val, flags | GSYNC_MUTATE)
  79. /* Release the robust lock at PTR. */
  80. extern void __lll_robust_unlock (void *__ptr, int __flags);
  81. #define lll_robust_unlock(var, flags) \
  82. __lll_robust_unlock (&(var), flags)
  83. libc_hidden_proto (__lll_robust_unlock)
  84. /* Rearrange threads waiting on address SRC to instead wait on
  85. DST, waking one of them if WAIT_ONE is non-zero. */
  86. #define __lll_requeue(src, dst, wake_one, flags) \
  87. __gsync_requeue (__mach_task_self (), (vm_offset_t)src, \
  88. (vm_offset_t)dst, (boolean_t)wake_one, flags)
  89. /* The following are hacks that allow us to simulate optional
  90. parameters in C, to avoid having to pass the clock id for
  91. every one of these calls, defaulting to CLOCK_REALTIME if
  92. no argument is passed. */
  93. #define lll_abstimed_wait(var, val, tsp, flags, ...) \
  94. ({ \
  95. const clockid_t __clk[] = { CLOCK_REALTIME, ##__VA_ARGS__ }; \
  96. __lll_abstimed_wait (&(var), (val), (tsp), (flags), \
  97. __clk[sizeof (__clk) / sizeof (__clk[0]) - 1]); \
  98. })
  99. #define lll_abstimed_wait_intr(var, val, tsp, flags, ...) \
  100. ({ \
  101. const clockid_t __clk[] = { CLOCK_REALTIME, ##__VA_ARGS__ }; \
  102. __lll_abstimed_wait_intr (&(var), (val), (tsp), (flags), \
  103. __clk[sizeof (__clk) / sizeof (__clk[0]) - 1]); \
  104. })
  105. #define lll_abstimed_xwait(var, lo, hi, tsp, flags, ...) \
  106. ({ \
  107. const clockid_t __clk[] = { CLOCK_REALTIME, ##__VA_ARGS__ }; \
  108. __lll_abstimed_xwait (&(var), (lo), (hi), (tsp), (flags), \
  109. __clk[sizeof (__clk) / sizeof (__clk[0]) - 1]); \
  110. })
  111. #define lll_abstimed_lock(var, tsp, flags, ...) \
  112. ({ \
  113. const clockid_t __clk[] = { CLOCK_REALTIME, ##__VA_ARGS__ }; \
  114. __lll_abstimed_lock (&(var), (tsp), (flags), \
  115. __clk[sizeof (__clk) / sizeof (__clk[0]) - 1]); \
  116. })
  117. #define lll_robust_abstimed_lock(var, tsp, flags, ...) \
  118. ({ \
  119. const clockid_t __clk[] = { CLOCK_REALTIME, ##__VA_ARGS__ }; \
  120. __lll_robust_abstimed_lock (&(var), (tsp), (flags), \
  121. __clk[sizeof (__clk) / sizeof (__clk[0]) - 1]); \
  122. })
  123. #endif