Statistics
| Branch: | Revision:

root / qemu-thread-posix.c @ cc015e9a

History | View | Annotate | Download (3.8 kB)

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

    
22
static void error_exit(int err, const char *msg)
23
{
24
    fprintf(stderr, "qemu: %s: %s\n", msg, strerror(err));
25
    exit(1);
26
}
27

    
28
void qemu_mutex_init(QemuMutex *mutex)
29
{
30
    int err;
31
    pthread_mutexattr_t mutexattr;
32

    
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);
37
    if (err)
38
        error_exit(err, __func__);
39
}
40

    
41
void 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

    
50
void 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

    
59
int qemu_mutex_trylock(QemuMutex *mutex)
60
{
61
    return pthread_mutex_trylock(&mutex->lock);
62
}
63

    
64
static void timespec_add_ms(struct timespec *ts, uint64_t msecs)
65
{
66
    ts->tv_sec = ts->tv_sec + (long)(msecs / 1000);
67
    ts->tv_nsec = (ts->tv_nsec + ((long)msecs % 1000) * 1000000);
68
    if (ts->tv_nsec >= 1000000000) {
69
        ts->tv_nsec -= 1000000000;
70
        ts->tv_sec++;
71
    }
72
}
73

    
74
int qemu_mutex_timedlock(QemuMutex *mutex, uint64_t msecs)
75
{
76
    int err;
77
    struct timespec ts;
78

    
79
    clock_gettime(CLOCK_REALTIME, &ts);
80
    timespec_add_ms(&ts, msecs);
81

    
82
    err = pthread_mutex_timedlock(&mutex->lock, &ts);
83
    if (err && err != ETIMEDOUT)
84
        error_exit(err, __func__);
85
    return err;
86
}
87

    
88
void qemu_mutex_unlock(QemuMutex *mutex)
89
{
90
    int err;
91

    
92
    err = pthread_mutex_unlock(&mutex->lock);
93
    if (err)
94
        error_exit(err, __func__);
95
}
96

    
97
void qemu_cond_init(QemuCond *cond)
98
{
99
    int err;
100

    
101
    err = pthread_cond_init(&cond->cond, NULL);
102
    if (err)
103
        error_exit(err, __func__);
104
}
105

    
106
void qemu_cond_destroy(QemuCond *cond)
107
{
108
    int err;
109

    
110
    err = pthread_cond_destroy(&cond->cond);
111
    if (err)
112
        error_exit(err, __func__);
113
}
114

    
115
void qemu_cond_signal(QemuCond *cond)
116
{
117
    int err;
118

    
119
    err = pthread_cond_signal(&cond->cond);
120
    if (err)
121
        error_exit(err, __func__);
122
}
123

    
124
void qemu_cond_broadcast(QemuCond *cond)
125
{
126
    int err;
127

    
128
    err = pthread_cond_broadcast(&cond->cond);
129
    if (err)
130
        error_exit(err, __func__);
131
}
132

    
133
void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex)
134
{
135
    int err;
136

    
137
    err = pthread_cond_wait(&cond->cond, &mutex->lock);
138
    if (err)
139
        error_exit(err, __func__);
140
}
141

    
142
int qemu_cond_timedwait(QemuCond *cond, QemuMutex *mutex, uint64_t msecs)
143
{
144
    struct timespec ts;
145
    int err;
146

    
147
    clock_gettime(CLOCK_REALTIME, &ts);
148
    timespec_add_ms(&ts, msecs);
149

    
150
    err = pthread_cond_timedwait(&cond->cond, &mutex->lock, &ts);
151
    if (err && err != ETIMEDOUT)
152
        error_exit(err, __func__);
153
    return err;
154
}
155

    
156
void qemu_thread_create(QemuThread *thread,
157
                       void *(*start_routine)(void*),
158
                       void *arg)
159
{
160
    int err;
161

    
162
    /* Leave signal handling to the iothread.  */
163
    sigset_t set, oldset;
164

    
165
    sigfillset(&set);
166
    pthread_sigmask(SIG_SETMASK, &set, &oldset);
167
    err = pthread_create(&thread->thread, NULL, start_routine, arg);
168
    if (err)
169
        error_exit(err, __func__);
170

    
171
    pthread_sigmask(SIG_SETMASK, &oldset, NULL);
172
}
173

    
174
void qemu_thread_get_self(QemuThread *thread)
175
{
176
    thread->thread = pthread_self();
177
}
178

    
179
int qemu_thread_is_self(QemuThread *thread)
180
{
181
   return pthread_equal(pthread_self(), thread->thread);
182
}
183

    
184
void qemu_thread_exit(void *retval)
185
{
186
    pthread_exit(retval);
187
}