Revision 44bc10d5

b/qemu-thread-posix.c
61 61
    return pthread_mutex_trylock(&mutex->lock);
62 62
}
63 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 64
void qemu_mutex_unlock(QemuMutex *mutex)
89 65
{
90 66
    int err;
......
139 115
        error_exit(err, __func__);
140 116
}
141 117

  
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 118
void qemu_thread_create(QemuThread *thread,
157 119
                       void *(*start_routine)(void*),
158 120
                       void *arg)
b/qemu-thread.h
15 15
void qemu_mutex_destroy(QemuMutex *mutex);
16 16
void qemu_mutex_lock(QemuMutex *mutex);
17 17
int qemu_mutex_trylock(QemuMutex *mutex);
18
int qemu_mutex_timedlock(QemuMutex *mutex, uint64_t msecs);
19 18
void qemu_mutex_unlock(QemuMutex *mutex);
20 19

  
21 20
void qemu_cond_init(QemuCond *cond);
......
29 28
void qemu_cond_signal(QemuCond *cond);
30 29
void qemu_cond_broadcast(QemuCond *cond);
31 30
void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex);
32
int qemu_cond_timedwait(QemuCond *cond, QemuMutex *mutex, uint64_t msecs);
33 31

  
34 32
void qemu_thread_create(QemuThread *thread,
35 33
                       void *(*start_routine)(void*),

Also available in: Unified diff