target-i386: Introduce X86CPU::filtered_features field
[qemu.git] / qemu-coroutine-lock.c
blob86efe1f86a486da60ae03148dc521223703d7b49
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 "block/coroutine.h"
27 #include "block/coroutine_int.h"
28 #include "qemu/queue.h"
29 #include "block/aio.h"
30 #include "trace.h"
32 /* Coroutines are awoken from a BH to allow the current coroutine to complete
33 * its flow of execution. The BH may run after the CoQueue has been destroyed,
34 * so keep BH data in a separate heap-allocated struct.
36 typedef struct {
37 QEMUBH *bh;
38 QTAILQ_HEAD(, Coroutine) entries;
39 } CoQueueNextData;
41 static void qemu_co_queue_next_bh(void *opaque)
43 CoQueueNextData *data = opaque;
44 Coroutine *next;
46 trace_qemu_co_queue_next_bh();
47 while ((next = QTAILQ_FIRST(&data->entries))) {
48 QTAILQ_REMOVE(&data->entries, next, co_queue_next);
49 qemu_coroutine_enter(next, NULL);
52 qemu_bh_delete(data->bh);
53 g_slice_free(CoQueueNextData, data);
56 void qemu_co_queue_init(CoQueue *queue)
58 QTAILQ_INIT(&queue->entries);
60 /* This will be exposed to callers once there are multiple AioContexts */
61 queue->ctx = qemu_get_aio_context();
64 void coroutine_fn qemu_co_queue_wait(CoQueue *queue)
66 Coroutine *self = qemu_coroutine_self();
67 QTAILQ_INSERT_TAIL(&queue->entries, self, co_queue_next);
68 qemu_coroutine_yield();
69 assert(qemu_in_coroutine());
72 void coroutine_fn qemu_co_queue_wait_insert_head(CoQueue *queue)
74 Coroutine *self = qemu_coroutine_self();
75 QTAILQ_INSERT_HEAD(&queue->entries, self, co_queue_next);
76 qemu_coroutine_yield();
77 assert(qemu_in_coroutine());
80 static bool qemu_co_queue_do_restart(CoQueue *queue, bool single)
82 Coroutine *next;
83 CoQueueNextData *data;
85 if (QTAILQ_EMPTY(&queue->entries)) {
86 return false;
89 data = g_slice_new(CoQueueNextData);
90 data->bh = aio_bh_new(queue->ctx, qemu_co_queue_next_bh, data);
91 QTAILQ_INIT(&data->entries);
92 qemu_bh_schedule(data->bh);
94 while ((next = QTAILQ_FIRST(&queue->entries)) != NULL) {
95 QTAILQ_REMOVE(&queue->entries, next, co_queue_next);
96 QTAILQ_INSERT_TAIL(&data->entries, next, co_queue_next);
97 trace_qemu_co_queue_next(next);
98 if (single) {
99 break;
102 return true;
105 bool qemu_co_queue_next(CoQueue *queue)
107 return qemu_co_queue_do_restart(queue, true);
110 void qemu_co_queue_restart_all(CoQueue *queue)
112 qemu_co_queue_do_restart(queue, false);
115 bool qemu_co_queue_empty(CoQueue *queue)
117 return (QTAILQ_FIRST(&queue->entries) == NULL);
120 void qemu_co_mutex_init(CoMutex *mutex)
122 memset(mutex, 0, sizeof(*mutex));
123 qemu_co_queue_init(&mutex->queue);
126 void coroutine_fn qemu_co_mutex_lock(CoMutex *mutex)
128 Coroutine *self = qemu_coroutine_self();
130 trace_qemu_co_mutex_lock_entry(mutex, self);
132 while (mutex->locked) {
133 qemu_co_queue_wait(&mutex->queue);
136 mutex->locked = true;
138 trace_qemu_co_mutex_lock_return(mutex, self);
141 void coroutine_fn qemu_co_mutex_unlock(CoMutex *mutex)
143 Coroutine *self = qemu_coroutine_self();
145 trace_qemu_co_mutex_unlock_entry(mutex, self);
147 assert(mutex->locked == true);
148 assert(qemu_in_coroutine());
150 mutex->locked = false;
151 qemu_co_queue_next(&mutex->queue);
153 trace_qemu_co_mutex_unlock_return(mutex, self);
156 void qemu_co_rwlock_init(CoRwlock *lock)
158 memset(lock, 0, sizeof(*lock));
159 qemu_co_queue_init(&lock->queue);
162 void qemu_co_rwlock_rdlock(CoRwlock *lock)
164 while (lock->writer) {
165 qemu_co_queue_wait(&lock->queue);
167 lock->reader++;
170 void qemu_co_rwlock_unlock(CoRwlock *lock)
172 assert(qemu_in_coroutine());
173 if (lock->writer) {
174 lock->writer = false;
175 qemu_co_queue_restart_all(&lock->queue);
176 } else {
177 lock->reader--;
178 assert(lock->reader >= 0);
179 /* Wakeup only one waiting writer */
180 if (!lock->reader) {
181 qemu_co_queue_next(&lock->queue);
186 void qemu_co_rwlock_wrlock(CoRwlock *lock)
188 while (lock->writer || lock->reader) {
189 qemu_co_queue_wait(&lock->queue);
191 lock->writer = true;