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-common.h"
26 #include "qemu/coroutine.h"
27 #include "qemu/coroutine_int.h"
28 #include "qemu/queue.h"
31 void qemu_co_queue_init(CoQueue
*queue
)
33 QTAILQ_INIT(&queue
->entries
);
36 void coroutine_fn
qemu_co_queue_wait(CoQueue
*queue
)
38 Coroutine
*self
= qemu_coroutine_self();
39 QTAILQ_INSERT_TAIL(&queue
->entries
, self
, co_queue_next
);
40 qemu_coroutine_yield();
41 assert(qemu_in_coroutine());
45 * qemu_co_queue_run_restart:
47 * Enter each coroutine that was previously marked for restart by
48 * qemu_co_queue_next() or qemu_co_queue_restart_all(). This function is
49 * invoked by the core coroutine code when the current coroutine yields or
52 void qemu_co_queue_run_restart(Coroutine
*co
)
56 trace_qemu_co_queue_run_restart(co
);
57 while ((next
= QTAILQ_FIRST(&co
->co_queue_wakeup
))) {
58 QTAILQ_REMOVE(&co
->co_queue_wakeup
, next
, co_queue_next
);
59 qemu_coroutine_enter(next
, NULL
);
63 static bool qemu_co_queue_do_restart(CoQueue
*queue
, bool single
)
65 Coroutine
*self
= qemu_coroutine_self();
68 if (QTAILQ_EMPTY(&queue
->entries
)) {
72 while ((next
= QTAILQ_FIRST(&queue
->entries
)) != NULL
) {
73 QTAILQ_REMOVE(&queue
->entries
, next
, co_queue_next
);
74 QTAILQ_INSERT_TAIL(&self
->co_queue_wakeup
, next
, co_queue_next
);
75 trace_qemu_co_queue_next(next
);
83 bool coroutine_fn
qemu_co_queue_next(CoQueue
*queue
)
85 assert(qemu_in_coroutine());
86 return qemu_co_queue_do_restart(queue
, true);
89 void coroutine_fn
qemu_co_queue_restart_all(CoQueue
*queue
)
91 assert(qemu_in_coroutine());
92 qemu_co_queue_do_restart(queue
, false);
95 bool qemu_co_enter_next(CoQueue
*queue
)
99 next
= QTAILQ_FIRST(&queue
->entries
);
104 QTAILQ_REMOVE(&queue
->entries
, next
, co_queue_next
);
105 qemu_coroutine_enter(next
, NULL
);
109 bool qemu_co_queue_empty(CoQueue
*queue
)
111 return QTAILQ_FIRST(&queue
->entries
) == NULL
;
114 void qemu_co_mutex_init(CoMutex
*mutex
)
116 memset(mutex
, 0, sizeof(*mutex
));
117 qemu_co_queue_init(&mutex
->queue
);
120 void coroutine_fn
qemu_co_mutex_lock(CoMutex
*mutex
)
122 Coroutine
*self
= qemu_coroutine_self();
124 trace_qemu_co_mutex_lock_entry(mutex
, self
);
126 while (mutex
->locked
) {
127 qemu_co_queue_wait(&mutex
->queue
);
130 mutex
->locked
= true;
132 trace_qemu_co_mutex_lock_return(mutex
, self
);
135 void coroutine_fn
qemu_co_mutex_unlock(CoMutex
*mutex
)
137 Coroutine
*self
= qemu_coroutine_self();
139 trace_qemu_co_mutex_unlock_entry(mutex
, self
);
141 assert(mutex
->locked
== true);
142 assert(qemu_in_coroutine());
144 mutex
->locked
= false;
145 qemu_co_queue_next(&mutex
->queue
);
147 trace_qemu_co_mutex_unlock_return(mutex
, self
);
150 void qemu_co_rwlock_init(CoRwlock
*lock
)
152 memset(lock
, 0, sizeof(*lock
));
153 qemu_co_queue_init(&lock
->queue
);
156 void qemu_co_rwlock_rdlock(CoRwlock
*lock
)
158 while (lock
->writer
) {
159 qemu_co_queue_wait(&lock
->queue
);
164 void qemu_co_rwlock_unlock(CoRwlock
*lock
)
166 assert(qemu_in_coroutine());
168 lock
->writer
= false;
169 qemu_co_queue_restart_all(&lock
->queue
);
172 assert(lock
->reader
>= 0);
173 /* Wakeup only one waiting writer */
175 qemu_co_queue_next(&lock
->queue
);
180 void qemu_co_rwlock_wrlock(CoRwlock
*lock
)
182 while (lock
->writer
|| lock
->reader
) {
183 qemu_co_queue_wait(&lock
->queue
);