]>
Commit | Line | Data |
---|---|---|
b96e9247 KW |
1 | /* |
2 | * coroutine queues and locks | |
3 | * | |
4 | * Copyright (c) 2011 Kevin Wolf <[email protected]> | |
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 | ||
aafd7584 | 25 | #include "qemu/osdep.h" |
b96e9247 | 26 | #include "qemu-common.h" |
10817bf0 DB |
27 | #include "qemu/coroutine.h" |
28 | #include "qemu/coroutine_int.h" | |
1de7afc9 | 29 | #include "qemu/queue.h" |
b96e9247 KW |
30 | #include "trace.h" |
31 | ||
b96e9247 KW |
32 | void qemu_co_queue_init(CoQueue *queue) |
33 | { | |
7d9c8581 | 34 | QSIMPLEQ_INIT(&queue->entries); |
b96e9247 KW |
35 | } |
36 | ||
37 | void coroutine_fn qemu_co_queue_wait(CoQueue *queue) | |
38 | { | |
39 | Coroutine *self = qemu_coroutine_self(); | |
7d9c8581 | 40 | QSIMPLEQ_INSERT_TAIL(&queue->entries, self, co_queue_next); |
b96e9247 KW |
41 | qemu_coroutine_yield(); |
42 | assert(qemu_in_coroutine()); | |
43 | } | |
44 | ||
02ffb504 SH |
45 | /** |
46 | * qemu_co_queue_run_restart: | |
47 | * | |
48 | * Enter each coroutine that was previously marked for restart by | |
49 | * qemu_co_queue_next() or qemu_co_queue_restart_all(). This function is | |
50 | * invoked by the core coroutine code when the current coroutine yields or | |
51 | * terminates. | |
52 | */ | |
53 | void qemu_co_queue_run_restart(Coroutine *co) | |
54 | { | |
55 | Coroutine *next; | |
56 | ||
57 | trace_qemu_co_queue_run_restart(co); | |
7d9c8581 PB |
58 | while ((next = QSIMPLEQ_FIRST(&co->co_queue_wakeup))) { |
59 | QSIMPLEQ_REMOVE_HEAD(&co->co_queue_wakeup, co_queue_next); | |
0b8b8753 | 60 | qemu_coroutine_enter(next); |
02ffb504 SH |
61 | } |
62 | } | |
63 | ||
28f08246 | 64 | static bool qemu_co_queue_do_restart(CoQueue *queue, bool single) |
b96e9247 | 65 | { |
02ffb504 | 66 | Coroutine *self = qemu_coroutine_self(); |
b96e9247 | 67 | Coroutine *next; |
28f08246 | 68 | |
7d9c8581 | 69 | if (QSIMPLEQ_EMPTY(&queue->entries)) { |
28f08246 SH |
70 | return false; |
71 | } | |
b96e9247 | 72 | |
7d9c8581 PB |
73 | while ((next = QSIMPLEQ_FIRST(&queue->entries)) != NULL) { |
74 | QSIMPLEQ_REMOVE_HEAD(&queue->entries, co_queue_next); | |
75 | QSIMPLEQ_INSERT_TAIL(&self->co_queue_wakeup, next, co_queue_next); | |
b96e9247 | 76 | trace_qemu_co_queue_next(next); |
28f08246 SH |
77 | if (single) { |
78 | break; | |
79 | } | |
b96e9247 | 80 | } |
28f08246 SH |
81 | return true; |
82 | } | |
b96e9247 | 83 | |
b681a1c7 | 84 | bool coroutine_fn qemu_co_queue_next(CoQueue *queue) |
28f08246 | 85 | { |
b681a1c7 | 86 | assert(qemu_in_coroutine()); |
28f08246 | 87 | return qemu_co_queue_do_restart(queue, true); |
b96e9247 KW |
88 | } |
89 | ||
b681a1c7 | 90 | void coroutine_fn qemu_co_queue_restart_all(CoQueue *queue) |
e8ee5e4c | 91 | { |
b681a1c7 | 92 | assert(qemu_in_coroutine()); |
28f08246 | 93 | qemu_co_queue_do_restart(queue, false); |
e8ee5e4c SH |
94 | } |
95 | ||
b681a1c7 BC |
96 | bool qemu_co_enter_next(CoQueue *queue) |
97 | { | |
98 | Coroutine *next; | |
99 | ||
7d9c8581 | 100 | next = QSIMPLEQ_FIRST(&queue->entries); |
b681a1c7 BC |
101 | if (!next) { |
102 | return false; | |
103 | } | |
104 | ||
7d9c8581 | 105 | QSIMPLEQ_REMOVE_HEAD(&queue->entries, co_queue_next); |
0b8b8753 | 106 | qemu_coroutine_enter(next); |
b681a1c7 BC |
107 | return true; |
108 | } | |
109 | ||
b96e9247 KW |
110 | bool qemu_co_queue_empty(CoQueue *queue) |
111 | { | |
7d9c8581 | 112 | return QSIMPLEQ_FIRST(&queue->entries) == NULL; |
b96e9247 KW |
113 | } |
114 | ||
115 | void qemu_co_mutex_init(CoMutex *mutex) | |
116 | { | |
117 | memset(mutex, 0, sizeof(*mutex)); | |
118 | qemu_co_queue_init(&mutex->queue); | |
119 | } | |
120 | ||
121 | void coroutine_fn qemu_co_mutex_lock(CoMutex *mutex) | |
122 | { | |
123 | Coroutine *self = qemu_coroutine_self(); | |
124 | ||
125 | trace_qemu_co_mutex_lock_entry(mutex, self); | |
126 | ||
127 | while (mutex->locked) { | |
128 | qemu_co_queue_wait(&mutex->queue); | |
129 | } | |
130 | ||
131 | mutex->locked = true; | |
0e438cdc | 132 | mutex->holder = self; |
1b7f01d9 | 133 | self->locks_held++; |
b96e9247 KW |
134 | |
135 | trace_qemu_co_mutex_lock_return(mutex, self); | |
136 | } | |
137 | ||
138 | void coroutine_fn qemu_co_mutex_unlock(CoMutex *mutex) | |
139 | { | |
140 | Coroutine *self = qemu_coroutine_self(); | |
141 | ||
142 | trace_qemu_co_mutex_unlock_entry(mutex, self); | |
143 | ||
144 | assert(mutex->locked == true); | |
0e438cdc | 145 | assert(mutex->holder == self); |
b96e9247 KW |
146 | assert(qemu_in_coroutine()); |
147 | ||
148 | mutex->locked = false; | |
0e438cdc | 149 | mutex->holder = NULL; |
1b7f01d9 | 150 | self->locks_held--; |
b96e9247 KW |
151 | qemu_co_queue_next(&mutex->queue); |
152 | ||
153 | trace_qemu_co_mutex_unlock_return(mutex, self); | |
154 | } | |
12888904 AK |
155 | |
156 | void qemu_co_rwlock_init(CoRwlock *lock) | |
157 | { | |
158 | memset(lock, 0, sizeof(*lock)); | |
159 | qemu_co_queue_init(&lock->queue); | |
160 | } | |
161 | ||
162 | void qemu_co_rwlock_rdlock(CoRwlock *lock) | |
163 | { | |
1b7f01d9 KW |
164 | Coroutine *self = qemu_coroutine_self(); |
165 | ||
12888904 AK |
166 | while (lock->writer) { |
167 | qemu_co_queue_wait(&lock->queue); | |
168 | } | |
169 | lock->reader++; | |
1b7f01d9 | 170 | self->locks_held++; |
12888904 AK |
171 | } |
172 | ||
173 | void qemu_co_rwlock_unlock(CoRwlock *lock) | |
174 | { | |
1b7f01d9 KW |
175 | Coroutine *self = qemu_coroutine_self(); |
176 | ||
12888904 AK |
177 | assert(qemu_in_coroutine()); |
178 | if (lock->writer) { | |
179 | lock->writer = false; | |
e8ee5e4c | 180 | qemu_co_queue_restart_all(&lock->queue); |
12888904 AK |
181 | } else { |
182 | lock->reader--; | |
183 | assert(lock->reader >= 0); | |
184 | /* Wakeup only one waiting writer */ | |
185 | if (!lock->reader) { | |
186 | qemu_co_queue_next(&lock->queue); | |
187 | } | |
188 | } | |
1b7f01d9 | 189 | self->locks_held--; |
12888904 AK |
190 | } |
191 | ||
192 | void qemu_co_rwlock_wrlock(CoRwlock *lock) | |
193 | { | |
1b7f01d9 KW |
194 | Coroutine *self = qemu_coroutine_self(); |
195 | ||
12888904 AK |
196 | while (lock->writer || lock->reader) { |
197 | qemu_co_queue_wait(&lock->queue); | |
198 | } | |
199 | lock->writer = true; | |
1b7f01d9 | 200 | self->locks_held++; |
12888904 | 201 | } |