Revision 9257d46d
b/Makefile.objs | ||
---|---|---|
142 | 142 |
common-obj-y += $(addprefix ui/, $(ui-obj-y)) |
143 | 143 |
|
144 | 144 |
common-obj-y += iov.o acl.o |
145 |
common-obj-$(CONFIG_THREAD) += qemu-thread.o
|
|
146 |
common-obj-$(CONFIG_POSIX) += compatfd.o
|
|
145 |
common-obj-$(CONFIG_POSIX) += qemu-thread-posix.o compatfd.o
|
|
146 |
common-obj-$(CONFIG_WIN32) += qemu-thread-win32.o
|
|
147 | 147 |
common-obj-y += notify.o event_notifier.o |
148 | 148 |
common-obj-y += qemu-timer.o qemu-timer-common.o |
149 | 149 |
|
b/qemu-thread-posix.c | ||
---|---|---|
1 |
/* |
|
2 |
* Wrappers around mutex/cond/thread functions |
|
3 |
* |
|
4 |
* Copyright Red Hat, Inc. 2009 |
|
5 |
* |
|
6 |
* Author: |
|
7 |
* Marcelo Tosatti <mtosatti@redhat.com> |
|
8 |
* |
|
9 |
* This work is licensed under the terms of the GNU GPL, version 2 or later. |
|
10 |
* See the COPYING file in the top-level directory. |
|
11 |
* |
|
12 |
*/ |
|
13 |
#include <stdlib.h> |
|
14 |
#include <stdio.h> |
|
15 |
#include <errno.h> |
|
16 |
#include <time.h> |
|
17 |
#include <signal.h> |
|
18 |
#include <stdint.h> |
|
19 |
#include <string.h> |
|
20 |
#include "qemu-thread.h" |
|
21 |
|
|
22 |
static void error_exit(int err, const char *msg) |
|
23 |
{ |
|
24 |
fprintf(stderr, "qemu: %s: %s\n", msg, strerror(err)); |
|
25 |
exit(1); |
|
26 |
} |
|
27 |
|
|
28 |
void qemu_mutex_init(QemuMutex *mutex) |
|
29 |
{ |
|
30 |
int err; |
|
31 |
|
|
32 |
err = pthread_mutex_init(&mutex->lock, NULL); |
|
33 |
if (err) |
|
34 |
error_exit(err, __func__); |
|
35 |
} |
|
36 |
|
|
37 |
void qemu_mutex_destroy(QemuMutex *mutex) |
|
38 |
{ |
|
39 |
int err; |
|
40 |
|
|
41 |
err = pthread_mutex_destroy(&mutex->lock); |
|
42 |
if (err) |
|
43 |
error_exit(err, __func__); |
|
44 |
} |
|
45 |
|
|
46 |
void qemu_mutex_lock(QemuMutex *mutex) |
|
47 |
{ |
|
48 |
int err; |
|
49 |
|
|
50 |
err = pthread_mutex_lock(&mutex->lock); |
|
51 |
if (err) |
|
52 |
error_exit(err, __func__); |
|
53 |
} |
|
54 |
|
|
55 |
int qemu_mutex_trylock(QemuMutex *mutex) |
|
56 |
{ |
|
57 |
return pthread_mutex_trylock(&mutex->lock); |
|
58 |
} |
|
59 |
|
|
60 |
static void timespec_add_ms(struct timespec *ts, uint64_t msecs) |
|
61 |
{ |
|
62 |
ts->tv_sec = ts->tv_sec + (long)(msecs / 1000); |
|
63 |
ts->tv_nsec = (ts->tv_nsec + ((long)msecs % 1000) * 1000000); |
|
64 |
if (ts->tv_nsec >= 1000000000) { |
|
65 |
ts->tv_nsec -= 1000000000; |
|
66 |
ts->tv_sec++; |
|
67 |
} |
|
68 |
} |
|
69 |
|
|
70 |
int qemu_mutex_timedlock(QemuMutex *mutex, uint64_t msecs) |
|
71 |
{ |
|
72 |
int err; |
|
73 |
struct timespec ts; |
|
74 |
|
|
75 |
clock_gettime(CLOCK_REALTIME, &ts); |
|
76 |
timespec_add_ms(&ts, msecs); |
|
77 |
|
|
78 |
err = pthread_mutex_timedlock(&mutex->lock, &ts); |
|
79 |
if (err && err != ETIMEDOUT) |
|
80 |
error_exit(err, __func__); |
|
81 |
return err; |
|
82 |
} |
|
83 |
|
|
84 |
void qemu_mutex_unlock(QemuMutex *mutex) |
|
85 |
{ |
|
86 |
int err; |
|
87 |
|
|
88 |
err = pthread_mutex_unlock(&mutex->lock); |
|
89 |
if (err) |
|
90 |
error_exit(err, __func__); |
|
91 |
} |
|
92 |
|
|
93 |
void qemu_cond_init(QemuCond *cond) |
|
94 |
{ |
|
95 |
int err; |
|
96 |
|
|
97 |
err = pthread_cond_init(&cond->cond, NULL); |
|
98 |
if (err) |
|
99 |
error_exit(err, __func__); |
|
100 |
} |
|
101 |
|
|
102 |
void qemu_cond_destroy(QemuCond *cond) |
|
103 |
{ |
|
104 |
int err; |
|
105 |
|
|
106 |
err = pthread_cond_destroy(&cond->cond); |
|
107 |
if (err) |
|
108 |
error_exit(err, __func__); |
|
109 |
} |
|
110 |
|
|
111 |
void qemu_cond_signal(QemuCond *cond) |
|
112 |
{ |
|
113 |
int err; |
|
114 |
|
|
115 |
err = pthread_cond_signal(&cond->cond); |
|
116 |
if (err) |
|
117 |
error_exit(err, __func__); |
|
118 |
} |
|
119 |
|
|
120 |
void qemu_cond_broadcast(QemuCond *cond) |
|
121 |
{ |
|
122 |
int err; |
|
123 |
|
|
124 |
err = pthread_cond_broadcast(&cond->cond); |
|
125 |
if (err) |
|
126 |
error_exit(err, __func__); |
|
127 |
} |
|
128 |
|
|
129 |
void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex) |
|
130 |
{ |
|
131 |
int err; |
|
132 |
|
|
133 |
err = pthread_cond_wait(&cond->cond, &mutex->lock); |
|
134 |
if (err) |
|
135 |
error_exit(err, __func__); |
|
136 |
} |
|
137 |
|
|
138 |
int qemu_cond_timedwait(QemuCond *cond, QemuMutex *mutex, uint64_t msecs) |
|
139 |
{ |
|
140 |
struct timespec ts; |
|
141 |
int err; |
|
142 |
|
|
143 |
clock_gettime(CLOCK_REALTIME, &ts); |
|
144 |
timespec_add_ms(&ts, msecs); |
|
145 |
|
|
146 |
err = pthread_cond_timedwait(&cond->cond, &mutex->lock, &ts); |
|
147 |
if (err && err != ETIMEDOUT) |
|
148 |
error_exit(err, __func__); |
|
149 |
return err; |
|
150 |
} |
|
151 |
|
|
152 |
void qemu_thread_create(QemuThread *thread, |
|
153 |
void *(*start_routine)(void*), |
|
154 |
void *arg) |
|
155 |
{ |
|
156 |
int err; |
|
157 |
|
|
158 |
/* Leave signal handling to the iothread. */ |
|
159 |
sigset_t set, oldset; |
|
160 |
|
|
161 |
sigfillset(&set); |
|
162 |
pthread_sigmask(SIG_SETMASK, &set, &oldset); |
|
163 |
err = pthread_create(&thread->thread, NULL, start_routine, arg); |
|
164 |
if (err) |
|
165 |
error_exit(err, __func__); |
|
166 |
|
|
167 |
pthread_sigmask(SIG_SETMASK, &oldset, NULL); |
|
168 |
} |
|
169 |
|
|
170 |
void qemu_thread_signal(QemuThread *thread, int sig) |
|
171 |
{ |
|
172 |
int err; |
|
173 |
|
|
174 |
err = pthread_kill(thread->thread, sig); |
|
175 |
if (err) |
|
176 |
error_exit(err, __func__); |
|
177 |
} |
|
178 |
|
|
179 |
void qemu_thread_get_self(QemuThread *thread) |
|
180 |
{ |
|
181 |
thread->thread = pthread_self(); |
|
182 |
} |
|
183 |
|
|
184 |
int qemu_thread_is_self(QemuThread *thread) |
|
185 |
{ |
|
186 |
return pthread_equal(pthread_self(), thread->thread); |
|
187 |
} |
|
188 |
|
|
189 |
void qemu_thread_exit(void *retval) |
|
190 |
{ |
|
191 |
pthread_exit(retval); |
|
192 |
} |
b/qemu-thread-posix.h | ||
---|---|---|
1 |
#ifndef __QEMU_THREAD_POSIX_H |
|
2 |
#define __QEMU_THREAD_POSIX_H 1 |
|
3 |
#include "pthread.h" |
|
4 |
|
|
5 |
struct QemuMutex { |
|
6 |
pthread_mutex_t lock; |
|
7 |
}; |
|
8 |
|
|
9 |
struct QemuCond { |
|
10 |
pthread_cond_t cond; |
|
11 |
}; |
|
12 |
|
|
13 |
struct QemuThread { |
|
14 |
pthread_t thread; |
|
15 |
}; |
|
16 |
|
|
17 |
void qemu_thread_signal(QemuThread *thread, int sig); |
|
18 |
#endif |
b/qemu-thread-win32.c | ||
---|---|---|
1 |
/* |
|
2 |
* Win32 implementation for mutex/cond/thread functions |
|
3 |
* |
|
4 |
* Copyright Red Hat, Inc. 2010 |
|
5 |
* |
|
6 |
* Author: |
|
7 |
* Paolo Bonzini <pbonzini@redhat.com> |
|
8 |
* |
|
9 |
* This work is licensed under the terms of the GNU GPL, version 2 or later. |
|
10 |
* See the COPYING file in the top-level directory. |
|
11 |
* |
|
12 |
*/ |
|
13 |
#include "qemu-common.h" |
|
14 |
#include "qemu-thread.h" |
|
15 |
#include <process.h> |
|
16 |
#include <assert.h> |
|
17 |
#include <limits.h> |
|
18 |
|
|
19 |
static void error_exit(int err, const char *msg) |
|
20 |
{ |
|
21 |
char *pstr; |
|
22 |
|
|
23 |
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, |
|
24 |
NULL, err, 0, (LPTSTR)&pstr, 2, NULL); |
|
25 |
fprintf(stderr, "qemu: %s: %s\n", msg, pstr); |
|
26 |
LocalFree(pstr); |
|
27 |
exit(1); |
|
28 |
} |
|
29 |
|
|
30 |
void qemu_mutex_init(QemuMutex *mutex) |
|
31 |
{ |
|
32 |
mutex->owner = 0; |
|
33 |
InitializeCriticalSection(&mutex->lock); |
|
34 |
} |
|
35 |
|
|
36 |
void qemu_mutex_lock(QemuMutex *mutex) |
|
37 |
{ |
|
38 |
EnterCriticalSection(&mutex->lock); |
|
39 |
|
|
40 |
/* Win32 CRITICAL_SECTIONs are recursive. Assert that we're not |
|
41 |
* using them as such. |
|
42 |
*/ |
|
43 |
assert(mutex->owner == 0); |
|
44 |
mutex->owner = GetCurrentThreadId(); |
|
45 |
} |
|
46 |
|
|
47 |
int qemu_mutex_trylock(QemuMutex *mutex) |
|
48 |
{ |
|
49 |
int owned; |
|
50 |
|
|
51 |
owned = TryEnterCriticalSection(&mutex->lock); |
|
52 |
if (owned) { |
|
53 |
assert(mutex->owner == 0); |
|
54 |
mutex->owner = GetCurrentThreadId(); |
|
55 |
} |
|
56 |
return !owned; |
|
57 |
} |
|
58 |
|
|
59 |
void qemu_mutex_unlock(QemuMutex *mutex) |
|
60 |
{ |
|
61 |
assert(mutex->owner == GetCurrentThreadId()); |
|
62 |
mutex->owner = 0; |
|
63 |
LeaveCriticalSection(&mutex->lock); |
|
64 |
} |
|
65 |
|
|
66 |
void qemu_cond_init(QemuCond *cond) |
|
67 |
{ |
|
68 |
memset(cond, 0, sizeof(*cond)); |
|
69 |
|
|
70 |
cond->sema = CreateSemaphore(NULL, 0, LONG_MAX, NULL); |
|
71 |
if (!cond->sema) { |
|
72 |
error_exit(GetLastError(), __func__); |
|
73 |
} |
|
74 |
cond->continue_event = CreateEvent(NULL, /* security */ |
|
75 |
FALSE, /* auto-reset */ |
|
76 |
FALSE, /* not signaled */ |
|
77 |
NULL); /* name */ |
|
78 |
if (!cond->continue_event) { |
|
79 |
error_exit(GetLastError(), __func__); |
|
80 |
} |
|
81 |
} |
|
82 |
|
|
83 |
void qemu_cond_signal(QemuCond *cond) |
|
84 |
{ |
|
85 |
DWORD result; |
|
86 |
|
|
87 |
/* |
|
88 |
* Signal only when there are waiters. cond->waiters is |
|
89 |
* incremented by pthread_cond_wait under the external lock, |
|
90 |
* so we are safe about that. |
|
91 |
*/ |
|
92 |
if (cond->waiters == 0) { |
|
93 |
return; |
|
94 |
} |
|
95 |
|
|
96 |
/* |
|
97 |
* Waiting threads decrement it outside the external lock, but |
|
98 |
* only if another thread is executing pthread_cond_broadcast and |
|
99 |
* has the mutex. So, it also cannot be decremented concurrently |
|
100 |
* with this particular access. |
|
101 |
*/ |
|
102 |
cond->target = cond->waiters - 1; |
|
103 |
result = SignalObjectAndWait(cond->sema, cond->continue_event, |
|
104 |
INFINITE, FALSE); |
|
105 |
if (result == WAIT_ABANDONED || result == WAIT_FAILED) { |
|
106 |
error_exit(GetLastError(), __func__); |
|
107 |
} |
|
108 |
} |
|
109 |
|
|
110 |
void qemu_cond_broadcast(QemuCond *cond) |
|
111 |
{ |
|
112 |
BOOLEAN result; |
|
113 |
/* |
|
114 |
* As in pthread_cond_signal, access to cond->waiters and |
|
115 |
* cond->target is locked via the external mutex. |
|
116 |
*/ |
|
117 |
if (cond->waiters == 0) { |
|
118 |
return; |
|
119 |
} |
|
120 |
|
|
121 |
cond->target = 0; |
|
122 |
result = ReleaseSemaphore(cond->sema, cond->waiters, NULL); |
|
123 |
if (!result) { |
|
124 |
error_exit(GetLastError(), __func__); |
|
125 |
} |
|
126 |
|
|
127 |
/* |
|
128 |
* At this point all waiters continue. Each one takes its |
|
129 |
* slice of the semaphore. Now it's our turn to wait: Since |
|
130 |
* the external mutex is held, no thread can leave cond_wait, |
|
131 |
* yet. For this reason, we can be sure that no thread gets |
|
132 |
* a chance to eat *more* than one slice. OTOH, it means |
|
133 |
* that the last waiter must send us a wake-up. |
|
134 |
*/ |
|
135 |
WaitForSingleObject(cond->continue_event, INFINITE); |
|
136 |
} |
|
137 |
|
|
138 |
void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex) |
|
139 |
{ |
|
140 |
/* |
|
141 |
* This access is protected under the mutex. |
|
142 |
*/ |
|
143 |
cond->waiters++; |
|
144 |
|
|
145 |
/* |
|
146 |
* Unlock external mutex and wait for signal. |
|
147 |
* NOTE: we've held mutex locked long enough to increment |
|
148 |
* waiters count above, so there's no problem with |
|
149 |
* leaving mutex unlocked before we wait on semaphore. |
|
150 |
*/ |
|
151 |
qemu_mutex_unlock(mutex); |
|
152 |
WaitForSingleObject(cond->sema, INFINITE); |
|
153 |
|
|
154 |
/* Now waiters must rendez-vous with the signaling thread and |
|
155 |
* let it continue. For cond_broadcast this has heavy contention |
|
156 |
* and triggers thundering herd. So goes life. |
|
157 |
* |
|
158 |
* Decrease waiters count. The mutex is not taken, so we have |
|
159 |
* to do this atomically. |
|
160 |
* |
|
161 |
* All waiters contend for the mutex at the end of this function |
|
162 |
* until the signaling thread relinquishes it. To ensure |
|
163 |
* each waiter consumes exactly one slice of the semaphore, |
|
164 |
* the signaling thread stops until it is told by the last |
|
165 |
* waiter that it can go on. |
|
166 |
*/ |
|
167 |
if (InterlockedDecrement(&cond->waiters) == cond->target) { |
|
168 |
SetEvent(cond->continue_event); |
|
169 |
} |
|
170 |
|
|
171 |
qemu_mutex_lock(mutex); |
|
172 |
} |
|
173 |
|
|
174 |
struct QemuThreadData { |
|
175 |
QemuThread *thread; |
|
176 |
void *(*start_routine)(void *); |
|
177 |
void *arg; |
|
178 |
}; |
|
179 |
|
|
180 |
static int qemu_thread_tls_index = TLS_OUT_OF_INDEXES; |
|
181 |
|
|
182 |
static unsigned __stdcall win32_start_routine(void *arg) |
|
183 |
{ |
|
184 |
struct QemuThreadData data = *(struct QemuThreadData *) arg; |
|
185 |
QemuThread *thread = data.thread; |
|
186 |
|
|
187 |
free(arg); |
|
188 |
TlsSetValue(qemu_thread_tls_index, thread); |
|
189 |
|
|
190 |
/* |
|
191 |
* Use DuplicateHandle instead of assigning thread->thread in the |
|
192 |
* creating thread to avoid races. It's simpler this way than with |
|
193 |
* synchronization. |
|
194 |
*/ |
|
195 |
DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), |
|
196 |
GetCurrentProcess(), &thread->thread, |
|
197 |
0, FALSE, DUPLICATE_SAME_ACCESS); |
|
198 |
|
|
199 |
qemu_thread_exit(data.start_routine(data.arg)); |
|
200 |
abort(); |
|
201 |
} |
|
202 |
|
|
203 |
void qemu_thread_exit(void *arg) |
|
204 |
{ |
|
205 |
QemuThread *thread = TlsGetValue(qemu_thread_tls_index); |
|
206 |
thread->ret = arg; |
|
207 |
CloseHandle(thread->thread); |
|
208 |
thread->thread = NULL; |
|
209 |
ExitThread(0); |
|
210 |
} |
|
211 |
|
|
212 |
static inline void qemu_thread_init(void) |
|
213 |
{ |
|
214 |
if (qemu_thread_tls_index == TLS_OUT_OF_INDEXES) { |
|
215 |
qemu_thread_tls_index = TlsAlloc(); |
|
216 |
if (qemu_thread_tls_index == TLS_OUT_OF_INDEXES) { |
|
217 |
error_exit(ERROR_NO_SYSTEM_RESOURCES, __func__); |
|
218 |
} |
|
219 |
} |
|
220 |
} |
|
221 |
|
|
222 |
|
|
223 |
void qemu_thread_create(QemuThread *thread, |
|
224 |
void *(*start_routine)(void *), |
|
225 |
void *arg) |
|
226 |
{ |
|
227 |
HANDLE hThread; |
|
228 |
|
|
229 |
struct QemuThreadData *data; |
|
230 |
qemu_thread_init(); |
|
231 |
data = qemu_malloc(sizeof *data); |
|
232 |
data->thread = thread; |
|
233 |
data->start_routine = start_routine; |
|
234 |
data->arg = arg; |
|
235 |
|
|
236 |
hThread = (HANDLE) _beginthreadex(NULL, 0, win32_start_routine, |
|
237 |
data, 0, NULL); |
|
238 |
if (!hThread) { |
|
239 |
error_exit(GetLastError(), __func__); |
|
240 |
} |
|
241 |
CloseHandle(hThread); |
|
242 |
} |
|
243 |
|
|
244 |
void qemu_thread_get_self(QemuThread *thread) |
|
245 |
{ |
|
246 |
if (!thread->thread) { |
|
247 |
/* In the main thread of the process. Initialize the QemuThread |
|
248 |
pointer in TLS, and use the dummy GetCurrentThread handle as |
|
249 |
the identifier for qemu_thread_is_self. */ |
|
250 |
qemu_thread_init(); |
|
251 |
TlsSetValue(qemu_thread_tls_index, thread); |
|
252 |
thread->thread = GetCurrentThread(); |
|
253 |
} |
|
254 |
} |
|
255 |
|
|
256 |
int qemu_thread_is_self(QemuThread *thread) |
|
257 |
{ |
|
258 |
QemuThread *this_thread = TlsGetValue(qemu_thread_tls_index); |
|
259 |
return this_thread->thread == thread->thread; |
|
260 |
} |
b/qemu-thread-win32.h | ||
---|---|---|
1 |
#ifndef __QEMU_THREAD_WIN32_H |
|
2 |
#define __QEMU_THREAD_WIN32_H 1 |
|
3 |
#include "windows.h" |
|
4 |
|
|
5 |
struct QemuMutex { |
|
6 |
CRITICAL_SECTION lock; |
|
7 |
LONG owner; |
|
8 |
}; |
|
9 |
|
|
10 |
struct QemuCond { |
|
11 |
LONG waiters, target; |
|
12 |
HANDLE sema; |
|
13 |
HANDLE continue_event; |
|
14 |
}; |
|
15 |
|
|
16 |
struct QemuThread { |
|
17 |
HANDLE thread; |
|
18 |
void *ret; |
|
19 |
}; |
|
20 |
|
|
21 |
#endif |
/dev/null | ||
---|---|---|
1 |
/* |
|
2 |
* Wrappers around mutex/cond/thread functions |
|
3 |
* |
|
4 |
* Copyright Red Hat, Inc. 2009 |
|
5 |
* |
|
6 |
* Author: |
|
7 |
* Marcelo Tosatti <mtosatti@redhat.com> |
|
8 |
* |
|
9 |
* This work is licensed under the terms of the GNU GPL, version 2 or later. |
|
10 |
* See the COPYING file in the top-level directory. |
|
11 |
* |
|
12 |
*/ |
|
13 |
#include <stdlib.h> |
|
14 |
#include <stdio.h> |
|
15 |
#include <errno.h> |
|
16 |
#include <time.h> |
|
17 |
#include <signal.h> |
|
18 |
#include <stdint.h> |
|
19 |
#include <string.h> |
|
20 |
#include "qemu-thread.h" |
|
21 |
|
|
22 |
static void error_exit(int err, const char *msg) |
|
23 |
{ |
|
24 |
fprintf(stderr, "qemu: %s: %s\n", msg, strerror(err)); |
|
25 |
exit(1); |
|
26 |
} |
|
27 |
|
|
28 |
void qemu_mutex_init(QemuMutex *mutex) |
|
29 |
{ |
|
30 |
int err; |
|
31 |
|
|
32 |
err = pthread_mutex_init(&mutex->lock, NULL); |
|
33 |
if (err) |
|
34 |
error_exit(err, __func__); |
|
35 |
} |
|
36 |
|
|
37 |
void qemu_mutex_destroy(QemuMutex *mutex) |
|
38 |
{ |
|
39 |
int err; |
|
40 |
|
|
41 |
err = pthread_mutex_destroy(&mutex->lock); |
|
42 |
if (err) |
|
43 |
error_exit(err, __func__); |
|
44 |
} |
|
45 |
|
|
46 |
void qemu_mutex_lock(QemuMutex *mutex) |
|
47 |
{ |
|
48 |
int err; |
|
49 |
|
|
50 |
err = pthread_mutex_lock(&mutex->lock); |
|
51 |
if (err) |
|
52 |
error_exit(err, __func__); |
|
53 |
} |
|
54 |
|
|
55 |
int qemu_mutex_trylock(QemuMutex *mutex) |
|
56 |
{ |
|
57 |
return pthread_mutex_trylock(&mutex->lock); |
|
58 |
} |
|
59 |
|
|
60 |
static void timespec_add_ms(struct timespec *ts, uint64_t msecs) |
|
61 |
{ |
|
62 |
ts->tv_sec = ts->tv_sec + (long)(msecs / 1000); |
|
63 |
ts->tv_nsec = (ts->tv_nsec + ((long)msecs % 1000) * 1000000); |
|
64 |
if (ts->tv_nsec >= 1000000000) { |
|
65 |
ts->tv_nsec -= 1000000000; |
|
66 |
ts->tv_sec++; |
|
67 |
} |
|
68 |
} |
|
69 |
|
|
70 |
int qemu_mutex_timedlock(QemuMutex *mutex, uint64_t msecs) |
|
71 |
{ |
|
72 |
int err; |
|
73 |
struct timespec ts; |
|
74 |
|
|
75 |
clock_gettime(CLOCK_REALTIME, &ts); |
|
76 |
timespec_add_ms(&ts, msecs); |
|
77 |
|
|
78 |
err = pthread_mutex_timedlock(&mutex->lock, &ts); |
|
79 |
if (err && err != ETIMEDOUT) |
|
80 |
error_exit(err, __func__); |
|
81 |
return err; |
|
82 |
} |
|
83 |
|
|
84 |
void qemu_mutex_unlock(QemuMutex *mutex) |
|
85 |
{ |
|
86 |
int err; |
|
87 |
|
|
88 |
err = pthread_mutex_unlock(&mutex->lock); |
|
89 |
if (err) |
|
90 |
error_exit(err, __func__); |
|
91 |
} |
|
92 |
|
|
93 |
void qemu_cond_init(QemuCond *cond) |
|
94 |
{ |
|
95 |
int err; |
|
96 |
|
|
97 |
err = pthread_cond_init(&cond->cond, NULL); |
|
98 |
if (err) |
|
99 |
error_exit(err, __func__); |
|
100 |
} |
|
101 |
|
|
102 |
void qemu_cond_destroy(QemuCond *cond) |
|
103 |
{ |
|
104 |
int err; |
|
105 |
|
|
106 |
err = pthread_cond_destroy(&cond->cond); |
|
107 |
if (err) |
|
108 |
error_exit(err, __func__); |
|
109 |
} |
|
110 |
|
|
111 |
void qemu_cond_signal(QemuCond *cond) |
|
112 |
{ |
|
113 |
int err; |
|
114 |
|
|
115 |
err = pthread_cond_signal(&cond->cond); |
|
116 |
if (err) |
|
117 |
error_exit(err, __func__); |
|
118 |
} |
|
119 |
|
|
120 |
void qemu_cond_broadcast(QemuCond *cond) |
|
121 |
{ |
|
122 |
int err; |
|
123 |
|
|
124 |
err = pthread_cond_broadcast(&cond->cond); |
|
125 |
if (err) |
|
126 |
error_exit(err, __func__); |
|
127 |
} |
|
128 |
|
|
129 |
void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex) |
|
130 |
{ |
|
131 |
int err; |
|
132 |
|
|
133 |
err = pthread_cond_wait(&cond->cond, &mutex->lock); |
|
134 |
if (err) |
|
135 |
error_exit(err, __func__); |
|
136 |
} |
|
137 |
|
|
138 |
int qemu_cond_timedwait(QemuCond *cond, QemuMutex *mutex, uint64_t msecs) |
|
139 |
{ |
|
140 |
struct timespec ts; |
|
141 |
int err; |
|
142 |
|
|
143 |
clock_gettime(CLOCK_REALTIME, &ts); |
|
144 |
timespec_add_ms(&ts, msecs); |
|
145 |
|
|
146 |
err = pthread_cond_timedwait(&cond->cond, &mutex->lock, &ts); |
|
147 |
if (err && err != ETIMEDOUT) |
|
148 |
error_exit(err, __func__); |
|
149 |
return err; |
|
150 |
} |
|
151 |
|
|
152 |
void qemu_thread_create(QemuThread *thread, |
|
153 |
void *(*start_routine)(void*), |
|
154 |
void *arg) |
|
155 |
{ |
|
156 |
int err; |
|
157 |
|
|
158 |
/* Leave signal handling to the iothread. */ |
|
159 |
sigset_t set, oldset; |
|
160 |
|
|
161 |
sigfillset(&set); |
|
162 |
pthread_sigmask(SIG_SETMASK, &set, &oldset); |
|
163 |
err = pthread_create(&thread->thread, NULL, start_routine, arg); |
|
164 |
if (err) |
|
165 |
error_exit(err, __func__); |
|
166 |
|
|
167 |
pthread_sigmask(SIG_SETMASK, &oldset, NULL); |
|
168 |
} |
|
169 |
|
|
170 |
void qemu_thread_signal(QemuThread *thread, int sig) |
|
171 |
{ |
|
172 |
int err; |
|
173 |
|
|
174 |
err = pthread_kill(thread->thread, sig); |
|
175 |
if (err) |
|
176 |
error_exit(err, __func__); |
|
177 |
} |
|
178 |
|
|
179 |
void qemu_thread_get_self(QemuThread *thread) |
|
180 |
{ |
|
181 |
thread->thread = pthread_self(); |
|
182 |
} |
|
183 |
|
|
184 |
int qemu_thread_is_self(QemuThread *thread) |
|
185 |
{ |
|
186 |
return pthread_equal(pthread_self(), thread->thread); |
|
187 |
} |
|
188 |
|
|
189 |
void qemu_thread_exit(void *retval) |
|
190 |
{ |
|
191 |
pthread_exit(retval); |
|
192 |
} |
b/qemu-thread.h | ||
---|---|---|
1 | 1 |
#ifndef __QEMU_THREAD_H |
2 | 2 |
#define __QEMU_THREAD_H 1 |
3 |
#include "semaphore.h" |
|
4 |
#include "pthread.h" |
|
5 |
|
|
6 |
struct QemuMutex { |
|
7 |
pthread_mutex_t lock; |
|
8 |
}; |
|
9 |
|
|
10 |
struct QemuCond { |
|
11 |
pthread_cond_t cond; |
|
12 |
}; |
|
13 |
|
|
14 |
struct QemuThread { |
|
15 |
pthread_t thread; |
|
16 |
}; |
|
17 | 3 |
|
18 | 4 |
typedef struct QemuMutex QemuMutex; |
19 | 5 |
typedef struct QemuCond QemuCond; |
20 | 6 |
typedef struct QemuThread QemuThread; |
21 | 7 |
|
8 |
#ifdef _WIN32 |
|
9 |
#include "qemu-thread-win32.h" |
|
10 |
#else |
|
11 |
#include "qemu-thread-posix.h" |
|
12 |
#endif |
|
13 |
|
|
22 | 14 |
void qemu_mutex_init(QemuMutex *mutex); |
23 | 15 |
void qemu_mutex_destroy(QemuMutex *mutex); |
24 | 16 |
void qemu_mutex_lock(QemuMutex *mutex); |
... | ... | |
28 | 20 |
|
29 | 21 |
void qemu_cond_init(QemuCond *cond); |
30 | 22 |
void qemu_cond_destroy(QemuCond *cond); |
23 |
|
|
24 |
/* |
|
25 |
* IMPORTANT: The implementation does not guarantee that pthread_cond_signal |
|
26 |
* and pthread_cond_broadcast can be called except while the same mutex is |
|
27 |
* held as in the corresponding pthread_cond_wait calls! |
|
28 |
*/ |
|
31 | 29 |
void qemu_cond_signal(QemuCond *cond); |
32 | 30 |
void qemu_cond_broadcast(QemuCond *cond); |
33 | 31 |
void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex); |
... | ... | |
36 | 34 |
void qemu_thread_create(QemuThread *thread, |
37 | 35 |
void *(*start_routine)(void*), |
38 | 36 |
void *arg); |
39 |
void qemu_thread_signal(QemuThread *thread, int sig); |
|
40 | 37 |
void qemu_thread_get_self(QemuThread *thread); |
41 | 38 |
int qemu_thread_is_self(QemuThread *thread); |
42 | 39 |
void qemu_thread_exit(void *retval); |
Also available in: Unified diff