arm: remove string/tst-memmove-overflow XFAIL
[glibc.git] / hurd / hurdlock.c
blob59d017fc02672719105731ef2ee548859dfec704
1 /* Hurd helpers for lowlevellocks.
2 Copyright (C) 1999-2020 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include "hurdlock.h"
20 #include <hurd.h>
21 #include <hurd/hurd.h>
22 #include <time.h>
23 #include <errno.h>
24 #include <unistd.h>
26 /* Convert an absolute timeout in nanoseconds to a relative
27 timeout in milliseconds. */
28 static inline int __attribute__ ((gnu_inline))
29 compute_reltime (const struct timespec *abstime, clockid_t clk)
31 struct timespec ts;
32 __clock_gettime (clk, &ts);
34 ts.tv_sec = abstime->tv_sec - ts.tv_sec;
35 ts.tv_nsec = abstime->tv_nsec - ts.tv_nsec;
37 if (ts.tv_nsec < 0)
39 --ts.tv_sec;
40 ts.tv_nsec += 1000000000;
43 return ts.tv_sec < 0 ? -1 : (int)(ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
46 int
47 __lll_abstimed_wait (void *ptr, int val,
48 const struct timespec *tsp, int flags, int clk)
50 if (clk != CLOCK_REALTIME)
51 return EINVAL;
53 int mlsec = compute_reltime (tsp, clk);
54 return mlsec < 0 ? KERN_TIMEDOUT : lll_timed_wait (ptr, val, mlsec, flags);
57 int
58 __lll_abstimed_xwait (void *ptr, int lo, int hi,
59 const struct timespec *tsp, int flags, int clk)
61 if (clk != CLOCK_REALTIME)
62 return EINVAL;
64 int mlsec = compute_reltime (tsp, clk);
65 return mlsec < 0 ? KERN_TIMEDOUT : lll_timed_xwait (ptr, lo, hi, mlsec,
66 flags);
69 int
70 __lll_abstimed_lock (void *ptr,
71 const struct timespec *tsp, int flags, int clk)
73 if (clk != CLOCK_REALTIME)
74 return EINVAL;
76 if (lll_trylock (ptr) == 0)
77 return 0;
79 while (1)
81 if (atomic_exchange_acq ((int *)ptr, 2) == 0)
82 return 0;
83 else if (! valid_nanoseconds (tsp->tv_nsec))
84 return EINVAL;
86 int mlsec = compute_reltime (tsp, clk);
87 if (mlsec < 0 || lll_timed_wait (ptr, 2, mlsec, flags) == KERN_TIMEDOUT)
88 return ETIMEDOUT;
92 /* Robust locks. */
94 /* Test if a given process id is still valid. */
95 static inline int
96 valid_pid (int pid)
98 task_t task = __pid2task (pid);
99 if (task == MACH_PORT_NULL)
100 return 0;
102 __mach_port_deallocate (__mach_task_self (), task);
103 return 1;
106 /* Robust locks have currently no support from the kernel; they
107 are simply implemented with periodic polling. When sleeping, the
108 maximum blocking time is determined by this constant. */
109 #define MAX_WAIT_TIME 1500
112 __lll_robust_lock (void *ptr, int flags)
114 int *iptr = (int *)ptr;
115 int id = __getpid ();
116 int wait_time = 25;
117 unsigned int val;
119 /* Try to set the lock word to our PID if it's clear. Otherwise,
120 mark it as having waiters. */
121 while (1)
123 val = *iptr;
124 if (!val && atomic_compare_and_exchange_bool_acq (iptr, id, 0) == 0)
125 return 0;
126 else if (atomic_compare_and_exchange_bool_acq (iptr,
127 val | LLL_WAITERS, val) == 0)
128 break;
131 for (id |= LLL_WAITERS ; ; )
133 val = *iptr;
134 if (!val && atomic_compare_and_exchange_bool_acq (iptr, id, 0) == 0)
135 return 0;
136 else if (val && !valid_pid (val & LLL_OWNER_MASK))
138 if (atomic_compare_and_exchange_bool_acq (iptr, id, val) == 0)
139 return EOWNERDEAD;
141 else
143 lll_timed_wait (iptr, val, wait_time, flags);
144 if (wait_time < MAX_WAIT_TIME)
145 wait_time <<= 1;
151 __lll_robust_abstimed_lock (void *ptr,
152 const struct timespec *tsp, int flags, int clk)
154 int *iptr = (int *)ptr;
155 int id = __getpid ();
156 int wait_time = 25;
157 unsigned int val;
159 if (clk != CLOCK_REALTIME)
160 return EINVAL;
162 while (1)
164 val = *iptr;
165 if (!val && atomic_compare_and_exchange_bool_acq (iptr, id, 0) == 0)
166 return 0;
167 else if (atomic_compare_and_exchange_bool_acq (iptr,
168 val | LLL_WAITERS, val) == 0)
169 break;
172 for (id |= LLL_WAITERS ; ; )
174 val = *iptr;
175 if (!val && atomic_compare_and_exchange_bool_acq (iptr, id, 0) == 0)
176 return 0;
177 else if (val && !valid_pid (val & LLL_OWNER_MASK))
179 if (atomic_compare_and_exchange_bool_acq (iptr, id, val) == 0)
180 return EOWNERDEAD;
182 else
184 int mlsec = compute_reltime (tsp, clk);
185 if (mlsec < 0)
186 return ETIMEDOUT;
187 else if (mlsec > wait_time)
188 mlsec = wait_time;
190 int res = lll_timed_wait (iptr, val, mlsec, flags);
191 if (res == KERN_TIMEDOUT)
192 return ETIMEDOUT;
193 else if (wait_time < MAX_WAIT_TIME)
194 wait_time <<= 1;
200 __lll_robust_trylock (void *ptr)
202 int *iptr = (int *)ptr;
203 int id = __getpid ();
204 unsigned int val = *iptr;
206 if (!val)
208 if (atomic_compare_and_exchange_bool_acq (iptr, id, 0) == 0)
209 return 0;
211 else if (!valid_pid (val & LLL_OWNER_MASK)
212 && atomic_compare_and_exchange_bool_acq (iptr, id, val) == 0)
213 return EOWNERDEAD;
215 return EBUSY;
218 void
219 __lll_robust_unlock (void *ptr, int flags)
221 unsigned int val = atomic_load_relaxed ((unsigned int *)ptr);
222 while (1)
224 if (val & LLL_WAITERS)
226 lll_set_wake (ptr, 0, flags);
227 break;
229 else if (atomic_compare_exchange_weak_release ((unsigned int *)ptr, &val, 0))
230 break;