diff --git a/lib/glthread/lock.c b/lib/glthread/lock.c index 6760bbd..215991e 100644 --- a/lib/glthread/lock.c +++ b/lib/glthread/lock.c @@ -30,7 +30,7 @@ /* ------------------------- gl_rwlock_t datatype ------------------------- */ -# if HAVE_PTHREAD_RWLOCK +# if HAVE_PTHREAD_RWLOCK && !(__GNU_LIBRARY__ > 1) # if !defined PTHREAD_RWLOCK_INITIALIZER @@ -152,11 +152,9 @@ glthread_rwlock_rdlock_multithreaded (gl_rwlock_t *lock) if (err != 0) return err; /* Test whether only readers are currently running, and whether the runcount - field will not overflow. */ - /* POSIX says: "It is implementation-defined whether the calling thread - acquires the lock when a writer does not hold the lock and there are - writers blocked on the lock." Let's say, no: give the writers a higher - priority. */ + field will not overflow, and whether no writer is waiting. The latter + condition is because POSIX recommends that "write locks shall take + precedence over read locks", to avoid "writer starvation". */ while (!(lock->runcount + 1 > 0 && lock->waiting_writers_count == 0)) { /* This thread has to wait for a while. Enqueue it among the @@ -796,8 +794,10 @@ glthread_rwlock_rdlock_func (gl_rwlock_t *lock) } EnterCriticalSection (&lock->lock); /* Test whether only readers are currently running, and whether the runcount - field will not overflow. */ - if (!(lock->runcount + 1 > 0)) + field will not overflow, and whether no writer is waiting. The latter + condition is because POSIX recommends that "write locks shall take + precedence over read locks", to avoid "writer starvation". */ + if (!(lock->runcount + 1 > 0 && lock->waiting_writers.count == 0)) { /* This thread has to wait for a while. Enqueue it among the waiting_readers. */ diff --git a/lib/glthread/lock.h b/lib/glthread/lock.h index 3ea98d6..c808d0e 100644 --- a/lib/glthread/lock.h +++ b/lib/glthread/lock.h @@ -176,7 +176,7 @@ typedef pthread_mutex_t gl_lock_t; /* ------------------------- gl_rwlock_t datatype ------------------------- */ -# if HAVE_PTHREAD_RWLOCK +# if HAVE_PTHREAD_RWLOCK && !(__GNU_LIBRARY__ > 1) # ifdef PTHREAD_RWLOCK_INITIALIZER