vvfat: convert to .bdrv_co_is_allocated()
[qemu-kvm.git] / qemu-coroutine-lock.c
blob9549c075ee7de3ebdef701c2f603a96516a36911
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-common.h"
26 #include "qemu-coroutine.h"
27 #include "qemu-coroutine-int.h"
28 #include "qemu-queue.h"
29 #include "main-loop.h"
30 #include "trace.h"
32 static QTAILQ_HEAD(, Coroutine) unlock_bh_queue =
33 QTAILQ_HEAD_INITIALIZER(unlock_bh_queue);
34 static QEMUBH* unlock_bh;
36 static void qemu_co_queue_next_bh(void *opaque)
38 Coroutine *next;
40 trace_qemu_co_queue_next_bh();
41 while ((next = QTAILQ_FIRST(&unlock_bh_queue))) {
42 QTAILQ_REMOVE(&unlock_bh_queue, next, co_queue_next);
43 qemu_coroutine_enter(next, NULL);
47 void qemu_co_queue_init(CoQueue *queue)
49 QTAILQ_INIT(&queue->entries);
51 if (!unlock_bh) {
52 unlock_bh = qemu_bh_new(qemu_co_queue_next_bh, NULL);
56 void coroutine_fn qemu_co_queue_wait(CoQueue *queue)
58 Coroutine *self = qemu_coroutine_self();
59 QTAILQ_INSERT_TAIL(&queue->entries, self, co_queue_next);
60 qemu_coroutine_yield();
61 assert(qemu_in_coroutine());
64 void coroutine_fn qemu_co_queue_wait_insert_head(CoQueue *queue)
66 Coroutine *self = qemu_coroutine_self();
67 QTAILQ_INSERT_HEAD(&queue->entries, self, co_queue_next);
68 qemu_coroutine_yield();
69 assert(qemu_in_coroutine());
72 bool qemu_co_queue_next(CoQueue *queue)
74 Coroutine *next;
76 next = QTAILQ_FIRST(&queue->entries);
77 if (next) {
78 QTAILQ_REMOVE(&queue->entries, next, co_queue_next);
79 QTAILQ_INSERT_TAIL(&unlock_bh_queue, next, co_queue_next);
80 trace_qemu_co_queue_next(next);
81 qemu_bh_schedule(unlock_bh);
84 return (next != NULL);
87 bool qemu_co_queue_empty(CoQueue *queue)
89 return (QTAILQ_FIRST(&queue->entries) == NULL);
92 void qemu_co_mutex_init(CoMutex *mutex)
94 memset(mutex, 0, sizeof(*mutex));
95 qemu_co_queue_init(&mutex->queue);
98 void coroutine_fn qemu_co_mutex_lock(CoMutex *mutex)
100 Coroutine *self = qemu_coroutine_self();
102 trace_qemu_co_mutex_lock_entry(mutex, self);
104 while (mutex->locked) {
105 qemu_co_queue_wait(&mutex->queue);
108 mutex->locked = true;
110 trace_qemu_co_mutex_lock_return(mutex, self);
113 void coroutine_fn qemu_co_mutex_unlock(CoMutex *mutex)
115 Coroutine *self = qemu_coroutine_self();
117 trace_qemu_co_mutex_unlock_entry(mutex, self);
119 assert(mutex->locked == true);
120 assert(qemu_in_coroutine());
122 mutex->locked = false;
123 qemu_co_queue_next(&mutex->queue);
125 trace_qemu_co_mutex_unlock_return(mutex, self);
128 void qemu_co_rwlock_init(CoRwlock *lock)
130 memset(lock, 0, sizeof(*lock));
131 qemu_co_queue_init(&lock->queue);
134 void qemu_co_rwlock_rdlock(CoRwlock *lock)
136 while (lock->writer) {
137 qemu_co_queue_wait(&lock->queue);
139 lock->reader++;
142 void qemu_co_rwlock_unlock(CoRwlock *lock)
144 assert(qemu_in_coroutine());
145 if (lock->writer) {
146 lock->writer = false;
147 while (!qemu_co_queue_empty(&lock->queue)) {
149 * Wakeup every body. This will include some
150 * writers too.
152 qemu_co_queue_next(&lock->queue);
154 } else {
155 lock->reader--;
156 assert(lock->reader >= 0);
157 /* Wakeup only one waiting writer */
158 if (!lock->reader) {
159 qemu_co_queue_next(&lock->queue);
164 void qemu_co_rwlock_wrlock(CoRwlock *lock)
166 while (lock->writer || lock->reader) {
167 qemu_co_queue_wait(&lock->queue);
169 lock->writer = true;