aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 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)); |
Jan Kiszka | 53380ac | 2011-09-21 09:28:31 +0200 | [diff] [blame] | 25 | abort(); |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | void qemu_mutex_init(QemuMutex *mutex) |
| 29 | { |
| 30 | int err; |
Paolo Bonzini | 89b48b5 | 2011-03-12 17:43:54 +0100 | [diff] [blame] | 31 | pthread_mutexattr_t mutexattr; |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 32 | |
Paolo Bonzini | 89b48b5 | 2011-03-12 17:43:54 +0100 | [diff] [blame] | 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); |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 37 | if (err) |
| 38 | error_exit(err, __func__); |
| 39 | } |
| 40 | |
Corentin Chary | 313b1d6 | 2010-07-07 20:58:01 +0200 | [diff] [blame] | 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 | |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 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 | |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 64 | void 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 | |
| 73 | void 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 | |
Corentin Chary | 313b1d6 | 2010-07-07 20:58:01 +0200 | [diff] [blame] | 82 | void 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 | |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 91 | void 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 | |
| 100 | void 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 | |
| 109 | void 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 | |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 118 | void qemu_thread_create(QemuThread *thread, |
| 119 | void *(*start_routine)(void*), |
Jan Kiszka | cf21871 | 2011-12-12 17:21:31 +0100 | [diff] [blame] | 120 | void *arg, int mode) |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 121 | { |
Jan Kiszka | cf21871 | 2011-12-12 17:21:31 +0100 | [diff] [blame] | 122 | sigset_t set, oldset; |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 123 | int err; |
Jan Kiszka | 8763046 | 2011-12-12 17:21:32 +0100 | [diff] [blame] | 124 | pthread_attr_t attr; |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 125 | |
Jan Kiszka | 8763046 | 2011-12-12 17:21:32 +0100 | [diff] [blame] | 126 | err = pthread_attr_init(&attr); |
| 127 | if (err) { |
| 128 | error_exit(err, __func__); |
| 129 | } |
| 130 | if (mode == QEMU_THREAD_DETACHED) { |
| 131 | err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 132 | if (err) { |
| 133 | error_exit(err, __func__); |
| 134 | } |
| 135 | } |
Paolo Bonzini | 55541c8 | 2010-06-03 15:20:32 +0200 | [diff] [blame] | 136 | |
Jan Kiszka | cf21871 | 2011-12-12 17:21:31 +0100 | [diff] [blame] | 137 | /* Leave signal handling to the iothread. */ |
Paolo Bonzini | 55541c8 | 2010-06-03 15:20:32 +0200 | [diff] [blame] | 138 | sigfillset(&set); |
| 139 | pthread_sigmask(SIG_SETMASK, &set, &oldset); |
Jan Kiszka | 8763046 | 2011-12-12 17:21:32 +0100 | [diff] [blame] | 140 | err = pthread_create(&thread->thread, &attr, start_routine, arg); |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 141 | if (err) |
| 142 | error_exit(err, __func__); |
Paolo Bonzini | 55541c8 | 2010-06-03 15:20:32 +0200 | [diff] [blame] | 143 | |
| 144 | pthread_sigmask(SIG_SETMASK, &oldset, NULL); |
Jan Kiszka | 8763046 | 2011-12-12 17:21:32 +0100 | [diff] [blame] | 145 | |
| 146 | pthread_attr_destroy(&attr); |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Jan Kiszka | b7680cb | 2011-03-12 17:43:51 +0100 | [diff] [blame] | 149 | void qemu_thread_get_self(QemuThread *thread) |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 150 | { |
| 151 | thread->thread = pthread_self(); |
| 152 | } |
| 153 | |
Jan Kiszka | b7680cb | 2011-03-12 17:43:51 +0100 | [diff] [blame] | 154 | int qemu_thread_is_self(QemuThread *thread) |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 155 | { |
Jan Kiszka | b7680cb | 2011-03-12 17:43:51 +0100 | [diff] [blame] | 156 | return pthread_equal(pthread_self(), thread->thread); |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Corentin Chary | 313b1d6 | 2010-07-07 20:58:01 +0200 | [diff] [blame] | 159 | void qemu_thread_exit(void *retval) |
| 160 | { |
| 161 | pthread_exit(retval); |
| 162 | } |
Jan Kiszka | 8763046 | 2011-12-12 17:21:32 +0100 | [diff] [blame] | 163 | |
| 164 | void *qemu_thread_join(QemuThread *thread) |
| 165 | { |
| 166 | int err; |
| 167 | void *ret; |
| 168 | |
| 169 | err = pthread_join(thread->thread, &ret); |
| 170 | if (err) { |
| 171 | error_exit(err, __func__); |
| 172 | } |
| 173 | return ret; |
| 174 | } |