block: explicitly acquire aiocontext in bottom halves that need it
[qemu/ar7.git] / util / qemu-coroutine-lock.c
blobe6afd1aa6c1a32f5af08737e5088c13b7333da86
1 /*
2 * coroutine queues and locks
4 * Copyright (c) 2011 Kevin Wolf <kwolf@redhat.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
26 #include "qemu-common.h"
27 #include "qemu/coroutine.h"
28 #include "qemu/coroutine_int.h"
29 #include "qemu/queue.h"
30 #include "block/aio.h"
31 #include "trace.h"
33 void qemu_co_queue_init(CoQueue *queue)
35 QSIMPLEQ_INIT(&queue->entries);
38 void coroutine_fn qemu_co_queue_wait(CoQueue *queue)
40 Coroutine *self = qemu_coroutine_self();
41 QSIMPLEQ_INSERT_TAIL(&queue->entries, self, co_queue_next);
42 qemu_coroutine_yield();
43 assert(qemu_in_coroutine());
46 /**
47 * qemu_co_queue_run_restart:
49 * Enter each coroutine that was previously marked for restart by
50 * qemu_co_queue_next() or qemu_co_queue_restart_all(). This function is
51 * invoked by the core coroutine code when the current coroutine yields or
52 * terminates.
54 void qemu_co_queue_run_restart(Coroutine *co)
56 Coroutine *next;
58 trace_qemu_co_queue_run_restart(co);
59 while ((next = QSIMPLEQ_FIRST(&co->co_queue_wakeup))) {
60 QSIMPLEQ_REMOVE_HEAD(&co->co_queue_wakeup, co_queue_next);
61 qemu_coroutine_enter(next);
65 static bool qemu_co_queue_do_restart(CoQueue *queue, bool single)
67 Coroutine *next;
69 if (QSIMPLEQ_EMPTY(&queue->entries)) {
70 return false;
73 while ((next = QSIMPLEQ_FIRST(&queue->entries)) != NULL) {
74 QSIMPLEQ_REMOVE_HEAD(&queue->entries, co_queue_next);
75 aio_co_wake(next);
76 if (single) {
77 break;
80 return true;
83 bool coroutine_fn qemu_co_queue_next(CoQueue *queue)
85 assert(qemu_in_coroutine());
86 return qemu_co_queue_do_restart(queue, true);
89 void coroutine_fn qemu_co_queue_restart_all(CoQueue *queue)
91 assert(qemu_in_coroutine());
92 qemu_co_queue_do_restart(queue, false);
95 bool qemu_co_enter_next(CoQueue *queue)
97 Coroutine *next;
99 next = QSIMPLEQ_FIRST(&queue->entries);
100 if (!next) {
101 return false;
104 QSIMPLEQ_REMOVE_HEAD(&queue->entries, co_queue_next);
105 qemu_coroutine_enter(next);
106 return true;
109 bool qemu_co_queue_empty(CoQueue *queue)
111 return QSIMPLEQ_FIRST(&queue->entries) == NULL;
114 void qemu_co_mutex_init(CoMutex *mutex)
116 memset(mutex, 0, sizeof(*mutex));
117 qemu_co_queue_init(&mutex->queue);
120 void coroutine_fn qemu_co_mutex_lock(CoMutex *mutex)
122 Coroutine *self = qemu_coroutine_self();
124 trace_qemu_co_mutex_lock_entry(mutex, self);
126 while (mutex->locked) {
127 qemu_co_queue_wait(&mutex->queue);
130 mutex->locked = true;
131 mutex->holder = self;
132 self->locks_held++;
134 trace_qemu_co_mutex_lock_return(mutex, self);
137 void coroutine_fn qemu_co_mutex_unlock(CoMutex *mutex)
139 Coroutine *self = qemu_coroutine_self();
141 trace_qemu_co_mutex_unlock_entry(mutex, self);
143 assert(mutex->locked == true);
144 assert(mutex->holder == self);
145 assert(qemu_in_coroutine());
147 mutex->locked = false;
148 mutex->holder = NULL;
149 self->locks_held--;
150 qemu_co_queue_next(&mutex->queue);
152 trace_qemu_co_mutex_unlock_return(mutex, self);
155 void qemu_co_rwlock_init(CoRwlock *lock)
157 memset(lock, 0, sizeof(*lock));
158 qemu_co_queue_init(&lock->queue);
161 void qemu_co_rwlock_rdlock(CoRwlock *lock)
163 Coroutine *self = qemu_coroutine_self();
165 while (lock->writer) {
166 qemu_co_queue_wait(&lock->queue);
168 lock->reader++;
169 self->locks_held++;
172 void qemu_co_rwlock_unlock(CoRwlock *lock)
174 Coroutine *self = qemu_coroutine_self();
176 assert(qemu_in_coroutine());
177 if (lock->writer) {
178 lock->writer = false;
179 qemu_co_queue_restart_all(&lock->queue);
180 } else {
181 lock->reader--;
182 assert(lock->reader >= 0);
183 /* Wakeup only one waiting writer */
184 if (!lock->reader) {
185 qemu_co_queue_next(&lock->queue);
188 self->locks_held--;
191 void qemu_co_rwlock_wrlock(CoRwlock *lock)
193 Coroutine *self = qemu_coroutine_self();
195 while (lock->writer || lock->reader) {
196 qemu_co_queue_wait(&lock->queue);
198 lock->writer = true;
199 self->locks_held++;