2 * Wrappers around mutex/cond/thread functions
4 * Copyright Red Hat, Inc. 2009
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.
20 #include "qemu-thread.h"
22 static void error_exit(int err, const char *msg)
24 fprintf(stderr, "qemu: %s: %s\n", msg, strerror(err));
28 void qemu_mutex_init(QemuMutex *mutex)
32 err = pthread_mutex_init(&mutex->lock, NULL);
34 error_exit(err, __func__);
37 void qemu_mutex_destroy(QemuMutex *mutex)
41 err = pthread_mutex_destroy(&mutex->lock);
43 error_exit(err, __func__);
46 void qemu_mutex_lock(QemuMutex *mutex)
50 err = pthread_mutex_lock(&mutex->lock);
52 error_exit(err, __func__);
55 int qemu_mutex_trylock(QemuMutex *mutex)
57 return pthread_mutex_trylock(&mutex->lock);
60 static void timespec_add_ms(struct timespec *ts, uint64_t msecs)
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;
70 int qemu_mutex_timedlock(QemuMutex *mutex, uint64_t msecs)
75 clock_gettime(CLOCK_REALTIME, &ts);
76 timespec_add_ms(&ts, msecs);
78 err = pthread_mutex_timedlock(&mutex->lock, &ts);
79 if (err && err != ETIMEDOUT)
80 error_exit(err, __func__);
84 void qemu_mutex_unlock(QemuMutex *mutex)
88 err = pthread_mutex_unlock(&mutex->lock);
90 error_exit(err, __func__);
93 void qemu_cond_init(QemuCond *cond)
97 err = pthread_cond_init(&cond->cond, NULL);
99 error_exit(err, __func__);
102 void qemu_cond_destroy(QemuCond *cond)
106 err = pthread_cond_destroy(&cond->cond);
108 error_exit(err, __func__);
111 void qemu_cond_signal(QemuCond *cond)
115 err = pthread_cond_signal(&cond->cond);
117 error_exit(err, __func__);
120 void qemu_cond_broadcast(QemuCond *cond)
124 err = pthread_cond_broadcast(&cond->cond);
126 error_exit(err, __func__);
129 void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex)
133 err = pthread_cond_wait(&cond->cond, &mutex->lock);
135 error_exit(err, __func__);
138 int qemu_cond_timedwait(QemuCond *cond, QemuMutex *mutex, uint64_t msecs)
143 clock_gettime(CLOCK_REALTIME, &ts);
144 timespec_add_ms(&ts, msecs);
146 err = pthread_cond_timedwait(&cond->cond, &mutex->lock, &ts);
147 if (err && err != ETIMEDOUT)
148 error_exit(err, __func__);
152 void qemu_thread_create(QemuThread *thread,
153 void *(*start_routine)(void*),
158 /* Leave signal handling to the iothread. */
159 sigset_t set, oldset;
162 pthread_sigmask(SIG_SETMASK, &set, &oldset);
163 err = pthread_create(&thread->thread, NULL, start_routine, arg);
165 error_exit(err, __func__);
167 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
170 void qemu_thread_signal(QemuThread *thread, int sig)
174 err = pthread_kill(thread->thread, sig);
176 error_exit(err, __func__);
179 void qemu_thread_self(QemuThread *thread)
181 thread->thread = pthread_self();
184 int qemu_thread_equal(QemuThread *thread1, QemuThread *thread2)
186 return pthread_equal(thread1->thread, thread2->thread);
189 void qemu_thread_exit(void *retval)
191 pthread_exit(retval);