Debian Codegraph @

lib/windows-rwlock.c

raw
1 /* Read-write locks (native Windows implementation).
2 Copyright (C) 2005-2026 Free Software Foundation, Inc.
3
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
8
9 This file is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
16
17 /* Written by Bruno Haible <[email protected]>, 2005.
18 Based on GCC's gthr-win32.h. */
19
20 #include <config.h>
21
22 /* Specification. */
23 #include "windows-rwlock.h"
24
25 #include <errno.h>
26 #include <stdlib.h>
27
28 /* Don't assume that UNICODE is not defined. */
29 #undef CreateEvent
30 #define CreateEvent CreateEventA
31
32 /* In this file, the waitqueues are implemented as circular arrays. */
33 #define glwthread_waitqueue_t glwthread_carray_waitqueue_t
34
35 static void
36 glwthread_waitqueue_init (glwthread_waitqueue_t *wq)
37 {
38 wq->array = NULL;
39 wq->count = 0;
40 wq->alloc = 0;
41 wq->offset = 0;
42 }
43
44 /* Enqueues the current thread, represented by an event, in a wait queue.
45 Returns INVALID_HANDLE_VALUE if an allocation failure occurs. */
46 static HANDLE
47 glwthread_waitqueue_add (glwthread_waitqueue_t *wq)
48 {
49 if (wq->count == wq->alloc)
50 {
51 unsigned int new_alloc = 2 * wq->alloc + 1;
52 HANDLE *new_array =
53 (HANDLE *) realloc (wq->array, new_alloc * sizeof (HANDLE));
54 if (new_array == NULL)
55 /* No more memory. */
56 return INVALID_HANDLE_VALUE;
57 /* Now is a good opportunity to rotate the array so that its contents
58 starts at offset 0. */
59 if (wq->offset > 0)
60 {
61 unsigned int old_count = wq->count;
62 unsigned int old_alloc = wq->alloc;
63 unsigned int old_offset = wq->offset;
64 if (old_offset + old_count > old_alloc)
65 {
66 unsigned int limit = old_offset + old_count - old_alloc;
67 for (unsigned int i = 0; i < limit; i++)
68 new_array[old_alloc + i] = new_array[i];
69 }
70 for (unsigned int i = 0; i < old_count; i++)
71 new_array[i] = new_array[old_offset + i];
72 wq->offset = 0;
73 }
74 wq->array = new_array;
75 wq->alloc = new_alloc;
76 }
77 /* Whether the created event is a manual-reset one or an auto-reset one,
78 does not matter, since we will wait on it only once. */
79 HANDLE event = CreateEvent (NULL, TRUE, FALSE, NULL);
80 if (event == INVALID_HANDLE_VALUE)
81 /* No way to allocate an event. */
82 return INVALID_HANDLE_VALUE;
83 unsigned int index = wq->offset + wq->count;
84 if (index >= wq->alloc)
85 index -= wq->alloc;
86 wq->array[index] = event;
87 wq->count++;
88 return event;
89 }
90
91 /* Notifies the first thread from a wait queue and dequeues it. */
92 static void
93 glwthread_waitqueue_notify_first (glwthread_waitqueue_t *wq)
94 {
95 SetEvent (wq->array[wq->offset + 0]);
96 wq->offset++;
97 wq->count--;
98 if (wq->count == 0 || wq->offset == wq->alloc)
99 wq->offset = 0;
100 }
101
102 /* Notifies all threads from a wait queue and dequeues them all. */
103 static void
104 glwthread_waitqueue_notify_all (glwthread_waitqueue_t *wq)
105 {
106 for (unsigned int i = 0; i < wq->count; i++)
107 {
108 unsigned int index = wq->offset + i;
109 if (index >= wq->alloc)
110 index -= wq->alloc;
111 SetEvent (wq->array[index]);
112 }
113 wq->count = 0;
114 wq->offset = 0;
115 }
116
117 void
118 glwthread_rwlock_init (glwthread_rwlock_t *lock)
119 {
120 InitializeCriticalSection (&lock->lock);
121 glwthread_waitqueue_init (&lock->waiting_readers);
122 glwthread_waitqueue_init (&lock->waiting_writers);
123 lock->runcount = 0;
124 lock->guard.done = 1;
125 }
126
127 int
128 glwthread_rwlock_rdlock (glwthread_rwlock_t *lock)
129 {
130 if (!lock->guard.done)
131 {
132 if (InterlockedIncrement (&lock->guard.started) == 0)
133 /* This thread is the first one to need this lock. Initialize it. */
134 glwthread_rwlock_init (lock);
135 else
136 {
137 /* Don't let lock->guard.started grow and wrap around. */
138 InterlockedDecrement (&lock->guard.started);
139 /* Yield the CPU while waiting for another thread to finish
140 initializing this lock. */
141 while (!lock->guard.done)
142 Sleep (0);
143 }
144 }
145 EnterCriticalSection (&lock->lock);
146 /* Test whether only readers are currently running, and whether the runcount
147 field will not overflow, and whether no writer is waiting. The latter
148 condition is because POSIX recommends that "write locks shall take
149 precedence over read locks", to avoid "writer starvation". */
150 if (!(lock->runcount + 1 > 0 && lock->waiting_writers.count == 0))
151 {
152 /* This thread has to wait for a while. Enqueue it among the
153 waiting_readers. */
154 HANDLE event = glwthread_waitqueue_add (&lock->waiting_readers);
155 if (event != INVALID_HANDLE_VALUE)
156 {
157 LeaveCriticalSection (&lock->lock);
158 /* Wait until another thread signals this event. */
159 DWORD result = WaitForSingleObject (event, INFINITE);
160 if (result == WAIT_FAILED || result == WAIT_TIMEOUT)
161 abort ();
162 CloseHandle (event);
163 /* The thread which signalled the event already did the bookkeeping:
164 removed us from the waiting_readers, incremented lock->runcount. */
165 if (!(lock->runcount > 0))
166 abort ();
167 return 0;
168 }
169 else
170 {
171 /* Allocation failure. Weird. */
172 do
173 {
174 LeaveCriticalSection (&lock->lock);
175 Sleep (1);
176 EnterCriticalSection (&lock->lock);
177 }
178 while (!(lock->runcount + 1 > 0));
179 }
180 }
181 lock->runcount++;
182 LeaveCriticalSection (&lock->lock);
183 return 0;
184 }
185
186 int
187 glwthread_rwlock_wrlock (glwthread_rwlock_t *lock)
188 {
189 if (!lock->guard.done)
190 {
191 if (InterlockedIncrement (&lock->guard.started) == 0)
192 /* This thread is the first one to need this lock. Initialize it. */
193 glwthread_rwlock_init (lock);
194 else
195 {
196 /* Don't let lock->guard.started grow and wrap around. */
197 InterlockedDecrement (&lock->guard.started);
198 /* Yield the CPU while waiting for another thread to finish
199 initializing this lock. */
200 while (!lock->guard.done)
201 Sleep (0);
202 }
203 }
204 EnterCriticalSection (&lock->lock);
205 /* Test whether no readers or writers are currently running. */
206 if (!(lock->runcount == 0))
207 {
208 /* This thread has to wait for a while. Enqueue it among the
209 waiting_writers. */
210 HANDLE event = glwthread_waitqueue_add (&lock->waiting_writers);
211 if (event != INVALID_HANDLE_VALUE)
212 {
213 LeaveCriticalSection (&lock->lock);
214 /* Wait until another thread signals this event. */
215 DWORD result = WaitForSingleObject (event, INFINITE);
216 if (result == WAIT_FAILED || result == WAIT_TIMEOUT)
217 abort ();
218 CloseHandle (event);
219 /* The thread which signalled the event already did the bookkeeping:
220 removed us from the waiting_writers, set lock->runcount = -1. */
221 if (!(lock->runcount == -1))
222 abort ();
223 return 0;
224 }
225 else
226 {
227 /* Allocation failure. Weird. */
228 do
229 {
230 LeaveCriticalSection (&lock->lock);
231 Sleep (1);
232 EnterCriticalSection (&lock->lock);
233 }
234 while (!(lock->runcount == 0));
235 }
236 }
237 lock->runcount--; /* runcount becomes -1 */
238 LeaveCriticalSection (&lock->lock);
239 return 0;
240 }
241
242 int
243 glwthread_rwlock_tryrdlock (glwthread_rwlock_t *lock)
244 {
245 if (!lock->guard.done)
246 {
247 if (InterlockedIncrement (&lock->guard.started) == 0)
248 /* This thread is the first one to need this lock. Initialize it. */
249 glwthread_rwlock_init (lock);
250 else
251 {
252 /* Don't let lock->guard.started grow and wrap around. */
253 InterlockedDecrement (&lock->guard.started);
254 /* Yield the CPU while waiting for another thread to finish
255 initializing this lock. */
256 while (!lock->guard.done)
257 Sleep (0);
258 }
259 }
260 /* It's OK to wait for this critical section, because it is never taken for a
261 long time. */
262 EnterCriticalSection (&lock->lock);
263 /* Test whether only readers are currently running, and whether the runcount
264 field will not overflow, and whether no writer is waiting. The latter
265 condition is because POSIX recommends that "write locks shall take
266 precedence over read locks", to avoid "writer starvation". */
267 if (!(lock->runcount + 1 > 0 && lock->waiting_writers.count == 0))
268 {
269 /* This thread would have to wait for a while. Return instead. */
270 LeaveCriticalSection (&lock->lock);
271 return EBUSY;
272 }
273 lock->runcount++;
274 LeaveCriticalSection (&lock->lock);
275 return 0;
276 }
277
278 int
279 glwthread_rwlock_trywrlock (glwthread_rwlock_t *lock)
280 {
281 if (!lock->guard.done)
282 {
283 if (InterlockedIncrement (&lock->guard.started) == 0)
284 /* This thread is the first one to need this lock. Initialize it. */
285 glwthread_rwlock_init (lock);
286 else
287 {
288 /* Don't let lock->guard.started grow and wrap around. */
289 InterlockedDecrement (&lock->guard.started);
290 /* Yield the CPU while waiting for another thread to finish
291 initializing this lock. */
292 while (!lock->guard.done)
293 Sleep (0);
294 }
295 }
296 /* It's OK to wait for this critical section, because it is never taken for a
297 long time. */
298 EnterCriticalSection (&lock->lock);
299 /* Test whether no readers or writers are currently running. */
300 if (!(lock->runcount == 0))
301 {
302 /* This thread would have to wait for a while. Return instead. */
303 LeaveCriticalSection (&lock->lock);
304 return EBUSY;
305 }
306 lock->runcount--; /* runcount becomes -1 */
307 LeaveCriticalSection (&lock->lock);
308 return 0;
309 }
310
311 int
312 glwthread_rwlock_unlock (glwthread_rwlock_t *lock)
313 {
314 if (!lock->guard.done)
315 return EINVAL;
316 EnterCriticalSection (&lock->lock);
317 if (lock->runcount < 0)
318 {
319 /* Drop a writer lock. */
320 if (!(lock->runcount == -1))
321 abort ();
322 lock->runcount = 0;
323 }
324 else
325 {
326 /* Drop a reader lock. */
327 if (!(lock->runcount > 0))
328 {
329 LeaveCriticalSection (&lock->lock);
330 return EPERM;
331 }
332 lock->runcount--;
333 }
334 if (lock->runcount == 0)
335 {
336 /* POSIX recommends that "write locks shall take precedence over read
337 locks", to avoid "writer starvation". */
338 if (lock->waiting_writers.count > 0)
339 {
340 /* Wake up one of the waiting writers. */
341 lock->runcount--;
342 glwthread_waitqueue_notify_first (&lock->waiting_writers);
343 }
344 else
345 {
346 /* Wake up all waiting readers. */
347 lock->runcount += lock->waiting_readers.count;
348 glwthread_waitqueue_notify_all (&lock->waiting_readers);
349 }
350 }
351 LeaveCriticalSection (&lock->lock);
352 return 0;
353 }
354
355 int
356 glwthread_rwlock_destroy (glwthread_rwlock_t *lock)
357 {
358 if (!lock->guard.done)
359 return EINVAL;
360 if (lock->runcount != 0)
361 return EBUSY;
362 DeleteCriticalSection (&lock->lock);
363 if (lock->waiting_readers.array != NULL)
364 free (lock->waiting_readers.array);
365 if (lock->waiting_writers.array != NULL)
366 free (lock->waiting_writers.array);
367 lock->guard.done = 0;
368 return 0;
369 }