2011-06-30 19:56:46 +04:00
|
|
|
/*
|
|
|
|
* coroutine queues and locks
|
|
|
|
*
|
|
|
|
* Copyright (c) 2011 Kevin Wolf <kwolf@redhat.com>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2016-01-29 20:49:55 +03:00
|
|
|
#include "qemu/osdep.h"
|
2011-06-30 19:56:46 +04:00
|
|
|
#include "qemu-common.h"
|
2015-09-01 16:48:02 +03:00
|
|
|
#include "qemu/coroutine.h"
|
|
|
|
#include "qemu/coroutine_int.h"
|
2012-12-17 21:20:00 +04:00
|
|
|
#include "qemu/queue.h"
|
2017-02-13 16:52:25 +03:00
|
|
|
#include "block/aio.h"
|
2011-06-30 19:56:46 +04:00
|
|
|
#include "trace.h"
|
|
|
|
|
|
|
|
void qemu_co_queue_init(CoQueue *queue)
|
|
|
|
{
|
2016-07-04 20:09:59 +03:00
|
|
|
QSIMPLEQ_INIT(&queue->entries);
|
2011-06-30 19:56:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void coroutine_fn qemu_co_queue_wait(CoQueue *queue)
|
|
|
|
{
|
|
|
|
Coroutine *self = qemu_coroutine_self();
|
2016-07-04 20:09:59 +03:00
|
|
|
QSIMPLEQ_INSERT_TAIL(&queue->entries, self, co_queue_next);
|
2011-06-30 19:56:46 +04:00
|
|
|
qemu_coroutine_yield();
|
|
|
|
assert(qemu_in_coroutine());
|
|
|
|
}
|
|
|
|
|
2013-05-17 17:51:26 +04:00
|
|
|
/**
|
|
|
|
* qemu_co_queue_run_restart:
|
|
|
|
*
|
|
|
|
* Enter each coroutine that was previously marked for restart by
|
|
|
|
* qemu_co_queue_next() or qemu_co_queue_restart_all(). This function is
|
|
|
|
* invoked by the core coroutine code when the current coroutine yields or
|
|
|
|
* terminates.
|
|
|
|
*/
|
|
|
|
void qemu_co_queue_run_restart(Coroutine *co)
|
|
|
|
{
|
|
|
|
Coroutine *next;
|
|
|
|
|
|
|
|
trace_qemu_co_queue_run_restart(co);
|
2016-07-04 20:09:59 +03:00
|
|
|
while ((next = QSIMPLEQ_FIRST(&co->co_queue_wakeup))) {
|
|
|
|
QSIMPLEQ_REMOVE_HEAD(&co->co_queue_wakeup, co_queue_next);
|
coroutine: move entry argument to qemu_coroutine_create
In practice the entry argument is always known at creation time, and
it is confusing that sometimes qemu_coroutine_enter is used with a
non-NULL argument to re-enter a coroutine (this happens in
block/sheepdog.c and tests/test-coroutine.c). So pass the opaque value
at creation time, for consistency with e.g. aio_bh_new.
Mostly done with the following semantic patch:
@ entry1 @
expression entry, arg, co;
@@
- co = qemu_coroutine_create(entry);
+ co = qemu_coroutine_create(entry, arg);
...
- qemu_coroutine_enter(co, arg);
+ qemu_coroutine_enter(co);
@ entry2 @
expression entry, arg;
identifier co;
@@
- Coroutine *co = qemu_coroutine_create(entry);
+ Coroutine *co = qemu_coroutine_create(entry, arg);
...
- qemu_coroutine_enter(co, arg);
+ qemu_coroutine_enter(co);
@ entry3 @
expression entry, arg;
@@
- qemu_coroutine_enter(qemu_coroutine_create(entry), arg);
+ qemu_coroutine_enter(qemu_coroutine_create(entry, arg));
@ reentry @
expression co;
@@
- qemu_coroutine_enter(co, NULL);
+ qemu_coroutine_enter(co);
except for the aforementioned few places where the semantic patch
stumbled (as expected) and for test_co_queue, which would otherwise
produce an uninitialized variable warning.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Fam Zheng <famz@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2016-07-04 20:10:01 +03:00
|
|
|
qemu_coroutine_enter(next);
|
2013-05-17 17:51:26 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-07 16:41:50 +04:00
|
|
|
static bool qemu_co_queue_do_restart(CoQueue *queue, bool single)
|
2011-06-30 19:56:46 +04:00
|
|
|
{
|
|
|
|
Coroutine *next;
|
2013-03-07 16:41:50 +04:00
|
|
|
|
2016-07-04 20:09:59 +03:00
|
|
|
if (QSIMPLEQ_EMPTY(&queue->entries)) {
|
2013-03-07 16:41:50 +04:00
|
|
|
return false;
|
|
|
|
}
|
2011-06-30 19:56:46 +04:00
|
|
|
|
2016-07-04 20:09:59 +03:00
|
|
|
while ((next = QSIMPLEQ_FIRST(&queue->entries)) != NULL) {
|
|
|
|
QSIMPLEQ_REMOVE_HEAD(&queue->entries, co_queue_next);
|
2017-02-13 16:52:25 +03:00
|
|
|
aio_co_wake(next);
|
2013-03-07 16:41:50 +04:00
|
|
|
if (single) {
|
|
|
|
break;
|
|
|
|
}
|
2011-06-30 19:56:46 +04:00
|
|
|
}
|
2013-03-07 16:41:50 +04:00
|
|
|
return true;
|
|
|
|
}
|
2011-06-30 19:56:46 +04:00
|
|
|
|
2013-07-27 00:39:22 +04:00
|
|
|
bool coroutine_fn qemu_co_queue_next(CoQueue *queue)
|
2013-03-07 16:41:50 +04:00
|
|
|
{
|
2013-07-27 00:39:22 +04:00
|
|
|
assert(qemu_in_coroutine());
|
2013-03-07 16:41:50 +04:00
|
|
|
return qemu_co_queue_do_restart(queue, true);
|
2011-06-30 19:56:46 +04:00
|
|
|
}
|
|
|
|
|
2013-07-27 00:39:22 +04:00
|
|
|
void coroutine_fn qemu_co_queue_restart_all(CoQueue *queue)
|
2011-11-17 17:40:26 +04:00
|
|
|
{
|
2013-07-27 00:39:22 +04:00
|
|
|
assert(qemu_in_coroutine());
|
2013-03-07 16:41:50 +04:00
|
|
|
qemu_co_queue_do_restart(queue, false);
|
2011-11-17 17:40:26 +04:00
|
|
|
}
|
|
|
|
|
2013-07-27 00:39:22 +04:00
|
|
|
bool qemu_co_enter_next(CoQueue *queue)
|
|
|
|
{
|
|
|
|
Coroutine *next;
|
|
|
|
|
2016-07-04 20:09:59 +03:00
|
|
|
next = QSIMPLEQ_FIRST(&queue->entries);
|
2013-07-27 00:39:22 +04:00
|
|
|
if (!next) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-04 20:09:59 +03:00
|
|
|
QSIMPLEQ_REMOVE_HEAD(&queue->entries, co_queue_next);
|
coroutine: move entry argument to qemu_coroutine_create
In practice the entry argument is always known at creation time, and
it is confusing that sometimes qemu_coroutine_enter is used with a
non-NULL argument to re-enter a coroutine (this happens in
block/sheepdog.c and tests/test-coroutine.c). So pass the opaque value
at creation time, for consistency with e.g. aio_bh_new.
Mostly done with the following semantic patch:
@ entry1 @
expression entry, arg, co;
@@
- co = qemu_coroutine_create(entry);
+ co = qemu_coroutine_create(entry, arg);
...
- qemu_coroutine_enter(co, arg);
+ qemu_coroutine_enter(co);
@ entry2 @
expression entry, arg;
identifier co;
@@
- Coroutine *co = qemu_coroutine_create(entry);
+ Coroutine *co = qemu_coroutine_create(entry, arg);
...
- qemu_coroutine_enter(co, arg);
+ qemu_coroutine_enter(co);
@ entry3 @
expression entry, arg;
@@
- qemu_coroutine_enter(qemu_coroutine_create(entry), arg);
+ qemu_coroutine_enter(qemu_coroutine_create(entry, arg));
@ reentry @
expression co;
@@
- qemu_coroutine_enter(co, NULL);
+ qemu_coroutine_enter(co);
except for the aforementioned few places where the semantic patch
stumbled (as expected) and for test_co_queue, which would otherwise
produce an uninitialized variable warning.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Fam Zheng <famz@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2016-07-04 20:10:01 +03:00
|
|
|
qemu_coroutine_enter(next);
|
2013-07-27 00:39:22 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-06-30 19:56:46 +04:00
|
|
|
bool qemu_co_queue_empty(CoQueue *queue)
|
|
|
|
{
|
2016-07-04 20:09:59 +03:00
|
|
|
return QSIMPLEQ_FIRST(&queue->entries) == NULL;
|
2011-06-30 19:56:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void qemu_co_mutex_init(CoMutex *mutex)
|
|
|
|
{
|
|
|
|
memset(mutex, 0, sizeof(*mutex));
|
|
|
|
qemu_co_queue_init(&mutex->queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
void coroutine_fn qemu_co_mutex_lock(CoMutex *mutex)
|
|
|
|
{
|
|
|
|
Coroutine *self = qemu_coroutine_self();
|
|
|
|
|
|
|
|
trace_qemu_co_mutex_lock_entry(mutex, self);
|
|
|
|
|
|
|
|
while (mutex->locked) {
|
|
|
|
qemu_co_queue_wait(&mutex->queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex->locked = true;
|
2016-08-11 18:45:06 +03:00
|
|
|
mutex->holder = self;
|
2016-08-11 18:51:59 +03:00
|
|
|
self->locks_held++;
|
2011-06-30 19:56:46 +04:00
|
|
|
|
|
|
|
trace_qemu_co_mutex_lock_return(mutex, self);
|
|
|
|
}
|
|
|
|
|
|
|
|
void coroutine_fn qemu_co_mutex_unlock(CoMutex *mutex)
|
|
|
|
{
|
|
|
|
Coroutine *self = qemu_coroutine_self();
|
|
|
|
|
|
|
|
trace_qemu_co_mutex_unlock_entry(mutex, self);
|
|
|
|
|
|
|
|
assert(mutex->locked == true);
|
2016-08-11 18:45:06 +03:00
|
|
|
assert(mutex->holder == self);
|
2011-06-30 19:56:46 +04:00
|
|
|
assert(qemu_in_coroutine());
|
|
|
|
|
|
|
|
mutex->locked = false;
|
2016-08-11 18:45:06 +03:00
|
|
|
mutex->holder = NULL;
|
2016-08-11 18:51:59 +03:00
|
|
|
self->locks_held--;
|
2011-06-30 19:56:46 +04:00
|
|
|
qemu_co_queue_next(&mutex->queue);
|
|
|
|
|
|
|
|
trace_qemu_co_mutex_unlock_return(mutex, self);
|
|
|
|
}
|
2011-08-02 10:32:51 +04:00
|
|
|
|
|
|
|
void qemu_co_rwlock_init(CoRwlock *lock)
|
|
|
|
{
|
|
|
|
memset(lock, 0, sizeof(*lock));
|
|
|
|
qemu_co_queue_init(&lock->queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
void qemu_co_rwlock_rdlock(CoRwlock *lock)
|
|
|
|
{
|
2016-08-11 18:51:59 +03:00
|
|
|
Coroutine *self = qemu_coroutine_self();
|
|
|
|
|
2011-08-02 10:32:51 +04:00
|
|
|
while (lock->writer) {
|
|
|
|
qemu_co_queue_wait(&lock->queue);
|
|
|
|
}
|
|
|
|
lock->reader++;
|
2016-08-11 18:51:59 +03:00
|
|
|
self->locks_held++;
|
2011-08-02 10:32:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void qemu_co_rwlock_unlock(CoRwlock *lock)
|
|
|
|
{
|
2016-08-11 18:51:59 +03:00
|
|
|
Coroutine *self = qemu_coroutine_self();
|
|
|
|
|
2011-08-02 10:32:51 +04:00
|
|
|
assert(qemu_in_coroutine());
|
|
|
|
if (lock->writer) {
|
|
|
|
lock->writer = false;
|
2011-11-17 17:40:26 +04:00
|
|
|
qemu_co_queue_restart_all(&lock->queue);
|
2011-08-02 10:32:51 +04:00
|
|
|
} else {
|
|
|
|
lock->reader--;
|
|
|
|
assert(lock->reader >= 0);
|
|
|
|
/* Wakeup only one waiting writer */
|
|
|
|
if (!lock->reader) {
|
|
|
|
qemu_co_queue_next(&lock->queue);
|
|
|
|
}
|
|
|
|
}
|
2016-08-11 18:51:59 +03:00
|
|
|
self->locks_held--;
|
2011-08-02 10:32:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void qemu_co_rwlock_wrlock(CoRwlock *lock)
|
|
|
|
{
|
2016-08-11 18:51:59 +03:00
|
|
|
Coroutine *self = qemu_coroutine_self();
|
|
|
|
|
2011-08-02 10:32:51 +04:00
|
|
|
while (lock->writer || lock->reader) {
|
|
|
|
qemu_co_queue_wait(&lock->queue);
|
|
|
|
}
|
|
|
|
lock->writer = true;
|
2016-08-11 18:51:59 +03:00
|
|
|
self->locks_held++;
|
2011-08-02 10:32:51 +04:00
|
|
|
}
|