]> git.proxmox.com Git - mirror_qemu.git/blob - util/qemu-coroutine-lock.c
coroutine-lock: reschedule coroutine on the AioContext it was running on
[mirror_qemu.git] / util / qemu-coroutine-lock.c
1 /*
2 * coroutine queues and locks
3 *
4 * Copyright (c) 2011 Kevin Wolf <kwolf@redhat.com>
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 */
24
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"
32
33 void qemu_co_queue_init(CoQueue *queue)
34 {
35 QSIMPLEQ_INIT(&queue->entries);
36 }
37
38 void coroutine_fn qemu_co_queue_wait(CoQueue *queue)
39 {
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());
44 }
45
46 /**
47 * qemu_co_queue_run_restart:
48 *
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.
53 */
54 void qemu_co_queue_run_restart(Coroutine *co)
55 {
56 Coroutine *next;
57
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);
62 }
63 }
64
65 static bool qemu_co_queue_do_restart(CoQueue *queue, bool single)
66 {
67 Coroutine *next;
68
69 if (QSIMPLEQ_EMPTY(&queue->entries)) {
70 return false;
71 }
72
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;
78 }
79 }
80 return true;
81 }
82
83 bool coroutine_fn qemu_co_queue_next(CoQueue *queue)
84 {
85 assert(qemu_in_coroutine());
86 return qemu_co_queue_do_restart(queue, true);
87 }
88
89 void coroutine_fn qemu_co_queue_restart_all(CoQueue *queue)
90 {
91 assert(qemu_in_coroutine());
92 qemu_co_queue_do_restart(queue, false);
93 }
94
95 bool qemu_co_enter_next(CoQueue *queue)
96 {
97 Coroutine *next;
98
99 next = QSIMPLEQ_FIRST(&queue->entries);
100 if (!next) {
101 return false;
102 }
103
104 QSIMPLEQ_REMOVE_HEAD(&queue->entries, co_queue_next);
105 qemu_coroutine_enter(next);
106 return true;
107 }
108
109 bool qemu_co_queue_empty(CoQueue *queue)
110 {
111 return QSIMPLEQ_FIRST(&queue->entries) == NULL;
112 }
113
114 void qemu_co_mutex_init(CoMutex *mutex)
115 {
116 memset(mutex, 0, sizeof(*mutex));
117 qemu_co_queue_init(&mutex->queue);
118 }
119
120 void coroutine_fn qemu_co_mutex_lock(CoMutex *mutex)
121 {
122 Coroutine *self = qemu_coroutine_self();
123
124 trace_qemu_co_mutex_lock_entry(mutex, self);
125
126 while (mutex->locked) {
127 qemu_co_queue_wait(&mutex->queue);
128 }
129
130 mutex->locked = true;
131 mutex->holder = self;
132 self->locks_held++;
133
134 trace_qemu_co_mutex_lock_return(mutex, self);
135 }
136
137 void coroutine_fn qemu_co_mutex_unlock(CoMutex *mutex)
138 {
139 Coroutine *self = qemu_coroutine_self();
140
141 trace_qemu_co_mutex_unlock_entry(mutex, self);
142
143 assert(mutex->locked == true);
144 assert(mutex->holder == self);
145 assert(qemu_in_coroutine());
146
147 mutex->locked = false;
148 mutex->holder = NULL;
149 self->locks_held--;
150 qemu_co_queue_next(&mutex->queue);
151
152 trace_qemu_co_mutex_unlock_return(mutex, self);
153 }
154
155 void qemu_co_rwlock_init(CoRwlock *lock)
156 {
157 memset(lock, 0, sizeof(*lock));
158 qemu_co_queue_init(&lock->queue);
159 }
160
161 void qemu_co_rwlock_rdlock(CoRwlock *lock)
162 {
163 Coroutine *self = qemu_coroutine_self();
164
165 while (lock->writer) {
166 qemu_co_queue_wait(&lock->queue);
167 }
168 lock->reader++;
169 self->locks_held++;
170 }
171
172 void qemu_co_rwlock_unlock(CoRwlock *lock)
173 {
174 Coroutine *self = qemu_coroutine_self();
175
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);
186 }
187 }
188 self->locks_held--;
189 }
190
191 void qemu_co_rwlock_wrlock(CoRwlock *lock)
192 {
193 Coroutine *self = qemu_coroutine_self();
194
195 while (lock->writer || lock->reader) {
196 qemu_co_queue_wait(&lock->queue);
197 }
198 lock->writer = true;
199 self->locks_held++;
200 }