1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#include "qemu/osdep.h"
30#include "qemu/coroutine_int.h"
31#include "qemu/processor.h"
32#include "qemu/queue.h"
33#include "block/aio.h"
34#include "trace.h"
35
36void qemu_co_queue_init(CoQueue *queue)
37{
38 QSIMPLEQ_INIT(&queue->entries);
39}
40
41void coroutine_fn qemu_co_queue_wait_impl(CoQueue *queue, QemuLockable *lock,
42 CoQueueWaitFlags flags)
43{
44 Coroutine *self = qemu_coroutine_self();
45 if (flags & CO_QUEUE_WAIT_FRONT) {
46 QSIMPLEQ_INSERT_HEAD(&queue->entries, self, co_queue_next);
47 } else {
48 QSIMPLEQ_INSERT_TAIL(&queue->entries, self, co_queue_next);
49 }
50
51 if (lock) {
52 qemu_lockable_unlock(lock);
53 }
54
55
56
57
58
59
60 qemu_coroutine_yield();
61 assert(qemu_in_coroutine());
62
63
64
65
66
67
68
69 if (lock) {
70 qemu_lockable_lock(lock);
71 }
72}
73
74bool qemu_co_enter_next_impl(CoQueue *queue, QemuLockable *lock)
75{
76 Coroutine *next;
77
78 next = QSIMPLEQ_FIRST(&queue->entries);
79 if (!next) {
80 return false;
81 }
82
83 QSIMPLEQ_REMOVE_HEAD(&queue->entries, co_queue_next);
84 if (lock) {
85 qemu_lockable_unlock(lock);
86 }
87 aio_co_wake(next);
88 if (lock) {
89 qemu_lockable_lock(lock);
90 }
91 return true;
92}
93
94bool coroutine_fn qemu_co_queue_next(CoQueue *queue)
95{
96
97 return qemu_co_enter_next_impl(queue, NULL);
98}
99
100void qemu_co_enter_all_impl(CoQueue *queue, QemuLockable *lock)
101{
102 while (qemu_co_enter_next_impl(queue, lock)) {
103
104 }
105}
106
107void coroutine_fn qemu_co_queue_restart_all(CoQueue *queue)
108{
109
110 qemu_co_enter_all_impl(queue, NULL);
111}
112
113bool qemu_co_queue_empty(CoQueue *queue)
114{
115 return QSIMPLEQ_FIRST(&queue->entries) == NULL;
116}
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137typedef struct CoWaitRecord {
138 Coroutine *co;
139 QSLIST_ENTRY(CoWaitRecord) next;
140} CoWaitRecord;
141
142static void coroutine_fn push_waiter(CoMutex *mutex, CoWaitRecord *w)
143{
144 w->co = qemu_coroutine_self();
145 QSLIST_INSERT_HEAD_ATOMIC(&mutex->from_push, w, next);
146}
147
148static void move_waiters(CoMutex *mutex)
149{
150 QSLIST_HEAD(, CoWaitRecord) reversed;
151 QSLIST_MOVE_ATOMIC(&reversed, &mutex->from_push);
152 while (!QSLIST_EMPTY(&reversed)) {
153 CoWaitRecord *w = QSLIST_FIRST(&reversed);
154 QSLIST_REMOVE_HEAD(&reversed, next);
155 QSLIST_INSERT_HEAD(&mutex->to_pop, w, next);
156 }
157}
158
159static CoWaitRecord *pop_waiter(CoMutex *mutex)
160{
161 CoWaitRecord *w;
162
163 if (QSLIST_EMPTY(&mutex->to_pop)) {
164 move_waiters(mutex);
165 if (QSLIST_EMPTY(&mutex->to_pop)) {
166 return NULL;
167 }
168 }
169 w = QSLIST_FIRST(&mutex->to_pop);
170 QSLIST_REMOVE_HEAD(&mutex->to_pop, next);
171 return w;
172}
173
174static bool has_waiters(CoMutex *mutex)
175{
176 return QSLIST_EMPTY(&mutex->to_pop) || QSLIST_EMPTY(&mutex->from_push);
177}
178
179void qemu_co_mutex_init(CoMutex *mutex)
180{
181 memset(mutex, 0, sizeof(*mutex));
182}
183
184static void coroutine_fn qemu_co_mutex_wake(CoMutex *mutex, Coroutine *co)
185{
186
187
188
189 smp_read_barrier_depends();
190 mutex->ctx = co->ctx;
191 aio_co_wake(co);
192}
193
194static void coroutine_fn qemu_co_mutex_lock_slowpath(AioContext *ctx,
195 CoMutex *mutex)
196{
197 Coroutine *self = qemu_coroutine_self();
198 CoWaitRecord w;
199 unsigned old_handoff;
200
201 trace_qemu_co_mutex_lock_entry(mutex, self);
202 push_waiter(mutex, &w);
203
204
205
206
207
208 smp_mb__after_rmw();
209
210
211
212
213 old_handoff = qatomic_read(&mutex->handoff);
214 if (old_handoff &&
215 has_waiters(mutex) &&
216 qatomic_cmpxchg(&mutex->handoff, old_handoff, 0) == old_handoff) {
217
218
219
220 CoWaitRecord *to_wake = pop_waiter(mutex);
221 Coroutine *co = to_wake->co;
222 if (co == self) {
223
224 assert(to_wake == &w);
225 mutex->ctx = ctx;
226 return;
227 }
228
229 qemu_co_mutex_wake(mutex, co);
230 }
231
232 qemu_coroutine_yield();
233 trace_qemu_co_mutex_lock_return(mutex, self);
234}
235
236void coroutine_fn qemu_co_mutex_lock(CoMutex *mutex)
237{
238 AioContext *ctx = qemu_get_current_aio_context();
239 Coroutine *self = qemu_coroutine_self();
240 int waiters, i;
241
242
243
244
245
246
247
248
249 i = 0;
250retry_fast_path:
251 waiters = qatomic_cmpxchg(&mutex->locked, 0, 1);
252 if (waiters != 0) {
253 while (waiters == 1 && ++i < 1000) {
254 if (qatomic_read(&mutex->ctx) == ctx) {
255 break;
256 }
257 if (qatomic_read(&mutex->locked) == 0) {
258 goto retry_fast_path;
259 }
260 cpu_relax();
261 }
262 waiters = qatomic_fetch_inc(&mutex->locked);
263 }
264
265 if (waiters == 0) {
266
267 trace_qemu_co_mutex_lock_uncontended(mutex, self);
268 mutex->ctx = ctx;
269 } else {
270 qemu_co_mutex_lock_slowpath(ctx, mutex);
271 }
272 mutex->holder = self;
273 self->locks_held++;
274}
275
276void coroutine_fn qemu_co_mutex_unlock(CoMutex *mutex)
277{
278 Coroutine *self = qemu_coroutine_self();
279
280 trace_qemu_co_mutex_unlock_entry(mutex, self);
281
282 assert(mutex->locked);
283 assert(mutex->holder == self);
284 assert(qemu_in_coroutine());
285
286 mutex->ctx = NULL;
287 mutex->holder = NULL;
288 self->locks_held--;
289 if (qatomic_fetch_dec(&mutex->locked) == 1) {
290
291 return;
292 }
293
294 for (;;) {
295 CoWaitRecord *to_wake = pop_waiter(mutex);
296 unsigned our_handoff;
297
298 if (to_wake) {
299 qemu_co_mutex_wake(mutex, to_wake->co);
300 break;
301 }
302
303
304
305
306
307 if (++mutex->sequence == 0) {
308 mutex->sequence = 1;
309 }
310
311 our_handoff = mutex->sequence;
312
313 qatomic_mb_set(&mutex->handoff, our_handoff);
314 if (!has_waiters(mutex)) {
315
316
317
318 break;
319 }
320
321
322
323
324 if (qatomic_cmpxchg(&mutex->handoff, our_handoff, 0) != our_handoff) {
325 break;
326 }
327 }
328
329 trace_qemu_co_mutex_unlock_return(mutex, self);
330}
331
332struct CoRwTicket {
333 bool read;
334 Coroutine *co;
335 QSIMPLEQ_ENTRY(CoRwTicket) next;
336};
337
338void qemu_co_rwlock_init(CoRwlock *lock)
339{
340 qemu_co_mutex_init(&lock->mutex);
341 lock->owners = 0;
342 QSIMPLEQ_INIT(&lock->tickets);
343}
344
345
346static void coroutine_fn qemu_co_rwlock_maybe_wake_one(CoRwlock *lock)
347{
348 CoRwTicket *tkt = QSIMPLEQ_FIRST(&lock->tickets);
349 Coroutine *co = NULL;
350
351
352
353
354
355
356 if (tkt) {
357 if (tkt->read) {
358 if (lock->owners >= 0) {
359 lock->owners++;
360 co = tkt->co;
361 }
362 } else {
363 if (lock->owners == 0) {
364 lock->owners = -1;
365 co = tkt->co;
366 }
367 }
368 }
369
370 if (co) {
371 QSIMPLEQ_REMOVE_HEAD(&lock->tickets, next);
372 qemu_co_mutex_unlock(&lock->mutex);
373 aio_co_wake(co);
374 } else {
375 qemu_co_mutex_unlock(&lock->mutex);
376 }
377}
378
379void coroutine_fn qemu_co_rwlock_rdlock(CoRwlock *lock)
380{
381 Coroutine *self = qemu_coroutine_self();
382
383 qemu_co_mutex_lock(&lock->mutex);
384
385 if (lock->owners == 0 || (lock->owners > 0 && QSIMPLEQ_EMPTY(&lock->tickets))) {
386 lock->owners++;
387 qemu_co_mutex_unlock(&lock->mutex);
388 } else {
389 CoRwTicket my_ticket = { true, self };
390
391 QSIMPLEQ_INSERT_TAIL(&lock->tickets, &my_ticket, next);
392 qemu_co_mutex_unlock(&lock->mutex);
393 qemu_coroutine_yield();
394 assert(lock->owners >= 1);
395
396
397 qemu_co_mutex_lock(&lock->mutex);
398 qemu_co_rwlock_maybe_wake_one(lock);
399 }
400
401 self->locks_held++;
402}
403
404void coroutine_fn qemu_co_rwlock_unlock(CoRwlock *lock)
405{
406 Coroutine *self = qemu_coroutine_self();
407
408 assert(qemu_in_coroutine());
409 self->locks_held--;
410
411 qemu_co_mutex_lock(&lock->mutex);
412 if (lock->owners > 0) {
413 lock->owners--;
414 } else {
415 assert(lock->owners == -1);
416 lock->owners = 0;
417 }
418
419 qemu_co_rwlock_maybe_wake_one(lock);
420}
421
422void coroutine_fn qemu_co_rwlock_downgrade(CoRwlock *lock)
423{
424 qemu_co_mutex_lock(&lock->mutex);
425 assert(lock->owners == -1);
426 lock->owners = 1;
427
428
429 qemu_co_rwlock_maybe_wake_one(lock);
430}
431
432void coroutine_fn qemu_co_rwlock_wrlock(CoRwlock *lock)
433{
434 Coroutine *self = qemu_coroutine_self();
435
436 qemu_co_mutex_lock(&lock->mutex);
437 if (lock->owners == 0) {
438 lock->owners = -1;
439 qemu_co_mutex_unlock(&lock->mutex);
440 } else {
441 CoRwTicket my_ticket = { false, qemu_coroutine_self() };
442
443 QSIMPLEQ_INSERT_TAIL(&lock->tickets, &my_ticket, next);
444 qemu_co_mutex_unlock(&lock->mutex);
445 qemu_coroutine_yield();
446 assert(lock->owners == -1);
447 }
448
449 self->locks_held++;
450}
451
452void coroutine_fn qemu_co_rwlock_upgrade(CoRwlock *lock)
453{
454 qemu_co_mutex_lock(&lock->mutex);
455 assert(lock->owners > 0);
456
457 if (lock->owners == 1 && QSIMPLEQ_EMPTY(&lock->tickets)) {
458 lock->owners = -1;
459 qemu_co_mutex_unlock(&lock->mutex);
460 } else {
461 CoRwTicket my_ticket = { false, qemu_coroutine_self() };
462
463 lock->owners--;
464 QSIMPLEQ_INSERT_TAIL(&lock->tickets, &my_ticket, next);
465 qemu_co_rwlock_maybe_wake_one(lock);
466 qemu_coroutine_yield();
467 assert(lock->owners == -1);
468 }
469}
470