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 rc
= sem_init(&sem
->sem
, 0, init
);
127 error_exit(errno
, __func__
);
131 void qemu_sem_destroy(QemuSemaphore
*sem
)
135 rc
= sem_destroy(&sem
->sem
);
137 error_exit(errno
, __func__
);
141 void qemu_sem_post(QemuSemaphore
*sem
)
145 rc
= sem_post(&sem
->sem
);
147 error_exit(errno
, __func__
);
151 int qemu_sem_timedwait(QemuSemaphore
*sem
, int ms
)
156 /* This is cheaper than sem_timedwait. */
158 rc
= sem_trywait(&sem
->sem
);
159 } while (rc
== -1 && errno
== EINTR
);
160 if (rc
== -1 && errno
== EAGAIN
) {
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) {
171 ts
.tv_nsec
-= 1000000000;
174 rc
= sem_timedwait(&sem
->sem
, &ts
);
175 } while (rc
== -1 && errno
== EINTR
);
176 if (rc
== -1 && errno
== ETIMEDOUT
) {
181 error_exit(errno
, __func__
);
186 void qemu_sem_wait(QemuSemaphore
*sem
)
191 rc
= sem_wait(&sem
->sem
);
192 } while (rc
== -1 && errno
== EINTR
);
194 error_exit(errno
, __func__
);
198 void qemu_thread_create(QemuThread
*thread
,
199 void *(*start_routine
)(void*),
202 sigset_t set
, oldset
;
206 err
= pthread_attr_init(&attr
);
208 error_exit(err
, __func__
);
210 if (mode
== QEMU_THREAD_DETACHED
) {
211 err
= pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
213 error_exit(err
, __func__
);
217 /* Leave signal handling to the iothread. */
219 pthread_sigmask(SIG_SETMASK
, &set
, &oldset
);
220 err
= pthread_create(&thread
->thread
, &attr
, start_routine
, arg
);
222 error_exit(err
, __func__
);
224 pthread_sigmask(SIG_SETMASK
, &oldset
, NULL
);
226 pthread_attr_destroy(&attr
);
229 void qemu_thread_get_self(QemuThread
*thread
)
231 thread
->thread
= pthread_self();
234 bool qemu_thread_is_self(QemuThread
*thread
)
236 return pthread_equal(pthread_self(), thread
->thread
);
239 void qemu_thread_exit(void *retval
)
241 pthread_exit(retval
);
244 void *qemu_thread_join(QemuThread
*thread
)
249 err
= pthread_join(thread
->thread
, &ret
);
251 error_exit(err
, __func__
);