]> git.proxmox.com Git - mirror_qemu.git/blob - qemu-coroutine-lock.c
coroutines: Locks
[mirror_qemu.git] / 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-common.h"
26 #include "qemu-coroutine.h"
27 #include "qemu-coroutine-int.h"
28 #include "qemu-queue.h"
29 #include "trace.h"
30
31 static QTAILQ_HEAD(, Coroutine) unlock_bh_queue =
32 QTAILQ_HEAD_INITIALIZER(unlock_bh_queue);
33
34 struct unlock_bh {
35 QEMUBH *bh;
36 };
37
38 static void qemu_co_queue_next_bh(void *opaque)
39 {
40 struct unlock_bh *unlock_bh = opaque;
41 Coroutine *next;
42
43 trace_qemu_co_queue_next_bh();
44 while ((next = QTAILQ_FIRST(&unlock_bh_queue))) {
45 QTAILQ_REMOVE(&unlock_bh_queue, next, co_queue_next);
46 qemu_coroutine_enter(next, NULL);
47 }
48
49 qemu_bh_delete(unlock_bh->bh);
50 qemu_free(unlock_bh);
51 }
52
53 void qemu_co_queue_init(CoQueue *queue)
54 {
55 QTAILQ_INIT(&queue->entries);
56 }
57
58 void coroutine_fn qemu_co_queue_wait(CoQueue *queue)
59 {
60 Coroutine *self = qemu_coroutine_self();
61 QTAILQ_INSERT_TAIL(&queue->entries, self, co_queue_next);
62 qemu_coroutine_yield();
63 assert(qemu_in_coroutine());
64 }
65
66 bool qemu_co_queue_next(CoQueue *queue)
67 {
68 struct unlock_bh *unlock_bh;
69 Coroutine *next;
70
71 next = QTAILQ_FIRST(&queue->entries);
72 if (next) {
73 QTAILQ_REMOVE(&queue->entries, next, co_queue_next);
74 QTAILQ_INSERT_TAIL(&unlock_bh_queue, next, co_queue_next);
75 trace_qemu_co_queue_next(next);
76
77 unlock_bh = qemu_malloc(sizeof(*unlock_bh));
78 unlock_bh->bh = qemu_bh_new(qemu_co_queue_next_bh, unlock_bh);
79 qemu_bh_schedule(unlock_bh->bh);
80 }
81
82 return (next != NULL);
83 }
84
85 bool qemu_co_queue_empty(CoQueue *queue)
86 {
87 return (QTAILQ_FIRST(&queue->entries) == NULL);
88 }
89
90 void qemu_co_mutex_init(CoMutex *mutex)
91 {
92 memset(mutex, 0, sizeof(*mutex));
93 qemu_co_queue_init(&mutex->queue);
94 }
95
96 void coroutine_fn qemu_co_mutex_lock(CoMutex *mutex)
97 {
98 Coroutine *self = qemu_coroutine_self();
99
100 trace_qemu_co_mutex_lock_entry(mutex, self);
101
102 while (mutex->locked) {
103 qemu_co_queue_wait(&mutex->queue);
104 }
105
106 mutex->locked = true;
107
108 trace_qemu_co_mutex_lock_return(mutex, self);
109 }
110
111 void coroutine_fn qemu_co_mutex_unlock(CoMutex *mutex)
112 {
113 Coroutine *self = qemu_coroutine_self();
114
115 trace_qemu_co_mutex_unlock_entry(mutex, self);
116
117 assert(mutex->locked == true);
118 assert(qemu_in_coroutine());
119
120 mutex->locked = false;
121 qemu_co_queue_next(&mutex->queue);
122
123 trace_qemu_co_mutex_unlock_return(mutex, self);
124 }