mirror of https://github.com/xqemu/xqemu.git
coroutine-lock: make CoRwlock thread-safe and fair
This adds a CoMutex around the existing CoQueue. Because the write-side can just take CoMutex, the old "writer" field is not necessary anymore. Instead of removing it altogether, count the number of pending writers during a read-side critical section and forbid further readers from entering. Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> Reviewed-by: Fam Zheng <famz@redhat.com> Message-id: 20170213181244.16297-7-pbonzini@redhat.com Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
This commit is contained in:
parent
1ace7ceac5
commit
a7b91d35ba
|
@ -204,8 +204,9 @@ bool qemu_co_queue_empty(CoQueue *queue);
|
||||||
|
|
||||||
|
|
||||||
typedef struct CoRwlock {
|
typedef struct CoRwlock {
|
||||||
bool writer;
|
int pending_writer;
|
||||||
int reader;
|
int reader;
|
||||||
|
CoMutex mutex;
|
||||||
CoQueue queue;
|
CoQueue queue;
|
||||||
} CoRwlock;
|
} CoRwlock;
|
||||||
|
|
||||||
|
|
|
@ -346,16 +346,22 @@ void qemu_co_rwlock_init(CoRwlock *lock)
|
||||||
{
|
{
|
||||||
memset(lock, 0, sizeof(*lock));
|
memset(lock, 0, sizeof(*lock));
|
||||||
qemu_co_queue_init(&lock->queue);
|
qemu_co_queue_init(&lock->queue);
|
||||||
|
qemu_co_mutex_init(&lock->mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
void qemu_co_rwlock_rdlock(CoRwlock *lock)
|
void qemu_co_rwlock_rdlock(CoRwlock *lock)
|
||||||
{
|
{
|
||||||
Coroutine *self = qemu_coroutine_self();
|
Coroutine *self = qemu_coroutine_self();
|
||||||
|
|
||||||
while (lock->writer) {
|
qemu_co_mutex_lock(&lock->mutex);
|
||||||
qemu_co_queue_wait(&lock->queue, NULL);
|
/* For fairness, wait if a writer is in line. */
|
||||||
|
while (lock->pending_writer) {
|
||||||
|
qemu_co_queue_wait(&lock->queue, &lock->mutex);
|
||||||
}
|
}
|
||||||
lock->reader++;
|
lock->reader++;
|
||||||
|
qemu_co_mutex_unlock(&lock->mutex);
|
||||||
|
|
||||||
|
/* The rest of the read-side critical section is run without the mutex. */
|
||||||
self->locks_held++;
|
self->locks_held++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -364,10 +370,13 @@ void qemu_co_rwlock_unlock(CoRwlock *lock)
|
||||||
Coroutine *self = qemu_coroutine_self();
|
Coroutine *self = qemu_coroutine_self();
|
||||||
|
|
||||||
assert(qemu_in_coroutine());
|
assert(qemu_in_coroutine());
|
||||||
if (lock->writer) {
|
if (!lock->reader) {
|
||||||
lock->writer = false;
|
/* The critical section started in qemu_co_rwlock_wrlock. */
|
||||||
qemu_co_queue_restart_all(&lock->queue);
|
qemu_co_queue_restart_all(&lock->queue);
|
||||||
} else {
|
} else {
|
||||||
|
self->locks_held--;
|
||||||
|
|
||||||
|
qemu_co_mutex_lock(&lock->mutex);
|
||||||
lock->reader--;
|
lock->reader--;
|
||||||
assert(lock->reader >= 0);
|
assert(lock->reader >= 0);
|
||||||
/* Wakeup only one waiting writer */
|
/* Wakeup only one waiting writer */
|
||||||
|
@ -375,16 +384,20 @@ void qemu_co_rwlock_unlock(CoRwlock *lock)
|
||||||
qemu_co_queue_next(&lock->queue);
|
qemu_co_queue_next(&lock->queue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self->locks_held--;
|
qemu_co_mutex_unlock(&lock->mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
void qemu_co_rwlock_wrlock(CoRwlock *lock)
|
void qemu_co_rwlock_wrlock(CoRwlock *lock)
|
||||||
{
|
{
|
||||||
Coroutine *self = qemu_coroutine_self();
|
qemu_co_mutex_lock(&lock->mutex);
|
||||||
|
lock->pending_writer++;
|
||||||
while (lock->writer || lock->reader) {
|
while (lock->reader) {
|
||||||
qemu_co_queue_wait(&lock->queue, NULL);
|
qemu_co_queue_wait(&lock->queue, &lock->mutex);
|
||||||
}
|
}
|
||||||
lock->writer = true;
|
lock->pending_writer--;
|
||||||
self->locks_held++;
|
|
||||||
|
/* The rest of the write-side critical section is run with
|
||||||
|
* the mutex taken, so that lock->reader remains zero.
|
||||||
|
* There is no need to update self->locks_held.
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue