2 * Wrappers around mutex/cond/thread functions
4 * Copyright Red Hat, Inc. 2009
7 * Marcelo Tosatti <mtosatti@redhat.com>
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.
23 #include "qemu-thread.h"
25 static void error_exit(int err
, const char *msg
)
27 fprintf(stderr
, "qemu: %s: %s\n", msg
, strerror(err
));
31 void qemu_mutex_init(QemuMutex
*mutex
)
34 pthread_mutexattr_t mutexattr
;
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
);
41 error_exit(err
, __func__
);
44 void qemu_mutex_destroy(QemuMutex
*mutex
)
48 err
= pthread_mutex_destroy(&mutex
->lock
);
50 error_exit(err
, __func__
);
53 void qemu_mutex_lock(QemuMutex
*mutex
)
57 err
= pthread_mutex_lock(&mutex
->lock
);
59 error_exit(err
, __func__
);
62 int qemu_mutex_trylock(QemuMutex
*mutex
)
64 return pthread_mutex_trylock(&mutex
->lock
);
67 void qemu_mutex_unlock(QemuMutex
*mutex
)
71 err
= pthread_mutex_unlock(&mutex
->lock
);
73 error_exit(err
, __func__
);
76 void qemu_cond_init(QemuCond
*cond
)
80 err
= pthread_cond_init(&cond
->cond
, NULL
);
82 error_exit(err
, __func__
);
85 void qemu_cond_destroy(QemuCond
*cond
)
89 err
= pthread_cond_destroy(&cond
->cond
);
91 error_exit(err
, __func__
);
94 void qemu_cond_signal(QemuCond
*cond
)
98 err
= pthread_cond_signal(&cond
->cond
);
100 error_exit(err
, __func__
);
103 void qemu_cond_broadcast(QemuCond
*cond
)
107 err
= pthread_cond_broadcast(&cond
->cond
);
109 error_exit(err
, __func__
);
112 void qemu_cond_wait(QemuCond
*cond
, QemuMutex
*mutex
)
116 err
= pthread_cond_wait(&cond
->cond
, &mutex
->lock
);
118 error_exit(err
, __func__
);
121 void qemu_sem_init(QemuSemaphore
*sem
, int init
)
125 #if defined(__OpenBSD__) || defined(__APPLE__) || defined(__NetBSD__)
126 rc
= pthread_mutex_init(&sem
->lock
, NULL
);
128 error_exit(rc
, __func__
);
130 rc
= pthread_cond_init(&sem
->cond
, NULL
);
132 error_exit(rc
, __func__
);
135 error_exit(EINVAL
, __func__
);
139 rc
= sem_init(&sem
->sem
, 0, init
);
141 error_exit(errno
, __func__
);
146 void qemu_sem_destroy(QemuSemaphore
*sem
)
150 #if defined(__OpenBSD__) || defined(__APPLE__) || defined(__NetBSD__)
151 rc
= pthread_cond_destroy(&sem
->cond
);
153 error_exit(rc
, __func__
);
155 rc
= pthread_mutex_destroy(&sem
->lock
);
157 error_exit(rc
, __func__
);
160 rc
= sem_destroy(&sem
->sem
);
162 error_exit(errno
, __func__
);
167 void qemu_sem_post(QemuSemaphore
*sem
)
171 #if defined(__OpenBSD__) || defined(__APPLE__) || defined(__NetBSD__)
172 pthread_mutex_lock(&sem
->lock
);
173 if (sem
->count
== INT_MAX
) {
175 } else if (sem
->count
++ < 0) {
176 rc
= pthread_cond_signal(&sem
->cond
);
180 pthread_mutex_unlock(&sem
->lock
);
182 error_exit(rc
, __func__
);
185 rc
= sem_post(&sem
->sem
);
187 error_exit(errno
, __func__
);
192 static void compute_abs_deadline(struct timespec
*ts
, int ms
)
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) {
200 ts
->tv_nsec
-= 1000000000;
204 int qemu_sem_timedwait(QemuSemaphore
*sem
, int ms
)
209 #if defined(__OpenBSD__) || defined(__APPLE__) || defined(__NetBSD__)
210 compute_abs_deadline(&ts
, ms
);
211 pthread_mutex_lock(&sem
->lock
);
213 while (sem
->count
< 0) {
214 rc
= pthread_cond_timedwait(&sem
->cond
, &sem
->lock
, &ts
);
215 if (rc
== ETIMEDOUT
) {
219 error_exit(rc
, __func__
);
222 pthread_mutex_unlock(&sem
->lock
);
223 return (rc
== ETIMEDOUT
? -1 : 0);
226 /* This is cheaper than sem_timedwait. */
228 rc
= sem_trywait(&sem
->sem
);
229 } while (rc
== -1 && errno
== EINTR
);
230 if (rc
== -1 && errno
== EAGAIN
) {
234 compute_abs_deadline(&ts
, ms
);
236 rc
= sem_timedwait(&sem
->sem
, &ts
);
237 } while (rc
== -1 && errno
== EINTR
);
238 if (rc
== -1 && errno
== ETIMEDOUT
) {
243 error_exit(errno
, __func__
);
249 void qemu_sem_wait(QemuSemaphore
*sem
)
251 #if defined(__OpenBSD__) || defined(__APPLE__) || defined(__NetBSD__)
252 pthread_mutex_lock(&sem
->lock
);
254 while (sem
->count
< 0) {
255 pthread_cond_wait(&sem
->cond
, &sem
->lock
);
257 pthread_mutex_unlock(&sem
->lock
);
262 rc
= sem_wait(&sem
->sem
);
263 } while (rc
== -1 && errno
== EINTR
);
265 error_exit(errno
, __func__
);
270 void qemu_thread_create(QemuThread
*thread
,
271 void *(*start_routine
)(void*),
274 sigset_t set
, oldset
;
278 err
= pthread_attr_init(&attr
);
280 error_exit(err
, __func__
);
282 if (mode
== QEMU_THREAD_DETACHED
) {
283 err
= pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
285 error_exit(err
, __func__
);
289 /* Leave signal handling to the iothread. */
291 pthread_sigmask(SIG_SETMASK
, &set
, &oldset
);
292 err
= pthread_create(&thread
->thread
, &attr
, start_routine
, arg
);
294 error_exit(err
, __func__
);
296 pthread_sigmask(SIG_SETMASK
, &oldset
, NULL
);
298 pthread_attr_destroy(&attr
);
301 void qemu_thread_get_self(QemuThread
*thread
)
303 thread
->thread
= pthread_self();
306 bool qemu_thread_is_self(QemuThread
*thread
)
308 return pthread_equal(pthread_self(), thread
->thread
);
311 void qemu_thread_exit(void *retval
)
313 pthread_exit(retval
);
316 void *qemu_thread_join(QemuThread
*thread
)
321 err
= pthread_join(thread
->thread
, &ret
);
323 error_exit(err
, __func__
);