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
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"
32 void qemu_co_queue_init(CoQueue
*queue
)
34 QTAILQ_INIT(&queue
->entries
);
37 void coroutine_fn
qemu_co_queue_wait(CoQueue
*queue
)
39 Coroutine
*self
= qemu_coroutine_self();
40 QTAILQ_INSERT_TAIL(&queue
->entries
, self
, co_queue_next
);
41 qemu_coroutine_yield();
42 assert(qemu_in_coroutine());
46 * qemu_co_queue_run_restart:
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
53 void qemu_co_queue_run_restart(Coroutine
*co
)
57 trace_qemu_co_queue_run_restart(co
);
58 while ((next
= QTAILQ_FIRST(&co
->co_queue_wakeup
))) {
59 QTAILQ_REMOVE(&co
->co_queue_wakeup
, next
, co_queue_next
);
60 qemu_coroutine_enter(next
, NULL
);
64 static bool qemu_co_queue_do_restart(CoQueue
*queue
, bool single
)
66 Coroutine
*self
= qemu_coroutine_self();
69 if (QTAILQ_EMPTY(&queue
->entries
)) {
73 while ((next
= QTAILQ_FIRST(&queue
->entries
)) != NULL
) {
74 QTAILQ_REMOVE(&queue
->entries
, next
, co_queue_next
);
75 QTAILQ_INSERT_TAIL(&self
->co_queue_wakeup
, next
, co_queue_next
);
76 trace_qemu_co_queue_next(next
);
84 bool coroutine_fn
qemu_co_queue_next(CoQueue
*queue
)
86 assert(qemu_in_coroutine());
87 return qemu_co_queue_do_restart(queue
, true);
90 void coroutine_fn
qemu_co_queue_restart_all(CoQueue
*queue
)
92 assert(qemu_in_coroutine());
93 qemu_co_queue_do_restart(queue
, false);
96 bool qemu_co_enter_next(CoQueue
*queue
)
100 next
= QTAILQ_FIRST(&queue
->entries
);
105 QTAILQ_REMOVE(&queue
->entries
, next
, co_queue_next
);
106 qemu_coroutine_enter(next
, NULL
);
110 bool qemu_co_queue_empty(CoQueue
*queue
)
112 return QTAILQ_FIRST(&queue
->entries
) == NULL
;
115 void qemu_co_mutex_init(CoMutex
*mutex
)
117 memset(mutex
, 0, sizeof(*mutex
));
118 qemu_co_queue_init(&mutex
->queue
);
121 void coroutine_fn
qemu_co_mutex_lock(CoMutex
*mutex
)
123 Coroutine
*self
= qemu_coroutine_self();
125 trace_qemu_co_mutex_lock_entry(mutex
, self
);
127 while (mutex
->locked
) {
128 qemu_co_queue_wait(&mutex
->queue
);
131 mutex
->locked
= true;
133 trace_qemu_co_mutex_lock_return(mutex
, self
);
136 void coroutine_fn
qemu_co_mutex_unlock(CoMutex
*mutex
)
138 Coroutine
*self
= qemu_coroutine_self();
140 trace_qemu_co_mutex_unlock_entry(mutex
, self
);
142 assert(mutex
->locked
== true);
143 assert(qemu_in_coroutine());
145 mutex
->locked
= false;
146 qemu_co_queue_next(&mutex
->queue
);
148 trace_qemu_co_mutex_unlock_return(mutex
, self
);
151 void qemu_co_rwlock_init(CoRwlock
*lock
)
153 memset(lock
, 0, sizeof(*lock
));
154 qemu_co_queue_init(&lock
->queue
);
157 void qemu_co_rwlock_rdlock(CoRwlock
*lock
)
159 while (lock
->writer
) {
160 qemu_co_queue_wait(&lock
->queue
);
165 void qemu_co_rwlock_unlock(CoRwlock
*lock
)
167 assert(qemu_in_coroutine());
169 lock
->writer
= false;
170 qemu_co_queue_restart_all(&lock
->queue
);
173 assert(lock
->reader
>= 0);
174 /* Wakeup only one waiting writer */
176 qemu_co_queue_next(&lock
->queue
);
181 void qemu_co_rwlock_wrlock(CoRwlock
*lock
)
183 while (lock
->writer
|| lock
->reader
) {
184 qemu_co_queue_wait(&lock
->queue
);