]>
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 | ||
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); | |
e680cfa7 | 33 | static QEMUBH* unlock_bh; |
b96e9247 KW |
34 | |
35 | static void qemu_co_queue_next_bh(void *opaque) | |
36 | { | |
b96e9247 KW |
37 | Coroutine *next; |
38 | ||
39 | trace_qemu_co_queue_next_bh(); | |
40 | while ((next = QTAILQ_FIRST(&unlock_bh_queue))) { | |
41 | QTAILQ_REMOVE(&unlock_bh_queue, next, co_queue_next); | |
42 | qemu_coroutine_enter(next, NULL); | |
43 | } | |
b96e9247 KW |
44 | } |
45 | ||
46 | void qemu_co_queue_init(CoQueue *queue) | |
47 | { | |
48 | QTAILQ_INIT(&queue->entries); | |
e680cfa7 KW |
49 | |
50 | if (!unlock_bh) { | |
51 | unlock_bh = qemu_bh_new(qemu_co_queue_next_bh, NULL); | |
52 | } | |
b96e9247 KW |
53 | } |
54 | ||
55 | void coroutine_fn qemu_co_queue_wait(CoQueue *queue) | |
56 | { | |
57 | Coroutine *self = qemu_coroutine_self(); | |
58 | QTAILQ_INSERT_TAIL(&queue->entries, self, co_queue_next); | |
59 | qemu_coroutine_yield(); | |
60 | assert(qemu_in_coroutine()); | |
61 | } | |
62 | ||
63 | bool qemu_co_queue_next(CoQueue *queue) | |
64 | { | |
b96e9247 KW |
65 | Coroutine *next; |
66 | ||
67 | next = QTAILQ_FIRST(&queue->entries); | |
68 | if (next) { | |
69 | QTAILQ_REMOVE(&queue->entries, next, co_queue_next); | |
70 | QTAILQ_INSERT_TAIL(&unlock_bh_queue, next, co_queue_next); | |
71 | trace_qemu_co_queue_next(next); | |
e680cfa7 | 72 | qemu_bh_schedule(unlock_bh); |
b96e9247 KW |
73 | } |
74 | ||
75 | return (next != NULL); | |
76 | } | |
77 | ||
78 | bool qemu_co_queue_empty(CoQueue *queue) | |
79 | { | |
80 | return (QTAILQ_FIRST(&queue->entries) == NULL); | |
81 | } | |
82 | ||
83 | void qemu_co_mutex_init(CoMutex *mutex) | |
84 | { | |
85 | memset(mutex, 0, sizeof(*mutex)); | |
86 | qemu_co_queue_init(&mutex->queue); | |
87 | } | |
88 | ||
89 | void coroutine_fn qemu_co_mutex_lock(CoMutex *mutex) | |
90 | { | |
91 | Coroutine *self = qemu_coroutine_self(); | |
92 | ||
93 | trace_qemu_co_mutex_lock_entry(mutex, self); | |
94 | ||
95 | while (mutex->locked) { | |
96 | qemu_co_queue_wait(&mutex->queue); | |
97 | } | |
98 | ||
99 | mutex->locked = true; | |
100 | ||
101 | trace_qemu_co_mutex_lock_return(mutex, self); | |
102 | } | |
103 | ||
104 | void coroutine_fn qemu_co_mutex_unlock(CoMutex *mutex) | |
105 | { | |
106 | Coroutine *self = qemu_coroutine_self(); | |
107 | ||
108 | trace_qemu_co_mutex_unlock_entry(mutex, self); | |
109 | ||
110 | assert(mutex->locked == true); | |
111 | assert(qemu_in_coroutine()); | |
112 | ||
113 | mutex->locked = false; | |
114 | qemu_co_queue_next(&mutex->queue); | |
115 | ||
116 | trace_qemu_co_mutex_unlock_return(mutex, self); | |
117 | } | |
12888904 AK |
118 | |
119 | void qemu_co_rwlock_init(CoRwlock *lock) | |
120 | { | |
121 | memset(lock, 0, sizeof(*lock)); | |
122 | qemu_co_queue_init(&lock->queue); | |
123 | } | |
124 | ||
125 | void qemu_co_rwlock_rdlock(CoRwlock *lock) | |
126 | { | |
127 | while (lock->writer) { | |
128 | qemu_co_queue_wait(&lock->queue); | |
129 | } | |
130 | lock->reader++; | |
131 | } | |
132 | ||
133 | void qemu_co_rwlock_unlock(CoRwlock *lock) | |
134 | { | |
135 | assert(qemu_in_coroutine()); | |
136 | if (lock->writer) { | |
137 | lock->writer = false; | |
138 | while (!qemu_co_queue_empty(&lock->queue)) { | |
139 | /* | |
140 | * Wakeup every body. This will include some | |
141 | * writers too. | |
142 | */ | |
143 | qemu_co_queue_next(&lock->queue); | |
144 | } | |
145 | } else { | |
146 | lock->reader--; | |
147 | assert(lock->reader >= 0); | |
148 | /* Wakeup only one waiting writer */ | |
149 | if (!lock->reader) { | |
150 | qemu_co_queue_next(&lock->queue); | |
151 | } | |
152 | } | |
153 | } | |
154 | ||
155 | void qemu_co_rwlock_wrlock(CoRwlock *lock) | |
156 | { | |
157 | while (lock->writer || lock->reader) { | |
158 | qemu_co_queue_wait(&lock->queue); | |
159 | } | |
160 | lock->writer = true; | |
161 | } |