1
/* Locking in multithreaded situations.
2
Copyright (C) 2005 Free Software Foundation, Inc.
4
This program is free software; you can redistribute it and/or modify it
5
under the terms of the GNU Library General Public License as published
6
by the Free Software Foundation; either version 2, or (at your option)
9
This program 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 GNU
12
Library General Public License for more details.
14
You should have received a copy of the GNU Library General Public
15
License along with this program; if not, write to the Free Software
16
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19
/* Written by Bruno Haible <bruno@clisp.org>, 2005.
20
Based on GCC's gthr-posix.h, gthr-posix95.h, gthr-solaris.h,
29
/* ========================================================================= */
33
/* Use the POSIX threads library. */
35
# if PTHREAD_IN_USE_DETECTION_HARD
37
/* The function to be executed by a dummy thread. */
39
dummy_thread_func (void *arg)
45
glthread_in_use (void)
48
static int result; /* 1: linked with -lpthread, 0: only with libc */
54
if (pthread_create (&thread, NULL, dummy_thread_func, NULL) != 0)
55
/* Thread creation failed. */
59
/* Thread creation works. */
61
if (pthread_join (thread, &retval) != 0)
72
/* -------------------------- gl_lock_t datatype -------------------------- */
74
/* ------------------------- gl_rwlock_t datatype ------------------------- */
76
# if HAVE_PTHREAD_RWLOCK
78
# if !defined PTHREAD_RWLOCK_INITIALIZER
81
glthread_rwlock_init (gl_rwlock_t *lock)
83
if (pthread_rwlock_init (&lock->rwlock, NULL) != 0)
85
lock->initialized = 1;
89
glthread_rwlock_rdlock (gl_rwlock_t *lock)
91
if (!lock->initialized)
93
if (pthread_mutex_lock (&lock->guard) != 0)
95
if (!lock->initialized)
96
glthread_rwlock_init (lock);
97
if (pthread_mutex_unlock (&lock->guard) != 0)
100
if (pthread_rwlock_rdlock (&lock->rwlock) != 0)
105
glthread_rwlock_wrlock (gl_rwlock_t *lock)
107
if (!lock->initialized)
109
if (pthread_mutex_lock (&lock->guard) != 0)
111
if (!lock->initialized)
112
glthread_rwlock_init (lock);
113
if (pthread_mutex_unlock (&lock->guard) != 0)
116
if (pthread_rwlock_wrlock (&lock->rwlock) != 0)
121
glthread_rwlock_unlock (gl_rwlock_t *lock)
123
if (!lock->initialized)
125
if (pthread_rwlock_unlock (&lock->rwlock) != 0)
130
glthread_rwlock_destroy (gl_rwlock_t *lock)
132
if (!lock->initialized)
134
if (pthread_rwlock_destroy (&lock->rwlock) != 0)
136
lock->initialized = 0;
144
glthread_rwlock_init (gl_rwlock_t *lock)
146
if (pthread_mutex_init (&lock->lock, NULL) != 0)
148
if (pthread_cond_init (&lock->waiting_readers, NULL) != 0)
150
if (pthread_cond_init (&lock->waiting_writers, NULL) != 0)
152
lock->waiting_writers_count = 0;
157
glthread_rwlock_rdlock (gl_rwlock_t *lock)
159
if (pthread_mutex_lock (&lock->lock) != 0)
161
/* Test whether only readers are currently running, and whether the runcount
162
field will not overflow. */
163
/* POSIX says: "It is implementation-defined whether the calling thread
164
acquires the lock when a writer does not hold the lock and there are
165
writers blocked on the lock." Let's say, no: give the writers a higher
167
while (!(lock->runcount + 1 > 0 && lock->waiting_writers_count == 0))
169
/* This thread has to wait for a while. Enqueue it among the
171
if (pthread_cond_wait (&lock->waiting_readers, &lock->lock) != 0)
175
if (pthread_mutex_unlock (&lock->lock) != 0)
180
glthread_rwlock_wrlock (gl_rwlock_t *lock)
182
if (pthread_mutex_lock (&lock->lock) != 0)
184
/* Test whether no readers or writers are currently running. */
185
while (!(lock->runcount == 0))
187
/* This thread has to wait for a while. Enqueue it among the
189
lock->waiting_writers_count++;
190
if (pthread_cond_wait (&lock->waiting_writers, &lock->lock) != 0)
192
lock->waiting_writers_count--;
194
lock->runcount--; /* runcount becomes -1 */
195
if (pthread_mutex_unlock (&lock->lock) != 0)
200
glthread_rwlock_unlock (gl_rwlock_t *lock)
202
if (pthread_mutex_lock (&lock->lock) != 0)
204
if (lock->runcount < 0)
206
/* Drop a writer lock. */
207
if (!(lock->runcount == -1))
213
/* Drop a reader lock. */
214
if (!(lock->runcount > 0))
218
if (lock->runcount == 0)
220
/* POSIX recommends that "write locks shall take precedence over read
221
locks", to avoid "writer starvation". */
222
if (lock->waiting_writers_count > 0)
224
/* Wake up one of the waiting writers. */
225
if (pthread_cond_signal (&lock->waiting_writers) != 0)
230
/* Wake up all waiting readers. */
231
if (pthread_cond_broadcast (&lock->waiting_readers) != 0)
235
if (pthread_mutex_unlock (&lock->lock) != 0)
240
glthread_rwlock_destroy (gl_rwlock_t *lock)
242
if (pthread_mutex_destroy (&lock->lock) != 0)
244
if (pthread_cond_destroy (&lock->waiting_readers) != 0)
246
if (pthread_cond_destroy (&lock->waiting_writers) != 0)
252
/* --------------------- gl_recursive_lock_t datatype --------------------- */
254
# if HAVE_PTHREAD_MUTEX_RECURSIVE
256
# if !(defined PTHREAD_RECURSIVE_MUTEX_INITIALIZER || defined PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
259
glthread_recursive_lock_init (gl_recursive_lock_t *lock)
261
pthread_mutexattr_t attributes;
263
if (pthread_mutexattr_init (&attributes) != 0)
265
if (pthread_mutexattr_settype (&attributes, PTHREAD_MUTEX_RECURSIVE) != 0)
267
if (pthread_mutex_init (&lock->recmutex, &attributes) != 0)
269
if (pthread_mutexattr_destroy (&attributes) != 0)
271
lock->initialized = 1;
275
glthread_recursive_lock_lock (gl_recursive_lock_t *lock)
277
if (!lock->initialized)
279
if (pthread_mutex_lock (&lock->guard) != 0)
281
if (!lock->initialized)
282
glthread_recursive_lock_init (lock);
283
if (pthread_mutex_unlock (&lock->guard) != 0)
286
if (pthread_mutex_lock (&lock->recmutex) != 0)
291
glthread_recursive_lock_unlock (gl_recursive_lock_t *lock)
293
if (!lock->initialized)
295
if (pthread_mutex_unlock (&lock->recmutex) != 0)
300
glthread_recursive_lock_destroy (gl_recursive_lock_t *lock)
302
if (!lock->initialized)
304
if (pthread_mutex_destroy (&lock->recmutex) != 0)
306
lock->initialized = 0;
314
glthread_recursive_lock_init (gl_recursive_lock_t *lock)
316
if (pthread_mutex_init (&lock->mutex, NULL) != 0)
318
lock->owner = (pthread_t) 0;
323
glthread_recursive_lock_lock (gl_recursive_lock_t *lock)
325
pthread_t self = pthread_self ();
326
if (lock->owner != self)
328
if (pthread_mutex_lock (&lock->mutex) != 0)
332
if (++(lock->depth) == 0) /* wraparound? */
337
glthread_recursive_lock_unlock (gl_recursive_lock_t *lock)
339
if (lock->owner != pthread_self ())
341
if (lock->depth == 0)
343
if (--(lock->depth) == 0)
345
lock->owner = (pthread_t) 0;
346
if (pthread_mutex_unlock (&lock->mutex) != 0)
352
glthread_recursive_lock_destroy (gl_recursive_lock_t *lock)
354
if (lock->owner != (pthread_t) 0)
356
if (pthread_mutex_destroy (&lock->mutex) != 0)
362
/* -------------------------- gl_once_t datatype -------------------------- */
364
static const pthread_once_t fresh_once = PTHREAD_ONCE_INIT;
367
glthread_once_singlethreaded (pthread_once_t *once_control)
369
/* We don't know whether pthread_once_t is an integer type, a floating-point
370
type, a pointer type, or a structure type. */
371
char *firstbyte = (char *)once_control;
372
if (*firstbyte == *(const char *)&fresh_once)
374
/* First time use of once_control. Invert the first byte. */
375
*firstbyte = ~ *(const char *)&fresh_once;
384
/* ========================================================================= */
388
/* Use the GNU Pth threads library. */
390
/* -------------------------- gl_lock_t datatype -------------------------- */
392
/* ------------------------- gl_rwlock_t datatype ------------------------- */
394
/* --------------------- gl_recursive_lock_t datatype --------------------- */
396
/* -------------------------- gl_once_t datatype -------------------------- */
399
glthread_once_call (void *arg)
401
void (**gl_once_temp_addr) (void) = (void (**) (void)) arg;
402
void (*initfunction) (void) = *gl_once_temp_addr;
407
glthread_once_singlethreaded (pth_once_t *once_control)
409
/* We know that pth_once_t is an integer type. */
410
if (*once_control == PTH_ONCE_INIT)
412
/* First time use of once_control. Invert the marker. */
413
*once_control = ~ PTH_ONCE_INIT;
422
/* ========================================================================= */
424
#if USE_SOLARIS_THREADS
426
/* Use the old Solaris threads library. */
428
/* -------------------------- gl_lock_t datatype -------------------------- */
430
/* ------------------------- gl_rwlock_t datatype ------------------------- */
432
/* --------------------- gl_recursive_lock_t datatype --------------------- */
435
glthread_recursive_lock_init (gl_recursive_lock_t *lock)
437
if (mutex_init (&lock->mutex, USYNC_THREAD, NULL) != 0)
439
lock->owner = (thread_t) 0;
444
glthread_recursive_lock_lock (gl_recursive_lock_t *lock)
446
thread_t self = thr_self ();
447
if (lock->owner != self)
449
if (mutex_lock (&lock->mutex) != 0)
453
if (++(lock->depth) == 0) /* wraparound? */
458
glthread_recursive_lock_unlock (gl_recursive_lock_t *lock)
460
if (lock->owner != thr_self ())
462
if (lock->depth == 0)
464
if (--(lock->depth) == 0)
466
lock->owner = (thread_t) 0;
467
if (mutex_unlock (&lock->mutex) != 0)
473
glthread_recursive_lock_destroy (gl_recursive_lock_t *lock)
475
if (lock->owner != (thread_t) 0)
477
if (mutex_destroy (&lock->mutex) != 0)
481
/* -------------------------- gl_once_t datatype -------------------------- */
484
glthread_once (gl_once_t *once_control, void (*initfunction) (void))
486
if (!once_control->inited)
488
/* Use the mutex to guarantee that if another thread is already calling
489
the initfunction, this thread waits until it's finished. */
490
if (mutex_lock (&once_control->mutex) != 0)
492
if (!once_control->inited)
494
once_control->inited = 1;
497
if (mutex_unlock (&once_control->mutex) != 0)
503
glthread_once_singlethreaded (gl_once_t *once_control)
505
/* We know that gl_once_t contains an integer type. */
506
if (!once_control->inited)
508
/* First time use of once_control. Invert the marker. */
509
once_control->inited = ~ 0;
518
/* ========================================================================= */
520
#if USE_WIN32_THREADS
522
/* -------------------------- gl_lock_t datatype -------------------------- */
525
glthread_lock_init (gl_lock_t *lock)
527
InitializeCriticalSection (&lock->lock);
528
lock->guard.done = 1;
532
glthread_lock_lock (gl_lock_t *lock)
534
if (!lock->guard.done)
536
if (InterlockedIncrement (&lock->guard.started) == 0)
537
/* This thread is the first one to need this lock. Initialize it. */
538
glthread_lock_init (lock);
540
/* Yield the CPU while waiting for another thread to finish
541
initializing this lock. */
542
while (!lock->guard.done)
545
EnterCriticalSection (&lock->lock);
549
glthread_lock_unlock (gl_lock_t *lock)
551
if (!lock->guard.done)
553
LeaveCriticalSection (&lock->lock);
557
glthread_lock_destroy (gl_lock_t *lock)
559
if (!lock->guard.done)
561
DeleteCriticalSection (&lock->lock);
562
lock->guard.done = 0;
565
/* ------------------------- gl_rwlock_t datatype ------------------------- */
568
gl_waitqueue_init (gl_waitqueue_t *wq)
576
/* Enqueues the current thread, represented by an event, in a wait queue.
577
Returns INVALID_HANDLE_VALUE if an allocation failure occurs. */
579
gl_waitqueue_add (gl_waitqueue_t *wq)
584
if (wq->count == wq->alloc)
586
unsigned int new_alloc = 2 * wq->alloc + 1;
588
(HANDLE *) realloc (wq->array, new_alloc * sizeof (HANDLE));
589
if (new_array == NULL)
590
/* No more memory. */
591
return INVALID_HANDLE_VALUE;
592
/* Now is a good opportunity to rotate the array so that its contents
593
starts at offset 0. */
596
unsigned int old_count = wq->count;
597
unsigned int old_alloc = wq->alloc;
598
unsigned int old_offset = wq->offset;
600
if (old_offset + old_count > old_alloc)
602
unsigned int limit = old_offset + old_count - old_alloc;
603
for (i = 0; i < limit; i++)
604
new_array[old_alloc + i] = new_array[i];
606
for (i = 0; i < old_count; i++)
607
new_array[i] = new_array[old_offset + i];
610
wq->array = new_array;
611
wq->alloc = new_alloc;
613
event = CreateEvent (NULL, TRUE, FALSE, NULL);
614
if (event == INVALID_HANDLE_VALUE)
615
/* No way to allocate an event. */
616
return INVALID_HANDLE_VALUE;
617
index = wq->offset + wq->count;
618
if (index >= wq->alloc)
620
wq->array[index] = event;
625
/* Notifies the first thread from a wait queue and dequeues it. */
627
gl_waitqueue_notify_first (gl_waitqueue_t *wq)
629
SetEvent (wq->array[wq->offset + 0]);
632
if (wq->count == 0 || wq->offset == wq->alloc)
636
/* Notifies all threads from a wait queue and dequeues them all. */
638
gl_waitqueue_notify_all (gl_waitqueue_t *wq)
642
for (i = 0; i < wq->count; i++)
644
unsigned int index = wq->offset + i;
645
if (index >= wq->alloc)
647
SetEvent (wq->array[index]);
654
glthread_rwlock_init (gl_rwlock_t *lock)
656
InitializeCriticalSection (&lock->lock);
657
gl_waitqueue_init (&lock->waiting_readers);
658
gl_waitqueue_init (&lock->waiting_writers);
660
lock->guard.done = 1;
664
glthread_rwlock_rdlock (gl_rwlock_t *lock)
666
if (!lock->guard.done)
668
if (InterlockedIncrement (&lock->guard.started) == 0)
669
/* This thread is the first one to need this lock. Initialize it. */
670
glthread_rwlock_init (lock);
672
/* Yield the CPU while waiting for another thread to finish
673
initializing this lock. */
674
while (!lock->guard.done)
677
EnterCriticalSection (&lock->lock);
678
/* Test whether only readers are currently running, and whether the runcount
679
field will not overflow. */
680
if (!(lock->runcount + 1 > 0))
682
/* This thread has to wait for a while. Enqueue it among the
684
HANDLE event = gl_waitqueue_add (&lock->waiting_readers);
685
if (event != INVALID_HANDLE_VALUE)
688
LeaveCriticalSection (&lock->lock);
689
/* Wait until another thread signals this event. */
690
result = WaitForSingleObject (event, INFINITE);
691
if (result == WAIT_FAILED || result == WAIT_TIMEOUT)
694
/* The thread which signalled the event already did the bookkeeping:
695
removed us from the waiting_readers, incremented lock->runcount. */
696
if (!(lock->runcount > 0))
702
/* Allocation failure. Weird. */
705
LeaveCriticalSection (&lock->lock);
707
EnterCriticalSection (&lock->lock);
709
while (!(lock->runcount + 1 > 0));
713
LeaveCriticalSection (&lock->lock);
717
glthread_rwlock_wrlock (gl_rwlock_t *lock)
719
if (!lock->guard.done)
721
if (InterlockedIncrement (&lock->guard.started) == 0)
722
/* This thread is the first one to need this lock. Initialize it. */
723
glthread_rwlock_init (lock);
725
/* Yield the CPU while waiting for another thread to finish
726
initializing this lock. */
727
while (!lock->guard.done)
730
EnterCriticalSection (&lock->lock);
731
/* Test whether no readers or writers are currently running. */
732
if (!(lock->runcount == 0))
734
/* This thread has to wait for a while. Enqueue it among the
736
HANDLE event = gl_waitqueue_add (&lock->waiting_writers);
737
if (event != INVALID_HANDLE_VALUE)
740
LeaveCriticalSection (&lock->lock);
741
/* Wait until another thread signals this event. */
742
result = WaitForSingleObject (event, INFINITE);
743
if (result == WAIT_FAILED || result == WAIT_TIMEOUT)
746
/* The thread which signalled the event already did the bookkeeping:
747
removed us from the waiting_writers, set lock->runcount = -1. */
748
if (!(lock->runcount == -1))
754
/* Allocation failure. Weird. */
757
LeaveCriticalSection (&lock->lock);
759
EnterCriticalSection (&lock->lock);
761
while (!(lock->runcount == 0));
764
lock->runcount--; /* runcount becomes -1 */
765
LeaveCriticalSection (&lock->lock);
769
glthread_rwlock_unlock (gl_rwlock_t *lock)
771
if (!lock->guard.done)
773
EnterCriticalSection (&lock->lock);
774
if (lock->runcount < 0)
776
/* Drop a writer lock. */
777
if (!(lock->runcount == -1))
783
/* Drop a reader lock. */
784
if (!(lock->runcount > 0))
788
if (lock->runcount == 0)
790
/* POSIX recommends that "write locks shall take precedence over read
791
locks", to avoid "writer starvation". */
792
if (lock->waiting_writers.count > 0)
794
/* Wake up one of the waiting writers. */
796
gl_waitqueue_notify_first (&lock->waiting_writers);
800
/* Wake up all waiting readers. */
801
lock->runcount += lock->waiting_readers.count;
802
gl_waitqueue_notify_all (&lock->waiting_readers);
805
LeaveCriticalSection (&lock->lock);
809
glthread_rwlock_destroy (gl_rwlock_t *lock)
811
if (!lock->guard.done)
813
if (lock->runcount != 0)
815
DeleteCriticalSection (&lock->lock);
816
if (lock->waiting_readers.array != NULL)
817
free (lock->waiting_readers.array);
818
if (lock->waiting_writers.array != NULL)
819
free (lock->waiting_writers.array);
820
lock->guard.done = 0;
823
/* --------------------- gl_recursive_lock_t datatype --------------------- */
826
glthread_recursive_lock_init (gl_recursive_lock_t *lock)
830
InitializeCriticalSection (&lock->lock);
831
lock->guard.done = 1;
835
glthread_recursive_lock_lock (gl_recursive_lock_t *lock)
837
if (!lock->guard.done)
839
if (InterlockedIncrement (&lock->guard.started) == 0)
840
/* This thread is the first one to need this lock. Initialize it. */
841
glthread_recursive_lock_init (lock);
843
/* Yield the CPU while waiting for another thread to finish
844
initializing this lock. */
845
while (!lock->guard.done)
849
DWORD self = GetCurrentThreadId ();
850
if (lock->owner != self)
852
EnterCriticalSection (&lock->lock);
855
if (++(lock->depth) == 0) /* wraparound? */
861
glthread_recursive_lock_unlock (gl_recursive_lock_t *lock)
863
if (lock->owner != GetCurrentThreadId ())
865
if (lock->depth == 0)
867
if (--(lock->depth) == 0)
870
LeaveCriticalSection (&lock->lock);
875
glthread_recursive_lock_destroy (gl_recursive_lock_t *lock)
877
if (lock->owner != 0)
879
DeleteCriticalSection (&lock->lock);
880
lock->guard.done = 0;
883
/* -------------------------- gl_once_t datatype -------------------------- */
886
glthread_once (gl_once_t *once_control, void (*initfunction) (void))
888
if (once_control->inited <= 0)
890
if (InterlockedIncrement (&once_control->started) == 0)
892
/* This thread is the first one to come to this once_control. */
893
InitializeCriticalSection (&once_control->lock);
894
EnterCriticalSection (&once_control->lock);
895
once_control->inited = 0;
897
once_control->inited = 1;
898
LeaveCriticalSection (&once_control->lock);
902
/* Undo last operation. */
903
InterlockedDecrement (&once_control->started);
904
/* Some other thread has already started the initialization.
905
Yield the CPU while waiting for the other thread to finish
906
initializing and taking the lock. */
907
while (once_control->inited < 0)
909
if (once_control->inited <= 0)
911
/* Take the lock. This blocks until the other thread has
912
finished calling the initfunction. */
913
EnterCriticalSection (&once_control->lock);
914
LeaveCriticalSection (&once_control->lock);
915
if (!(once_control->inited > 0))
924
/* ========================================================================= */