libc: fix wunlock() libthread deadlock

when wunlock() was used by threads running within the same proc,
the wunlock() can deadlock as it keeps holding the RWLock.lock
spinlock while indirectly calling _threadrendezvous(). when
_threadrendezvous() switches to another thread in the same proc,
then that thread can hang at rlock()/wlock()/runlock() again
waiting for wunlock() to release the spinlock which will never
happen as lock() does not schedule threads.

wunlock() is changed to release the spinlock during rendezvous
wakeup of readers. note that this is a bit dangerous as more
readers might queue concurrently now which means that if
we cannot keep up with the wakeups, we might keep on waking
readers forever. that will be another patch for the future.
This commit is contained in:
cinap_lenrek 2015-08-10 23:13:41 +02:00
parent bc895417f8
commit f43df64325

View file

@ -58,7 +58,6 @@ qlock(QLock *q)
return; return;
} }
/* chain into waiting list */ /* chain into waiting list */
mp = getqlp(); mp = getqlp();
p = q->tail; p = q->tail;
@ -259,17 +258,19 @@ wunlock(RWLock *q)
if(p->state != QueuingR) if(p->state != QueuingR)
abort(); abort();
/* wake waiting readers */ q->writer = 0;
while(q->head != nil && q->head->state == QueuingR){ do {
p = q->head; /* wake waiting readers */
q->head = p->next; q->head = p->next;
if(q->head == nil)
q->tail = nil;
q->readers++; q->readers++;
unlock(&q->lock);
while((*_rendezvousp)(p, 0) == (void*)~0) while((*_rendezvousp)(p, 0) == (void*)~0)
; ;
} lock(&q->lock);
if(q->head == nil) p = q->head;
q->tail = nil; } while(p != nil && p->state == QueuingR && q->writer == 0);
q->writer = 0;
unlock(&q->lock); unlock(&q->lock);
} }