blob: 4ef9c7b3f83469c87d09b66751ceb2e67e7877e1 [file] [log] [blame]
aliguorie5d355d2009-04-24 18:03:15 +00001/*
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>
Paolo Bonzini38b14db2011-08-08 14:36:41 +020020#include <limits.h>
21#include <unistd.h>
22#include <sys/time.h>
aliguorie5d355d2009-04-24 18:03:15 +000023#include "qemu-thread.h"
24
25static void error_exit(int err, const char *msg)
26{
27 fprintf(stderr, "qemu: %s: %s\n", msg, strerror(err));
Jan Kiszka53380ac2011-09-21 09:28:31 +020028 abort();
aliguorie5d355d2009-04-24 18:03:15 +000029}
30
31void qemu_mutex_init(QemuMutex *mutex)
32{
33 int err;
Paolo Bonzini89b48b52011-03-12 17:43:54 +010034 pthread_mutexattr_t mutexattr;
aliguorie5d355d2009-04-24 18:03:15 +000035
Paolo Bonzini89b48b52011-03-12 17:43:54 +010036 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);
aliguorie5d355d2009-04-24 18:03:15 +000040 if (err)
41 error_exit(err, __func__);
42}
43
Corentin Chary313b1d62010-07-07 20:58:01 +020044void 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
aliguorie5d355d2009-04-24 18:03:15 +000053void 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
62int qemu_mutex_trylock(QemuMutex *mutex)
63{
64 return pthread_mutex_trylock(&mutex->lock);
65}
66
aliguorie5d355d2009-04-24 18:03:15 +000067void 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
76void 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
Corentin Chary313b1d62010-07-07 20:58:01 +020085void 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
aliguorie5d355d2009-04-24 18:03:15 +000094void 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
103void 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
112void 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
Paolo Bonzini38b14db2011-08-08 14:36:41 +0200121void qemu_sem_init(QemuSemaphore *sem, int init)
122{
123 int rc;
124
Paolo Bonzinic166cb72012-11-02 15:43:21 +0100125#if defined(__OpenBSD__) || defined(__APPLE__) || defined(__NetBSD__)
126 rc = pthread_mutex_init(&sem->lock, NULL);
127 if (rc != 0) {
128 error_exit(rc, __func__);
129 }
130 rc = pthread_cond_init(&sem->cond, NULL);
131 if (rc != 0) {
132 error_exit(rc, __func__);
133 }
134 if (init < 0) {
135 error_exit(EINVAL, __func__);
136 }
137 sem->count = init;
138#else
Paolo Bonzini38b14db2011-08-08 14:36:41 +0200139 rc = sem_init(&sem->sem, 0, init);
140 if (rc < 0) {
141 error_exit(errno, __func__);
142 }
Paolo Bonzinic166cb72012-11-02 15:43:21 +0100143#endif
Paolo Bonzini38b14db2011-08-08 14:36:41 +0200144}
145
146void qemu_sem_destroy(QemuSemaphore *sem)
147{
148 int rc;
149
Paolo Bonzinic166cb72012-11-02 15:43:21 +0100150#if defined(__OpenBSD__) || defined(__APPLE__) || defined(__NetBSD__)
151 rc = pthread_cond_destroy(&sem->cond);
152 if (rc < 0) {
153 error_exit(rc, __func__);
154 }
155 rc = pthread_mutex_destroy(&sem->lock);
156 if (rc < 0) {
157 error_exit(rc, __func__);
158 }
159#else
Paolo Bonzini38b14db2011-08-08 14:36:41 +0200160 rc = sem_destroy(&sem->sem);
161 if (rc < 0) {
162 error_exit(errno, __func__);
163 }
Paolo Bonzinic166cb72012-11-02 15:43:21 +0100164#endif
Paolo Bonzini38b14db2011-08-08 14:36:41 +0200165}
166
167void qemu_sem_post(QemuSemaphore *sem)
168{
169 int rc;
170
Paolo Bonzinic166cb72012-11-02 15:43:21 +0100171#if defined(__OpenBSD__) || defined(__APPLE__) || defined(__NetBSD__)
172 pthread_mutex_lock(&sem->lock);
173 if (sem->count == INT_MAX) {
174 rc = EINVAL;
175 } else if (sem->count++ < 0) {
176 rc = pthread_cond_signal(&sem->cond);
177 } else {
178 rc = 0;
179 }
180 pthread_mutex_unlock(&sem->lock);
181 if (rc != 0) {
182 error_exit(rc, __func__);
183 }
184#else
Paolo Bonzini38b14db2011-08-08 14:36:41 +0200185 rc = sem_post(&sem->sem);
186 if (rc < 0) {
187 error_exit(errno, __func__);
188 }
Paolo Bonzinic166cb72012-11-02 15:43:21 +0100189#endif
190}
191
192static void compute_abs_deadline(struct timespec *ts, int ms)
193{
194 struct timeval tv;
195 gettimeofday(&tv, NULL);
196 ts->tv_nsec = tv.tv_usec * 1000 + (ms % 1000) * 1000000;
197 ts->tv_sec = tv.tv_sec + ms / 1000;
198 if (ts->tv_nsec >= 1000000000) {
199 ts->tv_sec++;
200 ts->tv_nsec -= 1000000000;
201 }
Paolo Bonzini38b14db2011-08-08 14:36:41 +0200202}
203
204int qemu_sem_timedwait(QemuSemaphore *sem, int ms)
205{
206 int rc;
Paolo Bonzinic166cb72012-11-02 15:43:21 +0100207 struct timespec ts;
Paolo Bonzini38b14db2011-08-08 14:36:41 +0200208
Paolo Bonzinic166cb72012-11-02 15:43:21 +0100209#if defined(__OpenBSD__) || defined(__APPLE__) || defined(__NetBSD__)
210 compute_abs_deadline(&ts, ms);
211 pthread_mutex_lock(&sem->lock);
212 --sem->count;
213 while (sem->count < 0) {
214 rc = pthread_cond_timedwait(&sem->cond, &sem->lock, &ts);
215 if (rc == ETIMEDOUT) {
216 break;
217 }
218 if (rc != 0) {
219 error_exit(rc, __func__);
220 }
221 }
222 pthread_mutex_unlock(&sem->lock);
223 return (rc == ETIMEDOUT ? -1 : 0);
224#else
Paolo Bonzini38b14db2011-08-08 14:36:41 +0200225 if (ms <= 0) {
226 /* This is cheaper than sem_timedwait. */
227 do {
228 rc = sem_trywait(&sem->sem);
229 } while (rc == -1 && errno == EINTR);
230 if (rc == -1 && errno == EAGAIN) {
231 return -1;
232 }
233 } else {
Paolo Bonzinic166cb72012-11-02 15:43:21 +0100234 compute_abs_deadline(&ts, ms);
Paolo Bonzini38b14db2011-08-08 14:36:41 +0200235 do {
236 rc = sem_timedwait(&sem->sem, &ts);
237 } while (rc == -1 && errno == EINTR);
238 if (rc == -1 && errno == ETIMEDOUT) {
239 return -1;
240 }
241 }
242 if (rc < 0) {
243 error_exit(errno, __func__);
244 }
245 return 0;
Paolo Bonzinic166cb72012-11-02 15:43:21 +0100246#endif
Paolo Bonzini38b14db2011-08-08 14:36:41 +0200247}
248
249void qemu_sem_wait(QemuSemaphore *sem)
250{
Paolo Bonzinic166cb72012-11-02 15:43:21 +0100251#if defined(__OpenBSD__) || defined(__APPLE__) || defined(__NetBSD__)
252 pthread_mutex_lock(&sem->lock);
253 --sem->count;
254 while (sem->count < 0) {
255 pthread_cond_wait(&sem->cond, &sem->lock);
256 }
257 pthread_mutex_unlock(&sem->lock);
258#else
Paolo Bonzini38b14db2011-08-08 14:36:41 +0200259 int rc;
260
261 do {
262 rc = sem_wait(&sem->sem);
263 } while (rc == -1 && errno == EINTR);
264 if (rc < 0) {
265 error_exit(errno, __func__);
266 }
Paolo Bonzinic166cb72012-11-02 15:43:21 +0100267#endif
Paolo Bonzini38b14db2011-08-08 14:36:41 +0200268}
269
aliguorie5d355d2009-04-24 18:03:15 +0000270void qemu_thread_create(QemuThread *thread,
271 void *(*start_routine)(void*),
Jan Kiszkacf218712011-12-12 17:21:31 +0100272 void *arg, int mode)
aliguorie5d355d2009-04-24 18:03:15 +0000273{
Jan Kiszkacf218712011-12-12 17:21:31 +0100274 sigset_t set, oldset;
aliguorie5d355d2009-04-24 18:03:15 +0000275 int err;
Jan Kiszka87630462011-12-12 17:21:32 +0100276 pthread_attr_t attr;
aliguorie5d355d2009-04-24 18:03:15 +0000277
Jan Kiszka87630462011-12-12 17:21:32 +0100278 err = pthread_attr_init(&attr);
279 if (err) {
280 error_exit(err, __func__);
281 }
282 if (mode == QEMU_THREAD_DETACHED) {
283 err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
284 if (err) {
285 error_exit(err, __func__);
286 }
287 }
Paolo Bonzini55541c82010-06-03 15:20:32 +0200288
Jan Kiszkacf218712011-12-12 17:21:31 +0100289 /* Leave signal handling to the iothread. */
Paolo Bonzini55541c82010-06-03 15:20:32 +0200290 sigfillset(&set);
291 pthread_sigmask(SIG_SETMASK, &set, &oldset);
Jan Kiszka87630462011-12-12 17:21:32 +0100292 err = pthread_create(&thread->thread, &attr, start_routine, arg);
aliguorie5d355d2009-04-24 18:03:15 +0000293 if (err)
294 error_exit(err, __func__);
Paolo Bonzini55541c82010-06-03 15:20:32 +0200295
296 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
Jan Kiszka87630462011-12-12 17:21:32 +0100297
298 pthread_attr_destroy(&attr);
aliguorie5d355d2009-04-24 18:03:15 +0000299}
300
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100301void qemu_thread_get_self(QemuThread *thread)
aliguorie5d355d2009-04-24 18:03:15 +0000302{
303 thread->thread = pthread_self();
304}
305
Andreas Färber2d797b62012-05-02 17:21:31 +0200306bool qemu_thread_is_self(QemuThread *thread)
aliguorie5d355d2009-04-24 18:03:15 +0000307{
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100308 return pthread_equal(pthread_self(), thread->thread);
aliguorie5d355d2009-04-24 18:03:15 +0000309}
310
Corentin Chary313b1d62010-07-07 20:58:01 +0200311void qemu_thread_exit(void *retval)
312{
313 pthread_exit(retval);
314}
Jan Kiszka87630462011-12-12 17:21:32 +0100315
316void *qemu_thread_join(QemuThread *thread)
317{
318 int err;
319 void *ret;
320
321 err = pthread_join(thread->thread, &ret);
322 if (err) {
323 error_exit(err, __func__);
324 }
325 return ret;
326}