hurdlock.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /* Hurd helpers for lowlevellocks.
  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. #include "hurdlock.h"
  16. #include <hurd.h>
  17. #include <hurd/hurd.h>
  18. #include <time.h>
  19. #include <errno.h>
  20. #include <unistd.h>
  21. /* Convert an absolute timeout in nanoseconds to a relative
  22. timeout in milliseconds. */
  23. static inline int __attribute__ ((gnu_inline))
  24. compute_reltime (const struct timespec *abstime, clockid_t clk)
  25. {
  26. struct timespec ts;
  27. __clock_gettime (clk, &ts);
  28. ts.tv_sec = abstime->tv_sec - ts.tv_sec;
  29. ts.tv_nsec = abstime->tv_nsec - ts.tv_nsec;
  30. if (ts.tv_nsec < 0)
  31. {
  32. --ts.tv_sec;
  33. ts.tv_nsec += 1000000000;
  34. }
  35. return ts.tv_sec < 0 ? -1 : (int)(ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
  36. }
  37. int
  38. __lll_abstimed_wait (void *ptr, int val,
  39. const struct timespec *tsp, int flags, int clk)
  40. {
  41. if (clk != CLOCK_REALTIME && clk != CLOCK_MONOTONIC)
  42. return EINVAL;
  43. int mlsec = compute_reltime (tsp, clk);
  44. return mlsec < 0 ? KERN_TIMEDOUT : __lll_timed_wait (ptr, val, mlsec, flags);
  45. }
  46. int
  47. __lll_abstimed_wait_intr (void *ptr, int val,
  48. const struct timespec *tsp, int flags, int clk)
  49. {
  50. if (clk != CLOCK_REALTIME && clk != CLOCK_MONOTONIC)
  51. return EINVAL;
  52. int mlsec = compute_reltime (tsp, clk);
  53. return mlsec < 0 ? KERN_TIMEDOUT : __lll_timed_wait_intr (ptr, val, mlsec, flags);
  54. }
  55. libc_hidden_def (__lll_abstimed_wait_intr)
  56. int
  57. __lll_abstimed_xwait (void *ptr, int lo, int hi,
  58. const struct timespec *tsp, int flags, int clk)
  59. {
  60. if (clk != CLOCK_REALTIME && clk != CLOCK_MONOTONIC)
  61. return EINVAL;
  62. int mlsec = compute_reltime (tsp, clk);
  63. return mlsec < 0 ? KERN_TIMEDOUT : __lll_timed_xwait (ptr, lo, hi, mlsec,
  64. flags);
  65. }
  66. int
  67. __lll_abstimed_lock (void *ptr,
  68. const struct timespec *tsp, int flags, int clk)
  69. {
  70. if (clk != CLOCK_REALTIME && clk != CLOCK_MONOTONIC)
  71. return EINVAL;
  72. if (__lll_trylock (ptr) == 0)
  73. return 0;
  74. while (1)
  75. {
  76. if (atomic_exchange_acquire ((int *)ptr, 2) == 0)
  77. return 0;
  78. else if (! valid_nanoseconds (tsp->tv_nsec))
  79. return EINVAL;
  80. int mlsec = compute_reltime (tsp, clk);
  81. if (mlsec < 0 || __lll_timed_wait (ptr, 2, mlsec, flags) == KERN_TIMEDOUT)
  82. return ETIMEDOUT;
  83. }
  84. }
  85. libc_hidden_def (__lll_abstimed_lock)
  86. /* Robust locks. */
  87. /* Test if a given process id is still valid. */
  88. static inline int
  89. valid_pid (int pid)
  90. {
  91. task_t task = __pid2task (pid);
  92. if (task == MACH_PORT_NULL)
  93. return 0;
  94. __mach_port_deallocate (__mach_task_self (), task);
  95. return 1;
  96. }
  97. /* Robust locks have currently no support from the kernel; they
  98. are simply implemented with periodic polling. When sleeping, the
  99. maximum blocking time is determined by this constant. */
  100. #define MAX_WAIT_TIME 1500
  101. int
  102. __lll_robust_lock (void *ptr, int flags)
  103. {
  104. int *iptr = (int *)ptr;
  105. int id = __getpid ();
  106. int wait_time = 25;
  107. unsigned int val;
  108. /* Try to set the lock word to our PID if it's clear. Otherwise,
  109. mark it as having waiters. */
  110. while (1)
  111. {
  112. val = *iptr;
  113. if (!val && atomic_compare_and_exchange_bool_acq (iptr, id, 0) == 0)
  114. return 0;
  115. else if (atomic_compare_and_exchange_bool_acq (iptr,
  116. val | LLL_WAITERS, val) == 0)
  117. break;
  118. }
  119. for (id |= LLL_WAITERS ; ; )
  120. {
  121. val = *iptr;
  122. if (!val && atomic_compare_and_exchange_bool_acq (iptr, id, 0) == 0)
  123. return 0;
  124. else if (val && !valid_pid (val & LLL_OWNER_MASK))
  125. {
  126. if (atomic_compare_and_exchange_bool_acq (iptr, id, val) == 0)
  127. return EOWNERDEAD;
  128. }
  129. else
  130. {
  131. __lll_timed_wait (iptr, val, wait_time, flags);
  132. if (wait_time < MAX_WAIT_TIME)
  133. wait_time <<= 1;
  134. }
  135. }
  136. }
  137. libc_hidden_def (__lll_robust_lock)
  138. int
  139. __lll_robust_abstimed_lock (void *ptr,
  140. const struct timespec *tsp, int flags, int clk)
  141. {
  142. int *iptr = (int *)ptr;
  143. int id = __getpid ();
  144. int wait_time = 25;
  145. unsigned int val;
  146. if (clk != CLOCK_REALTIME && clk != CLOCK_MONOTONIC)
  147. return EINVAL;
  148. while (1)
  149. {
  150. val = *iptr;
  151. if (!val && atomic_compare_and_exchange_bool_acq (iptr, id, 0) == 0)
  152. return 0;
  153. else if (atomic_compare_and_exchange_bool_acq (iptr,
  154. val | LLL_WAITERS, val) == 0)
  155. break;
  156. }
  157. for (id |= LLL_WAITERS ; ; )
  158. {
  159. val = *iptr;
  160. if (!val && atomic_compare_and_exchange_bool_acq (iptr, id, 0) == 0)
  161. return 0;
  162. else if (val && !valid_pid (val & LLL_OWNER_MASK))
  163. {
  164. if (atomic_compare_and_exchange_bool_acq (iptr, id, val) == 0)
  165. return EOWNERDEAD;
  166. }
  167. else
  168. {
  169. int mlsec = compute_reltime (tsp, clk);
  170. if (mlsec < 0)
  171. return ETIMEDOUT;
  172. else if (mlsec > wait_time)
  173. mlsec = wait_time;
  174. int res = __lll_timed_wait (iptr, val, mlsec, flags);
  175. if (res == KERN_TIMEDOUT)
  176. return ETIMEDOUT;
  177. else if (wait_time < MAX_WAIT_TIME)
  178. wait_time <<= 1;
  179. }
  180. }
  181. }
  182. libc_hidden_def (__lll_robust_abstimed_lock)
  183. int
  184. __lll_robust_trylock (void *ptr)
  185. {
  186. int *iptr = (int *)ptr;
  187. int id = __getpid ();
  188. unsigned int val = *iptr;
  189. if (!val)
  190. {
  191. if (atomic_compare_and_exchange_bool_acq (iptr, id, 0) == 0)
  192. return 0;
  193. }
  194. else if (!valid_pid (val & LLL_OWNER_MASK)
  195. && atomic_compare_and_exchange_bool_acq (iptr, id, val) == 0)
  196. return EOWNERDEAD;
  197. return EBUSY;
  198. }
  199. libc_hidden_def (__lll_robust_trylock)
  200. void
  201. __lll_robust_unlock (void *ptr, int flags)
  202. {
  203. unsigned int val = atomic_load_relaxed ((unsigned int *)ptr);
  204. while (1)
  205. {
  206. if (val & LLL_WAITERS)
  207. {
  208. __lll_set_wake (ptr, 0, flags);
  209. break;
  210. }
  211. else if (atomic_compare_exchange_weak_release ((unsigned int *)ptr, &val, 0))
  212. break;
  213. }
  214. }
  215. libc_hidden_def (__lll_robust_unlock)