root / qemu-thread-posix.c @ 38b14db3
History | View | Annotate | Download (5 kB)
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 <limits.h> |
21 |
#include <unistd.h> |
22 |
#include <sys/time.h> |
23 |
#include "qemu-thread.h" |
24 |
|
25 |
static void error_exit(int err, const char *msg) |
26 |
{ |
27 |
fprintf(stderr, "qemu: %s: %s\n", msg, strerror(err));
|
28 |
abort(); |
29 |
} |
30 |
|
31 |
void qemu_mutex_init(QemuMutex *mutex)
|
32 |
{ |
33 |
int err;
|
34 |
pthread_mutexattr_t mutexattr; |
35 |
|
36 |
pthread_mutexattr_init(&mutexattr); |
37 |
pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_ERRORCHECK); |
38 |
err = pthread_mutex_init(&mutex->lock, &mutexattr); |
39 |
pthread_mutexattr_destroy(&mutexattr); |
40 |
if (err)
|
41 |
error_exit(err, __func__); |
42 |
} |
43 |
|
44 |
void qemu_mutex_destroy(QemuMutex *mutex)
|
45 |
{ |
46 |
int err;
|
47 |
|
48 |
err = pthread_mutex_destroy(&mutex->lock); |
49 |
if (err)
|
50 |
error_exit(err, __func__); |
51 |
} |
52 |
|
53 |
void qemu_mutex_lock(QemuMutex *mutex)
|
54 |
{ |
55 |
int err;
|
56 |
|
57 |
err = pthread_mutex_lock(&mutex->lock); |
58 |
if (err)
|
59 |
error_exit(err, __func__); |
60 |
} |
61 |
|
62 |
int qemu_mutex_trylock(QemuMutex *mutex)
|
63 |
{ |
64 |
return pthread_mutex_trylock(&mutex->lock);
|
65 |
} |
66 |
|
67 |
void qemu_mutex_unlock(QemuMutex *mutex)
|
68 |
{ |
69 |
int err;
|
70 |
|
71 |
err = pthread_mutex_unlock(&mutex->lock); |
72 |
if (err)
|
73 |
error_exit(err, __func__); |
74 |
} |
75 |
|
76 |
void qemu_cond_init(QemuCond *cond)
|
77 |
{ |
78 |
int err;
|
79 |
|
80 |
err = pthread_cond_init(&cond->cond, NULL);
|
81 |
if (err)
|
82 |
error_exit(err, __func__); |
83 |
} |
84 |
|
85 |
void qemu_cond_destroy(QemuCond *cond)
|
86 |
{ |
87 |
int err;
|
88 |
|
89 |
err = pthread_cond_destroy(&cond->cond); |
90 |
if (err)
|
91 |
error_exit(err, __func__); |
92 |
} |
93 |
|
94 |
void qemu_cond_signal(QemuCond *cond)
|
95 |
{ |
96 |
int err;
|
97 |
|
98 |
err = pthread_cond_signal(&cond->cond); |
99 |
if (err)
|
100 |
error_exit(err, __func__); |
101 |
} |
102 |
|
103 |
void qemu_cond_broadcast(QemuCond *cond)
|
104 |
{ |
105 |
int err;
|
106 |
|
107 |
err = pthread_cond_broadcast(&cond->cond); |
108 |
if (err)
|
109 |
error_exit(err, __func__); |
110 |
} |
111 |
|
112 |
void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex)
|
113 |
{ |
114 |
int err;
|
115 |
|
116 |
err = pthread_cond_wait(&cond->cond, &mutex->lock); |
117 |
if (err)
|
118 |
error_exit(err, __func__); |
119 |
} |
120 |
|
121 |
void qemu_sem_init(QemuSemaphore *sem, int init) |
122 |
{ |
123 |
int rc;
|
124 |
|
125 |
rc = sem_init(&sem->sem, 0, init);
|
126 |
if (rc < 0) { |
127 |
error_exit(errno, __func__); |
128 |
} |
129 |
} |
130 |
|
131 |
void qemu_sem_destroy(QemuSemaphore *sem)
|
132 |
{ |
133 |
int rc;
|
134 |
|
135 |
rc = sem_destroy(&sem->sem); |
136 |
if (rc < 0) { |
137 |
error_exit(errno, __func__); |
138 |
} |
139 |
} |
140 |
|
141 |
void qemu_sem_post(QemuSemaphore *sem)
|
142 |
{ |
143 |
int rc;
|
144 |
|
145 |
rc = sem_post(&sem->sem); |
146 |
if (rc < 0) { |
147 |
error_exit(errno, __func__); |
148 |
} |
149 |
} |
150 |
|
151 |
int qemu_sem_timedwait(QemuSemaphore *sem, int ms) |
152 |
{ |
153 |
int rc;
|
154 |
|
155 |
if (ms <= 0) { |
156 |
/* This is cheaper than sem_timedwait. */
|
157 |
do {
|
158 |
rc = sem_trywait(&sem->sem); |
159 |
} while (rc == -1 && errno == EINTR); |
160 |
if (rc == -1 && errno == EAGAIN) { |
161 |
return -1; |
162 |
} |
163 |
} else {
|
164 |
struct timeval tv;
|
165 |
struct timespec ts;
|
166 |
gettimeofday(&tv, NULL);
|
167 |
ts.tv_nsec = tv.tv_usec * 1000 + (ms % 1000) * 1000000; |
168 |
ts.tv_sec = tv.tv_sec + ms / 1000;
|
169 |
if (ts.tv_nsec >= 1000000000) { |
170 |
ts.tv_sec++; |
171 |
ts.tv_nsec -= 1000000000;
|
172 |
} |
173 |
do {
|
174 |
rc = sem_timedwait(&sem->sem, &ts); |
175 |
} while (rc == -1 && errno == EINTR); |
176 |
if (rc == -1 && errno == ETIMEDOUT) { |
177 |
return -1; |
178 |
} |
179 |
} |
180 |
if (rc < 0) { |
181 |
error_exit(errno, __func__); |
182 |
} |
183 |
return 0; |
184 |
} |
185 |
|
186 |
void qemu_sem_wait(QemuSemaphore *sem)
|
187 |
{ |
188 |
int rc;
|
189 |
|
190 |
do {
|
191 |
rc = sem_wait(&sem->sem); |
192 |
} while (rc == -1 && errno == EINTR); |
193 |
if (rc < 0) { |
194 |
error_exit(errno, __func__); |
195 |
} |
196 |
} |
197 |
|
198 |
void qemu_thread_create(QemuThread *thread,
|
199 |
void *(*start_routine)(void*), |
200 |
void *arg, int mode) |
201 |
{ |
202 |
sigset_t set, oldset; |
203 |
int err;
|
204 |
pthread_attr_t attr; |
205 |
|
206 |
err = pthread_attr_init(&attr); |
207 |
if (err) {
|
208 |
error_exit(err, __func__); |
209 |
} |
210 |
if (mode == QEMU_THREAD_DETACHED) {
|
211 |
err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
212 |
if (err) {
|
213 |
error_exit(err, __func__); |
214 |
} |
215 |
} |
216 |
|
217 |
/* Leave signal handling to the iothread. */
|
218 |
sigfillset(&set); |
219 |
pthread_sigmask(SIG_SETMASK, &set, &oldset); |
220 |
err = pthread_create(&thread->thread, &attr, start_routine, arg); |
221 |
if (err)
|
222 |
error_exit(err, __func__); |
223 |
|
224 |
pthread_sigmask(SIG_SETMASK, &oldset, NULL);
|
225 |
|
226 |
pthread_attr_destroy(&attr); |
227 |
} |
228 |
|
229 |
void qemu_thread_get_self(QemuThread *thread)
|
230 |
{ |
231 |
thread->thread = pthread_self(); |
232 |
} |
233 |
|
234 |
bool qemu_thread_is_self(QemuThread *thread)
|
235 |
{ |
236 |
return pthread_equal(pthread_self(), thread->thread);
|
237 |
} |
238 |
|
239 |
void qemu_thread_exit(void *retval) |
240 |
{ |
241 |
pthread_exit(retval); |
242 |
} |
243 |
|
244 |
void *qemu_thread_join(QemuThread *thread)
|
245 |
{ |
246 |
int err;
|
247 |
void *ret;
|
248 |
|
249 |
err = pthread_join(thread->thread, &ret); |
250 |
if (err) {
|
251 |
error_exit(err, __func__); |
252 |
} |
253 |
return ret;
|
254 |
} |