]> git.proxmox.com Git - qemu.git/blame - qemu-thread-posix.c
Version 1.0.1
[qemu.git] / qemu-thread-posix.c
CommitLineData
e5d355d1
AL
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 "qemu-thread.h"
21
22static void error_exit(int err, const char *msg)
23{
24 fprintf(stderr, "qemu: %s: %s\n", msg, strerror(err));
53380ac3 25 abort();
e5d355d1
AL
26}
27
28void qemu_mutex_init(QemuMutex *mutex)
29{
30 int err;
89b48b56 31 pthread_mutexattr_t mutexattr;
e5d355d1 32
89b48b56
PB
33 pthread_mutexattr_init(&mutexattr);
34 pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_ERRORCHECK);
35 err = pthread_mutex_init(&mutex->lock, &mutexattr);
36 pthread_mutexattr_destroy(&mutexattr);
e5d355d1
AL
37 if (err)
38 error_exit(err, __func__);
39}
40
313b1d69
CC
41void qemu_mutex_destroy(QemuMutex *mutex)
42{
43 int err;
44
45 err = pthread_mutex_destroy(&mutex->lock);
46 if (err)
47 error_exit(err, __func__);
48}
49
e5d355d1
AL
50void qemu_mutex_lock(QemuMutex *mutex)
51{
52 int err;
53
54 err = pthread_mutex_lock(&mutex->lock);
55 if (err)
56 error_exit(err, __func__);
57}
58
59int qemu_mutex_trylock(QemuMutex *mutex)
60{
61 return pthread_mutex_trylock(&mutex->lock);
62}
63
e5d355d1
AL
64void qemu_mutex_unlock(QemuMutex *mutex)
65{
66 int err;
67
68 err = pthread_mutex_unlock(&mutex->lock);
69 if (err)
70 error_exit(err, __func__);
71}
72
73void qemu_cond_init(QemuCond *cond)
74{
75 int err;
76
77 err = pthread_cond_init(&cond->cond, NULL);
78 if (err)
79 error_exit(err, __func__);
80}
81
313b1d69
CC
82void qemu_cond_destroy(QemuCond *cond)
83{
84 int err;
85
86 err = pthread_cond_destroy(&cond->cond);
87 if (err)
88 error_exit(err, __func__);
89}
90
e5d355d1
AL
91void qemu_cond_signal(QemuCond *cond)
92{
93 int err;
94
95 err = pthread_cond_signal(&cond->cond);
96 if (err)
97 error_exit(err, __func__);
98}
99
100void qemu_cond_broadcast(QemuCond *cond)
101{
102 int err;
103
104 err = pthread_cond_broadcast(&cond->cond);
105 if (err)
106 error_exit(err, __func__);
107}
108
109void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex)
110{
111 int err;
112
113 err = pthread_cond_wait(&cond->cond, &mutex->lock);
114 if (err)
115 error_exit(err, __func__);
116}
117
e5d355d1
AL
118void qemu_thread_create(QemuThread *thread,
119 void *(*start_routine)(void*),
120 void *arg)
121{
122 int err;
123
55541c8a
PB
124 /* Leave signal handling to the iothread. */
125 sigset_t set, oldset;
126
127 sigfillset(&set);
128 pthread_sigmask(SIG_SETMASK, &set, &oldset);
e5d355d1
AL
129 err = pthread_create(&thread->thread, NULL, start_routine, arg);
130 if (err)
131 error_exit(err, __func__);
55541c8a
PB
132
133 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
e5d355d1
AL
134}
135
b7680cb6 136void qemu_thread_get_self(QemuThread *thread)
e5d355d1
AL
137{
138 thread->thread = pthread_self();
139}
140
b7680cb6 141int qemu_thread_is_self(QemuThread *thread)
e5d355d1 142{
b7680cb6 143 return pthread_equal(pthread_self(), thread->thread);
e5d355d1
AL
144}
145
313b1d69
CC
146void qemu_thread_exit(void *retval)
147{
148 pthread_exit(retval);
149}