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 | */ |
Peter Maydell | aafd758 | 2016-01-29 17:49:55 +0000 | [diff] [blame] | 13 | #include "qemu/osdep.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 14 | #include "qemu/thread.h" |
Paolo Bonzini | c7c4d06 | 2013-09-25 14:20:59 +0800 | [diff] [blame] | 15 | #include "qemu/atomic.h" |
Paolo Bonzini | ef57137 | 2014-12-02 12:05:45 +0100 | [diff] [blame] | 16 | #include "qemu/notify.h" |
Peter Xu | f1aff7a | 2018-04-25 10:54:57 +0800 | [diff] [blame] | 17 | #include "qemu-thread-common.h" |
Robert Foley | ce9f0e5 | 2020-06-12 20:02:33 +0100 | [diff] [blame] | 18 | #include "qemu/tsan.h" |
David Hildenbrand | 7730f32 | 2022-10-14 15:47:15 +0200 | [diff] [blame] | 19 | #include "qemu/bitmap.h" |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 20 | |
Brad Smith | 3ada67a | 2022-12-18 03:22:04 -0500 | [diff] [blame] | 21 | #ifdef CONFIG_PTHREAD_SET_NAME_NP |
| 22 | #include <pthread_np.h> |
| 23 | #endif |
| 24 | |
Dr. David Alan Gilbert | 8f480de | 2014-01-30 10:20:31 +0000 | [diff] [blame] | 25 | static bool name_threads; |
| 26 | |
| 27 | void qemu_thread_naming(bool enable) |
| 28 | { |
| 29 | name_threads = enable; |
Dr. David Alan Gilbert | 5c31207 | 2014-03-12 11:48:18 +0000 | [diff] [blame] | 30 | |
Paolo Bonzini | 10f6b23 | 2021-10-07 15:08:19 +0200 | [diff] [blame] | 31 | #if !defined CONFIG_PTHREAD_SETNAME_NP_W_TID && \ |
Brad Smith | 3ada67a | 2022-12-18 03:22:04 -0500 | [diff] [blame] | 32 | !defined CONFIG_PTHREAD_SETNAME_NP_WO_TID && \ |
| 33 | !defined CONFIG_PTHREAD_SET_NAME_NP |
Dr. David Alan Gilbert | 5c31207 | 2014-03-12 11:48:18 +0000 | [diff] [blame] | 34 | /* This is a debugging option, not fatal */ |
| 35 | if (enable) { |
| 36 | fprintf(stderr, "qemu: thread naming not supported on this host\n"); |
| 37 | } |
| 38 | #endif |
Dr. David Alan Gilbert | 8f480de | 2014-01-30 10:20:31 +0000 | [diff] [blame] | 39 | } |
| 40 | |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 41 | static void error_exit(int err, const char *msg) |
| 42 | { |
| 43 | fprintf(stderr, "qemu: %s: %s\n", msg, strerror(err)); |
Jan Kiszka | 53380ac | 2011-09-21 09:28:31 +0200 | [diff] [blame] | 44 | abort(); |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Longpeng(Mike) | 657ac98 | 2022-02-22 17:05:05 +0800 | [diff] [blame] | 47 | static inline clockid_t qemu_timedwait_clockid(void) |
| 48 | { |
| 49 | #ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK |
| 50 | return CLOCK_MONOTONIC; |
| 51 | #else |
| 52 | return CLOCK_REALTIME; |
| 53 | #endif |
| 54 | } |
| 55 | |
Yury Kotov | 3dcc9c6 | 2019-09-09 16:13:33 +0300 | [diff] [blame] | 56 | static void compute_abs_deadline(struct timespec *ts, int ms) |
| 57 | { |
Longpeng(Mike) | 657ac98 | 2022-02-22 17:05:05 +0800 | [diff] [blame] | 58 | clock_gettime(qemu_timedwait_clockid(), ts); |
| 59 | ts->tv_nsec += (ms % 1000) * 1000000; |
| 60 | ts->tv_sec += ms / 1000; |
Yury Kotov | 3dcc9c6 | 2019-09-09 16:13:33 +0300 | [diff] [blame] | 61 | if (ts->tv_nsec >= 1000000000) { |
| 62 | ts->tv_sec++; |
| 63 | ts->tv_nsec -= 1000000000; |
| 64 | } |
| 65 | } |
| 66 | |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 67 | void qemu_mutex_init(QemuMutex *mutex) |
| 68 | { |
| 69 | int err; |
| 70 | |
Paolo Bonzini | 24fa904 | 2015-03-05 16:47:14 +0100 | [diff] [blame] | 71 | err = pthread_mutex_init(&mutex->lock, NULL); |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 72 | if (err) |
| 73 | error_exit(err, __func__); |
Peter Xu | f1aff7a | 2018-04-25 10:54:57 +0800 | [diff] [blame] | 74 | qemu_mutex_post_init(mutex); |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Corentin Chary | 313b1d6 | 2010-07-07 20:58:01 +0200 | [diff] [blame] | 77 | void qemu_mutex_destroy(QemuMutex *mutex) |
| 78 | { |
| 79 | int err; |
| 80 | |
Fam Zheng | c096358 | 2017-07-04 20:23:25 +0800 | [diff] [blame] | 81 | assert(mutex->initialized); |
| 82 | mutex->initialized = false; |
Corentin Chary | 313b1d6 | 2010-07-07 20:58:01 +0200 | [diff] [blame] | 83 | err = pthread_mutex_destroy(&mutex->lock); |
| 84 | if (err) |
| 85 | error_exit(err, __func__); |
| 86 | } |
| 87 | |
Alex Bennée | 6c27a0d | 2018-01-11 11:27:16 +0300 | [diff] [blame] | 88 | void qemu_mutex_lock_impl(QemuMutex *mutex, const char *file, const int line) |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 89 | { |
| 90 | int err; |
| 91 | |
Fam Zheng | c096358 | 2017-07-04 20:23:25 +0800 | [diff] [blame] | 92 | assert(mutex->initialized); |
Peter Xu | f1aff7a | 2018-04-25 10:54:57 +0800 | [diff] [blame] | 93 | qemu_mutex_pre_lock(mutex, file, line); |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 94 | err = pthread_mutex_lock(&mutex->lock); |
| 95 | if (err) |
| 96 | error_exit(err, __func__); |
Peter Xu | f1aff7a | 2018-04-25 10:54:57 +0800 | [diff] [blame] | 97 | qemu_mutex_post_lock(mutex, file, line); |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Alex Bennée | 6c27a0d | 2018-01-11 11:27:16 +0300 | [diff] [blame] | 100 | int qemu_mutex_trylock_impl(QemuMutex *mutex, const char *file, const int line) |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 101 | { |
Jose Ricardo Ziviani | 31f5a72 | 2017-04-24 14:19:58 -0300 | [diff] [blame] | 102 | int err; |
| 103 | |
Fam Zheng | c096358 | 2017-07-04 20:23:25 +0800 | [diff] [blame] | 104 | assert(mutex->initialized); |
Jose Ricardo Ziviani | 31f5a72 | 2017-04-24 14:19:58 -0300 | [diff] [blame] | 105 | err = pthread_mutex_trylock(&mutex->lock); |
| 106 | if (err == 0) { |
Peter Xu | f1aff7a | 2018-04-25 10:54:57 +0800 | [diff] [blame] | 107 | qemu_mutex_post_lock(mutex, file, line); |
Jose Ricardo Ziviani | 31f5a72 | 2017-04-24 14:19:58 -0300 | [diff] [blame] | 108 | return 0; |
| 109 | } |
| 110 | if (err != EBUSY) { |
| 111 | error_exit(err, __func__); |
| 112 | } |
| 113 | return -EBUSY; |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Alex Bennée | 6c27a0d | 2018-01-11 11:27:16 +0300 | [diff] [blame] | 116 | void qemu_mutex_unlock_impl(QemuMutex *mutex, const char *file, const int line) |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 117 | { |
| 118 | int err; |
| 119 | |
Fam Zheng | c096358 | 2017-07-04 20:23:25 +0800 | [diff] [blame] | 120 | assert(mutex->initialized); |
Peter Xu | f1aff7a | 2018-04-25 10:54:57 +0800 | [diff] [blame] | 121 | qemu_mutex_pre_unlock(mutex, file, line); |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 122 | err = pthread_mutex_unlock(&mutex->lock); |
| 123 | if (err) |
| 124 | error_exit(err, __func__); |
| 125 | } |
| 126 | |
Paolo Bonzini | feadec6 | 2016-10-27 12:49:07 +0200 | [diff] [blame] | 127 | void qemu_rec_mutex_init(QemuRecMutex *mutex) |
| 128 | { |
| 129 | int err; |
| 130 | pthread_mutexattr_t attr; |
| 131 | |
| 132 | pthread_mutexattr_init(&attr); |
| 133 | pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); |
Richard Henderson | 6c98635 | 2021-06-14 16:31:40 -0700 | [diff] [blame] | 134 | err = pthread_mutex_init(&mutex->m.lock, &attr); |
Paolo Bonzini | feadec6 | 2016-10-27 12:49:07 +0200 | [diff] [blame] | 135 | pthread_mutexattr_destroy(&attr); |
| 136 | if (err) { |
| 137 | error_exit(err, __func__); |
| 138 | } |
Richard Henderson | 6c98635 | 2021-06-14 16:31:40 -0700 | [diff] [blame] | 139 | mutex->m.initialized = true; |
Paolo Bonzini | feadec6 | 2016-10-27 12:49:07 +0200 | [diff] [blame] | 140 | } |
| 141 | |
Richard Henderson | 4b193bb | 2021-06-14 16:31:38 -0700 | [diff] [blame] | 142 | void qemu_rec_mutex_destroy(QemuRecMutex *mutex) |
| 143 | { |
Richard Henderson | 6c98635 | 2021-06-14 16:31:40 -0700 | [diff] [blame] | 144 | qemu_mutex_destroy(&mutex->m); |
Richard Henderson | 4b193bb | 2021-06-14 16:31:38 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void qemu_rec_mutex_lock_impl(QemuRecMutex *mutex, const char *file, int line) |
| 148 | { |
Richard Henderson | 6c98635 | 2021-06-14 16:31:40 -0700 | [diff] [blame] | 149 | qemu_mutex_lock_impl(&mutex->m, file, line); |
Richard Henderson | 4b193bb | 2021-06-14 16:31:38 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | int qemu_rec_mutex_trylock_impl(QemuRecMutex *mutex, const char *file, int line) |
| 153 | { |
Richard Henderson | 6c98635 | 2021-06-14 16:31:40 -0700 | [diff] [blame] | 154 | return qemu_mutex_trylock_impl(&mutex->m, file, line); |
Richard Henderson | 4b193bb | 2021-06-14 16:31:38 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Richard Henderson | 9c75bae | 2021-06-14 16:31:39 -0700 | [diff] [blame] | 157 | void qemu_rec_mutex_unlock_impl(QemuRecMutex *mutex, const char *file, int line) |
Richard Henderson | 4b193bb | 2021-06-14 16:31:38 -0700 | [diff] [blame] | 158 | { |
Richard Henderson | 6c98635 | 2021-06-14 16:31:40 -0700 | [diff] [blame] | 159 | qemu_mutex_unlock_impl(&mutex->m, file, line); |
Richard Henderson | 4b193bb | 2021-06-14 16:31:38 -0700 | [diff] [blame] | 160 | } |
| 161 | |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 162 | void qemu_cond_init(QemuCond *cond) |
| 163 | { |
Longpeng(Mike) | 657ac98 | 2022-02-22 17:05:05 +0800 | [diff] [blame] | 164 | pthread_condattr_t attr; |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 165 | int err; |
| 166 | |
Longpeng(Mike) | 657ac98 | 2022-02-22 17:05:05 +0800 | [diff] [blame] | 167 | err = pthread_condattr_init(&attr); |
| 168 | if (err) { |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 169 | error_exit(err, __func__); |
Longpeng(Mike) | 657ac98 | 2022-02-22 17:05:05 +0800 | [diff] [blame] | 170 | } |
| 171 | #ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK |
| 172 | err = pthread_condattr_setclock(&attr, qemu_timedwait_clockid()); |
| 173 | if (err) { |
| 174 | error_exit(err, __func__); |
| 175 | } |
| 176 | #endif |
| 177 | err = pthread_cond_init(&cond->cond, &attr); |
| 178 | if (err) { |
| 179 | error_exit(err, __func__); |
| 180 | } |
| 181 | err = pthread_condattr_destroy(&attr); |
| 182 | if (err) { |
| 183 | error_exit(err, __func__); |
| 184 | } |
Fam Zheng | c096358 | 2017-07-04 20:23:25 +0800 | [diff] [blame] | 185 | cond->initialized = true; |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Corentin Chary | 313b1d6 | 2010-07-07 20:58:01 +0200 | [diff] [blame] | 188 | void qemu_cond_destroy(QemuCond *cond) |
| 189 | { |
| 190 | int err; |
| 191 | |
Fam Zheng | c096358 | 2017-07-04 20:23:25 +0800 | [diff] [blame] | 192 | assert(cond->initialized); |
| 193 | cond->initialized = false; |
Corentin Chary | 313b1d6 | 2010-07-07 20:58:01 +0200 | [diff] [blame] | 194 | err = pthread_cond_destroy(&cond->cond); |
| 195 | if (err) |
| 196 | error_exit(err, __func__); |
| 197 | } |
| 198 | |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 199 | void qemu_cond_signal(QemuCond *cond) |
| 200 | { |
| 201 | int err; |
| 202 | |
Fam Zheng | c096358 | 2017-07-04 20:23:25 +0800 | [diff] [blame] | 203 | assert(cond->initialized); |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 204 | err = pthread_cond_signal(&cond->cond); |
| 205 | if (err) |
| 206 | error_exit(err, __func__); |
| 207 | } |
| 208 | |
| 209 | void qemu_cond_broadcast(QemuCond *cond) |
| 210 | { |
| 211 | int err; |
| 212 | |
Fam Zheng | c096358 | 2017-07-04 20:23:25 +0800 | [diff] [blame] | 213 | assert(cond->initialized); |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 214 | err = pthread_cond_broadcast(&cond->cond); |
| 215 | if (err) |
| 216 | error_exit(err, __func__); |
| 217 | } |
| 218 | |
Alex Bennée | 6c27a0d | 2018-01-11 11:27:16 +0300 | [diff] [blame] | 219 | void qemu_cond_wait_impl(QemuCond *cond, QemuMutex *mutex, const char *file, const int line) |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 220 | { |
| 221 | int err; |
| 222 | |
Fam Zheng | c096358 | 2017-07-04 20:23:25 +0800 | [diff] [blame] | 223 | assert(cond->initialized); |
Peter Xu | f1aff7a | 2018-04-25 10:54:57 +0800 | [diff] [blame] | 224 | qemu_mutex_pre_unlock(mutex, file, line); |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 225 | err = pthread_cond_wait(&cond->cond, &mutex->lock); |
Peter Xu | f1aff7a | 2018-04-25 10:54:57 +0800 | [diff] [blame] | 226 | qemu_mutex_post_lock(mutex, file, line); |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 227 | if (err) |
| 228 | error_exit(err, __func__); |
| 229 | } |
| 230 | |
Emanuele Giuseppe Esposito | deb9c2a | 2023-01-17 08:52:01 -0500 | [diff] [blame] | 231 | static bool TSA_NO_TSA |
Longpeng(Mike) | a0d45db | 2022-02-22 17:05:06 +0800 | [diff] [blame] | 232 | qemu_cond_timedwait_ts(QemuCond *cond, QemuMutex *mutex, struct timespec *ts, |
| 233 | const char *file, const int line) |
Yury Kotov | 3dcc9c6 | 2019-09-09 16:13:33 +0300 | [diff] [blame] | 234 | { |
| 235 | int err; |
Yury Kotov | 3dcc9c6 | 2019-09-09 16:13:33 +0300 | [diff] [blame] | 236 | |
| 237 | assert(cond->initialized); |
| 238 | trace_qemu_mutex_unlock(mutex, file, line); |
Longpeng(Mike) | a0d45db | 2022-02-22 17:05:06 +0800 | [diff] [blame] | 239 | err = pthread_cond_timedwait(&cond->cond, &mutex->lock, ts); |
Yury Kotov | 3dcc9c6 | 2019-09-09 16:13:33 +0300 | [diff] [blame] | 240 | trace_qemu_mutex_locked(mutex, file, line); |
| 241 | if (err && err != ETIMEDOUT) { |
| 242 | error_exit(err, __func__); |
| 243 | } |
| 244 | return err != ETIMEDOUT; |
| 245 | } |
| 246 | |
Longpeng(Mike) | a0d45db | 2022-02-22 17:05:06 +0800 | [diff] [blame] | 247 | bool qemu_cond_timedwait_impl(QemuCond *cond, QemuMutex *mutex, int ms, |
| 248 | const char *file, const int line) |
| 249 | { |
| 250 | struct timespec ts; |
| 251 | |
| 252 | compute_abs_deadline(&ts, ms); |
| 253 | return qemu_cond_timedwait_ts(cond, mutex, &ts, file, line); |
| 254 | } |
| 255 | |
Paolo Bonzini | 38b14db | 2011-08-08 14:36:41 +0200 | [diff] [blame] | 256 | void qemu_sem_init(QemuSemaphore *sem, int init) |
| 257 | { |
Longpeng(Mike) | a0d45db | 2022-02-22 17:05:06 +0800 | [diff] [blame] | 258 | qemu_mutex_init(&sem->mutex); |
| 259 | qemu_cond_init(&sem->cond); |
Paolo Bonzini | 38b14db | 2011-08-08 14:36:41 +0200 | [diff] [blame] | 260 | |
Paolo Bonzini | c166cb7 | 2012-11-02 15:43:21 +0100 | [diff] [blame] | 261 | if (init < 0) { |
| 262 | error_exit(EINVAL, __func__); |
| 263 | } |
| 264 | sem->count = init; |
Paolo Bonzini | 38b14db | 2011-08-08 14:36:41 +0200 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | void qemu_sem_destroy(QemuSemaphore *sem) |
| 268 | { |
Longpeng(Mike) | a0d45db | 2022-02-22 17:05:06 +0800 | [diff] [blame] | 269 | qemu_cond_destroy(&sem->cond); |
| 270 | qemu_mutex_destroy(&sem->mutex); |
Paolo Bonzini | 38b14db | 2011-08-08 14:36:41 +0200 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | void qemu_sem_post(QemuSemaphore *sem) |
| 274 | { |
Longpeng(Mike) | a0d45db | 2022-02-22 17:05:06 +0800 | [diff] [blame] | 275 | qemu_mutex_lock(&sem->mutex); |
Izumi Tsutsui | 79761c6 | 2013-07-03 17:58:14 +0900 | [diff] [blame] | 276 | if (sem->count == UINT_MAX) { |
Longpeng(Mike) | a0d45db | 2022-02-22 17:05:06 +0800 | [diff] [blame] | 277 | error_exit(EINVAL, __func__); |
Paolo Bonzini | c166cb7 | 2012-11-02 15:43:21 +0100 | [diff] [blame] | 278 | } else { |
Izumi Tsutsui | 79761c6 | 2013-07-03 17:58:14 +0900 | [diff] [blame] | 279 | sem->count++; |
Longpeng(Mike) | a0d45db | 2022-02-22 17:05:06 +0800 | [diff] [blame] | 280 | qemu_cond_signal(&sem->cond); |
Paolo Bonzini | c166cb7 | 2012-11-02 15:43:21 +0100 | [diff] [blame] | 281 | } |
Longpeng(Mike) | a0d45db | 2022-02-22 17:05:06 +0800 | [diff] [blame] | 282 | qemu_mutex_unlock(&sem->mutex); |
Paolo Bonzini | c166cb7 | 2012-11-02 15:43:21 +0100 | [diff] [blame] | 283 | } |
| 284 | |
Paolo Bonzini | 38b14db | 2011-08-08 14:36:41 +0200 | [diff] [blame] | 285 | int qemu_sem_timedwait(QemuSemaphore *sem, int ms) |
| 286 | { |
Longpeng(Mike) | a0d45db | 2022-02-22 17:05:06 +0800 | [diff] [blame] | 287 | bool rc = true; |
Paolo Bonzini | c166cb7 | 2012-11-02 15:43:21 +0100 | [diff] [blame] | 288 | struct timespec ts; |
Paolo Bonzini | 38b14db | 2011-08-08 14:36:41 +0200 | [diff] [blame] | 289 | |
Paolo Bonzini | c166cb7 | 2012-11-02 15:43:21 +0100 | [diff] [blame] | 290 | compute_abs_deadline(&ts, ms); |
Longpeng(Mike) | a0d45db | 2022-02-22 17:05:06 +0800 | [diff] [blame] | 291 | qemu_mutex_lock(&sem->mutex); |
Izumi Tsutsui | 79761c6 | 2013-07-03 17:58:14 +0900 | [diff] [blame] | 292 | while (sem->count == 0) { |
Paolo Bonzini | 8ab3026 | 2022-02-21 12:46:32 +0100 | [diff] [blame] | 293 | if (ms == 0) { |
| 294 | rc = false; |
| 295 | } else { |
| 296 | rc = qemu_cond_timedwait_ts(&sem->cond, &sem->mutex, &ts, |
| 297 | __FILE__, __LINE__); |
| 298 | } |
Longpeng(Mike) | a0d45db | 2022-02-22 17:05:06 +0800 | [diff] [blame] | 299 | if (!rc) { /* timeout */ |
Paolo Bonzini | c166cb7 | 2012-11-02 15:43:21 +0100 | [diff] [blame] | 300 | break; |
| 301 | } |
Paolo Bonzini | c166cb7 | 2012-11-02 15:43:21 +0100 | [diff] [blame] | 302 | } |
Longpeng(Mike) | a0d45db | 2022-02-22 17:05:06 +0800 | [diff] [blame] | 303 | if (rc) { |
Izumi Tsutsui | 79761c6 | 2013-07-03 17:58:14 +0900 | [diff] [blame] | 304 | --sem->count; |
| 305 | } |
Longpeng(Mike) | a0d45db | 2022-02-22 17:05:06 +0800 | [diff] [blame] | 306 | qemu_mutex_unlock(&sem->mutex); |
| 307 | return (rc ? 0 : -1); |
Paolo Bonzini | 38b14db | 2011-08-08 14:36:41 +0200 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | void qemu_sem_wait(QemuSemaphore *sem) |
| 311 | { |
Longpeng(Mike) | a0d45db | 2022-02-22 17:05:06 +0800 | [diff] [blame] | 312 | qemu_mutex_lock(&sem->mutex); |
Izumi Tsutsui | 79761c6 | 2013-07-03 17:58:14 +0900 | [diff] [blame] | 313 | while (sem->count == 0) { |
Longpeng(Mike) | a0d45db | 2022-02-22 17:05:06 +0800 | [diff] [blame] | 314 | qemu_cond_wait(&sem->cond, &sem->mutex); |
Izumi Tsutsui | 79761c6 | 2013-07-03 17:58:14 +0900 | [diff] [blame] | 315 | } |
| 316 | --sem->count; |
Longpeng(Mike) | a0d45db | 2022-02-22 17:05:06 +0800 | [diff] [blame] | 317 | qemu_mutex_unlock(&sem->mutex); |
Paolo Bonzini | 38b14db | 2011-08-08 14:36:41 +0200 | [diff] [blame] | 318 | } |
| 319 | |
Paolo Bonzini | c7c4d06 | 2013-09-25 14:20:59 +0800 | [diff] [blame] | 320 | #ifdef __linux__ |
Paolo Bonzini | fbcc3e5 | 2017-01-12 19:07:54 +0100 | [diff] [blame] | 321 | #include "qemu/futex.h" |
Paolo Bonzini | c7c4d06 | 2013-09-25 14:20:59 +0800 | [diff] [blame] | 322 | #else |
Paolo Bonzini | fbcc3e5 | 2017-01-12 19:07:54 +0100 | [diff] [blame] | 323 | static inline void qemu_futex_wake(QemuEvent *ev, int n) |
Paolo Bonzini | c7c4d06 | 2013-09-25 14:20:59 +0800 | [diff] [blame] | 324 | { |
Fam Zheng | c096358 | 2017-07-04 20:23:25 +0800 | [diff] [blame] | 325 | assert(ev->initialized); |
Paolo Bonzini | 158ef8c | 2015-02-02 16:36:51 +0100 | [diff] [blame] | 326 | pthread_mutex_lock(&ev->lock); |
Paolo Bonzini | c7c4d06 | 2013-09-25 14:20:59 +0800 | [diff] [blame] | 327 | if (n == 1) { |
| 328 | pthread_cond_signal(&ev->cond); |
| 329 | } else { |
| 330 | pthread_cond_broadcast(&ev->cond); |
| 331 | } |
Paolo Bonzini | 158ef8c | 2015-02-02 16:36:51 +0100 | [diff] [blame] | 332 | pthread_mutex_unlock(&ev->lock); |
Paolo Bonzini | c7c4d06 | 2013-09-25 14:20:59 +0800 | [diff] [blame] | 333 | } |
| 334 | |
Paolo Bonzini | fbcc3e5 | 2017-01-12 19:07:54 +0100 | [diff] [blame] | 335 | static inline void qemu_futex_wait(QemuEvent *ev, unsigned val) |
Paolo Bonzini | c7c4d06 | 2013-09-25 14:20:59 +0800 | [diff] [blame] | 336 | { |
Fam Zheng | c096358 | 2017-07-04 20:23:25 +0800 | [diff] [blame] | 337 | assert(ev->initialized); |
Paolo Bonzini | c7c4d06 | 2013-09-25 14:20:59 +0800 | [diff] [blame] | 338 | pthread_mutex_lock(&ev->lock); |
| 339 | if (ev->value == val) { |
| 340 | pthread_cond_wait(&ev->cond, &ev->lock); |
| 341 | } |
| 342 | pthread_mutex_unlock(&ev->lock); |
| 343 | } |
| 344 | #endif |
| 345 | |
| 346 | /* Valid transitions: |
| 347 | * - free->set, when setting the event |
Paolo Bonzini | fbcc3e5 | 2017-01-12 19:07:54 +0100 | [diff] [blame] | 348 | * - busy->set, when setting the event, followed by qemu_futex_wake |
Paolo Bonzini | c7c4d06 | 2013-09-25 14:20:59 +0800 | [diff] [blame] | 349 | * - set->free, when resetting the event |
| 350 | * - free->busy, when waiting |
| 351 | * |
| 352 | * set->busy does not happen (it can be observed from the outside but |
| 353 | * it really is set->free->busy). |
| 354 | * |
| 355 | * busy->free provably cannot happen; to enforce it, the set->free transition |
| 356 | * is done with an OR, which becomes a no-op if the event has concurrently |
| 357 | * transitioned to free or busy. |
| 358 | */ |
| 359 | |
| 360 | #define EV_SET 0 |
| 361 | #define EV_FREE 1 |
| 362 | #define EV_BUSY -1 |
| 363 | |
| 364 | void qemu_event_init(QemuEvent *ev, bool init) |
| 365 | { |
| 366 | #ifndef __linux__ |
| 367 | pthread_mutex_init(&ev->lock, NULL); |
| 368 | pthread_cond_init(&ev->cond, NULL); |
| 369 | #endif |
| 370 | |
| 371 | ev->value = (init ? EV_SET : EV_FREE); |
Fam Zheng | c096358 | 2017-07-04 20:23:25 +0800 | [diff] [blame] | 372 | ev->initialized = true; |
Paolo Bonzini | c7c4d06 | 2013-09-25 14:20:59 +0800 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | void qemu_event_destroy(QemuEvent *ev) |
| 376 | { |
Fam Zheng | c096358 | 2017-07-04 20:23:25 +0800 | [diff] [blame] | 377 | assert(ev->initialized); |
| 378 | ev->initialized = false; |
Paolo Bonzini | c7c4d06 | 2013-09-25 14:20:59 +0800 | [diff] [blame] | 379 | #ifndef __linux__ |
| 380 | pthread_mutex_destroy(&ev->lock); |
| 381 | pthread_cond_destroy(&ev->cond); |
| 382 | #endif |
| 383 | } |
| 384 | |
| 385 | void qemu_event_set(QemuEvent *ev) |
| 386 | { |
Paolo Bonzini | 9586a13 | 2023-03-02 11:19:52 +0100 | [diff] [blame] | 387 | assert(ev->initialized); |
| 388 | |
| 389 | /* |
| 390 | * Pairs with both qemu_event_reset() and qemu_event_wait(). |
| 391 | * |
| 392 | * qemu_event_set has release semantics, but because it *loads* |
Paolo Bonzini | 374293c | 2016-09-19 11:10:57 +0200 | [diff] [blame] | 393 | * ev->value we need a full memory barrier here. |
| 394 | */ |
| 395 | smp_mb(); |
Stefan Hajnoczi | d73415a | 2020-09-23 11:56:46 +0100 | [diff] [blame] | 396 | if (qatomic_read(&ev->value) != EV_SET) { |
Paolo Bonzini | 9586a13 | 2023-03-02 11:19:52 +0100 | [diff] [blame] | 397 | int old = qatomic_xchg(&ev->value, EV_SET); |
| 398 | |
| 399 | /* Pairs with memory barrier in kernel futex_wait system call. */ |
| 400 | smp_mb__after_rmw(); |
| 401 | if (old == EV_BUSY) { |
Paolo Bonzini | c7c4d06 | 2013-09-25 14:20:59 +0800 | [diff] [blame] | 402 | /* There were waiters, wake them up. */ |
Paolo Bonzini | fbcc3e5 | 2017-01-12 19:07:54 +0100 | [diff] [blame] | 403 | qemu_futex_wake(ev, INT_MAX); |
Paolo Bonzini | c7c4d06 | 2013-09-25 14:20:59 +0800 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | void qemu_event_reset(QemuEvent *ev) |
| 409 | { |
Fam Zheng | c096358 | 2017-07-04 20:23:25 +0800 | [diff] [blame] | 410 | assert(ev->initialized); |
Paolo Bonzini | 9586a13 | 2023-03-02 11:19:52 +0100 | [diff] [blame] | 411 | |
| 412 | /* |
| 413 | * If there was a concurrent reset (or even reset+wait), |
| 414 | * do nothing. Otherwise change EV_SET->EV_FREE. |
| 415 | */ |
| 416 | qatomic_or(&ev->value, EV_FREE); |
| 417 | |
| 418 | /* |
| 419 | * Order reset before checking the condition in the caller. |
| 420 | * Pairs with the first memory barrier in qemu_event_set(). |
| 421 | */ |
| 422 | smp_mb__after_rmw(); |
Paolo Bonzini | c7c4d06 | 2013-09-25 14:20:59 +0800 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | void qemu_event_wait(QemuEvent *ev) |
| 426 | { |
| 427 | unsigned value; |
| 428 | |
Fam Zheng | c096358 | 2017-07-04 20:23:25 +0800 | [diff] [blame] | 429 | assert(ev->initialized); |
Paolo Bonzini | 9586a13 | 2023-03-02 11:19:52 +0100 | [diff] [blame] | 430 | |
| 431 | /* |
| 432 | * qemu_event_wait must synchronize with qemu_event_set even if it does |
| 433 | * not go down the slow path, so this load-acquire is needed that |
| 434 | * synchronizes with the first memory barrier in qemu_event_set(). |
| 435 | * |
| 436 | * If we do go down the slow path, there is no requirement at all: we |
| 437 | * might miss a qemu_event_set() here but ultimately the memory barrier in |
| 438 | * qemu_futex_wait() will ensure the check is done correctly. |
| 439 | */ |
| 440 | value = qatomic_load_acquire(&ev->value); |
Paolo Bonzini | c7c4d06 | 2013-09-25 14:20:59 +0800 | [diff] [blame] | 441 | if (value != EV_SET) { |
| 442 | if (value == EV_FREE) { |
| 443 | /* |
Paolo Bonzini | 9586a13 | 2023-03-02 11:19:52 +0100 | [diff] [blame] | 444 | * Leave the event reset and tell qemu_event_set that there are |
| 445 | * waiters. No need to retry, because there cannot be a concurrent |
| 446 | * busy->free transition. After the CAS, the event will be either |
| 447 | * set or busy. |
| 448 | * |
| 449 | * This cmpxchg doesn't have particular ordering requirements if it |
| 450 | * succeeds (moving the store earlier can only cause qemu_event_set() |
| 451 | * to issue _more_ wakeups), the failing case needs acquire semantics |
| 452 | * like the load above. |
Paolo Bonzini | c7c4d06 | 2013-09-25 14:20:59 +0800 | [diff] [blame] | 453 | */ |
Stefan Hajnoczi | d73415a | 2020-09-23 11:56:46 +0100 | [diff] [blame] | 454 | if (qatomic_cmpxchg(&ev->value, EV_FREE, EV_BUSY) == EV_SET) { |
Paolo Bonzini | c7c4d06 | 2013-09-25 14:20:59 +0800 | [diff] [blame] | 455 | return; |
| 456 | } |
| 457 | } |
Paolo Bonzini | 9586a13 | 2023-03-02 11:19:52 +0100 | [diff] [blame] | 458 | |
| 459 | /* |
| 460 | * This is the final check for a concurrent set, so it does need |
| 461 | * a smp_mb() pairing with the second barrier of qemu_event_set(). |
| 462 | * The barrier is inside the FUTEX_WAIT system call. |
| 463 | */ |
Paolo Bonzini | fbcc3e5 | 2017-01-12 19:07:54 +0100 | [diff] [blame] | 464 | qemu_futex_wait(ev, EV_BUSY); |
Paolo Bonzini | c7c4d06 | 2013-09-25 14:20:59 +0800 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
Peter Maydell | a458774 | 2018-11-05 13:55:38 +0000 | [diff] [blame] | 468 | static __thread NotifierList thread_exit; |
Paolo Bonzini | ef57137 | 2014-12-02 12:05:45 +0100 | [diff] [blame] | 469 | |
Peter Maydell | a458774 | 2018-11-05 13:55:38 +0000 | [diff] [blame] | 470 | /* |
| 471 | * Note that in this implementation you can register a thread-exit |
| 472 | * notifier for the main thread, but it will never be called. |
| 473 | * This is OK because main thread exit can only happen when the |
| 474 | * entire process is exiting, and the API allows notifiers to not |
| 475 | * be called on process exit. |
| 476 | */ |
Paolo Bonzini | ef57137 | 2014-12-02 12:05:45 +0100 | [diff] [blame] | 477 | void qemu_thread_atexit_add(Notifier *notifier) |
| 478 | { |
Peter Maydell | a458774 | 2018-11-05 13:55:38 +0000 | [diff] [blame] | 479 | notifier_list_add(&thread_exit, notifier); |
Paolo Bonzini | ef57137 | 2014-12-02 12:05:45 +0100 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | void qemu_thread_atexit_remove(Notifier *notifier) |
| 483 | { |
Paolo Bonzini | ef57137 | 2014-12-02 12:05:45 +0100 | [diff] [blame] | 484 | notifier_remove(notifier); |
Paolo Bonzini | ef57137 | 2014-12-02 12:05:45 +0100 | [diff] [blame] | 485 | } |
| 486 | |
Peter Maydell | a458774 | 2018-11-05 13:55:38 +0000 | [diff] [blame] | 487 | static void qemu_thread_atexit_notify(void *arg) |
Paolo Bonzini | ef57137 | 2014-12-02 12:05:45 +0100 | [diff] [blame] | 488 | { |
Peter Maydell | a458774 | 2018-11-05 13:55:38 +0000 | [diff] [blame] | 489 | /* |
| 490 | * Called when non-main thread exits (via qemu_thread_exit() |
| 491 | * or by returning from its start routine.) |
| 492 | */ |
| 493 | notifier_list_notify(&thread_exit, NULL); |
Paolo Bonzini | ef57137 | 2014-12-02 12:05:45 +0100 | [diff] [blame] | 494 | } |
| 495 | |
linzhecheng | 68a9398 | 2017-11-28 12:46:56 +0800 | [diff] [blame] | 496 | typedef struct { |
| 497 | void *(*start_routine)(void *); |
| 498 | void *arg; |
| 499 | char *name; |
| 500 | } QemuThreadArgs; |
| 501 | |
| 502 | static void *qemu_thread_start(void *args) |
| 503 | { |
| 504 | QemuThreadArgs *qemu_thread_args = args; |
| 505 | void *(*start_routine)(void *) = qemu_thread_args->start_routine; |
| 506 | void *arg = qemu_thread_args->arg; |
Peter Maydell | a458774 | 2018-11-05 13:55:38 +0000 | [diff] [blame] | 507 | void *r; |
linzhecheng | 68a9398 | 2017-11-28 12:46:56 +0800 | [diff] [blame] | 508 | |
| 509 | /* Attempt to set the threads name; note that this is for debug, so |
| 510 | * we're not going to fail if we can't set it. |
| 511 | */ |
Peter Xu | d820fa5 | 2018-04-12 13:34:44 +0800 | [diff] [blame] | 512 | if (name_threads && qemu_thread_args->name) { |
Roman Bolshakov | 479a574 | 2018-12-17 23:26:01 +0300 | [diff] [blame] | 513 | # if defined(CONFIG_PTHREAD_SETNAME_NP_W_TID) |
Peter Xu | d820fa5 | 2018-04-12 13:34:44 +0800 | [diff] [blame] | 514 | pthread_setname_np(pthread_self(), qemu_thread_args->name); |
Roman Bolshakov | 479a574 | 2018-12-17 23:26:01 +0300 | [diff] [blame] | 515 | # elif defined(CONFIG_PTHREAD_SETNAME_NP_WO_TID) |
| 516 | pthread_setname_np(qemu_thread_args->name); |
Brad Smith | 3ada67a | 2022-12-18 03:22:04 -0500 | [diff] [blame] | 517 | # elif defined(CONFIG_PTHREAD_SET_NAME_NP) |
| 518 | pthread_set_name_np(pthread_self(), qemu_thread_args->name); |
Roman Bolshakov | 479a574 | 2018-12-17 23:26:01 +0300 | [diff] [blame] | 519 | # endif |
Peter Xu | d820fa5 | 2018-04-12 13:34:44 +0800 | [diff] [blame] | 520 | } |
Robert Foley | ce9f0e5 | 2020-06-12 20:02:33 +0100 | [diff] [blame] | 521 | QEMU_TSAN_ANNOTATE_THREAD_NAME(qemu_thread_args->name); |
linzhecheng | 68a9398 | 2017-11-28 12:46:56 +0800 | [diff] [blame] | 522 | g_free(qemu_thread_args->name); |
| 523 | g_free(qemu_thread_args); |
Richard Henderson | 37daf1b | 2021-08-03 11:19:07 -1000 | [diff] [blame] | 524 | |
| 525 | /* |
| 526 | * GCC 11 with glibc 2.17 on PowerPC reports |
| 527 | * |
| 528 | * qemu-thread-posix.c:540:5: error: ‘__sigsetjmp’ accessing 656 bytes |
| 529 | * in a region of size 528 [-Werror=stringop-overflow=] |
| 530 | * 540 | pthread_cleanup_push(qemu_thread_atexit_notify, NULL); |
| 531 | * | ^~~~~~~~~~~~~~~~~~~~ |
| 532 | * |
| 533 | * which is clearly nonsense. |
| 534 | */ |
| 535 | #pragma GCC diagnostic push |
| 536 | #ifndef __clang__ |
| 537 | #pragma GCC diagnostic ignored "-Wstringop-overflow" |
| 538 | #endif |
| 539 | |
Peter Maydell | a458774 | 2018-11-05 13:55:38 +0000 | [diff] [blame] | 540 | pthread_cleanup_push(qemu_thread_atexit_notify, NULL); |
| 541 | r = start_routine(arg); |
| 542 | pthread_cleanup_pop(1); |
Richard Henderson | 37daf1b | 2021-08-03 11:19:07 -1000 | [diff] [blame] | 543 | |
| 544 | #pragma GCC diagnostic pop |
| 545 | |
Peter Maydell | a458774 | 2018-11-05 13:55:38 +0000 | [diff] [blame] | 546 | return r; |
Dr. David Alan Gilbert | 5c31207 | 2014-03-12 11:48:18 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Dr. David Alan Gilbert | 4900116 | 2014-01-30 10:20:32 +0000 | [diff] [blame] | 549 | void qemu_thread_create(QemuThread *thread, const char *name, |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 550 | void *(*start_routine)(void*), |
Jan Kiszka | cf21871 | 2011-12-12 17:21:31 +0100 | [diff] [blame] | 551 | void *arg, int mode) |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 552 | { |
Jan Kiszka | cf21871 | 2011-12-12 17:21:31 +0100 | [diff] [blame] | 553 | sigset_t set, oldset; |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 554 | int err; |
Jan Kiszka | 8763046 | 2011-12-12 17:21:32 +0100 | [diff] [blame] | 555 | pthread_attr_t attr; |
Peter Xu | d820fa5 | 2018-04-12 13:34:44 +0800 | [diff] [blame] | 556 | QemuThreadArgs *qemu_thread_args; |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 557 | |
Jan Kiszka | 8763046 | 2011-12-12 17:21:32 +0100 | [diff] [blame] | 558 | err = pthread_attr_init(&attr); |
| 559 | if (err) { |
| 560 | error_exit(err, __func__); |
| 561 | } |
Paolo Bonzini | 55541c8 | 2010-06-03 15:20:32 +0200 | [diff] [blame] | 562 | |
linzhecheng | 68a9398 | 2017-11-28 12:46:56 +0800 | [diff] [blame] | 563 | if (mode == QEMU_THREAD_DETACHED) { |
| 564 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 565 | } |
| 566 | |
Jan Kiszka | cf21871 | 2011-12-12 17:21:31 +0100 | [diff] [blame] | 567 | /* Leave signal handling to the iothread. */ |
Paolo Bonzini | 55541c8 | 2010-06-03 15:20:32 +0200 | [diff] [blame] | 568 | sigfillset(&set); |
Roman Bolshakov | 21a43af | 2018-12-17 23:26:02 +0300 | [diff] [blame] | 569 | /* Blocking the signals can result in undefined behaviour. */ |
| 570 | sigdelset(&set, SIGSEGV); |
| 571 | sigdelset(&set, SIGFPE); |
| 572 | sigdelset(&set, SIGILL); |
| 573 | /* TODO avoid SIGBUS loss on macOS */ |
Paolo Bonzini | 55541c8 | 2010-06-03 15:20:32 +0200 | [diff] [blame] | 574 | pthread_sigmask(SIG_SETMASK, &set, &oldset); |
linzhecheng | 68a9398 | 2017-11-28 12:46:56 +0800 | [diff] [blame] | 575 | |
Peter Xu | d820fa5 | 2018-04-12 13:34:44 +0800 | [diff] [blame] | 576 | qemu_thread_args = g_new0(QemuThreadArgs, 1); |
| 577 | qemu_thread_args->name = g_strdup(name); |
| 578 | qemu_thread_args->start_routine = start_routine; |
| 579 | qemu_thread_args->arg = arg; |
linzhecheng | 68a9398 | 2017-11-28 12:46:56 +0800 | [diff] [blame] | 580 | |
Peter Xu | d820fa5 | 2018-04-12 13:34:44 +0800 | [diff] [blame] | 581 | err = pthread_create(&thread->thread, &attr, |
| 582 | qemu_thread_start, qemu_thread_args); |
linzhecheng | 68a9398 | 2017-11-28 12:46:56 +0800 | [diff] [blame] | 583 | |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 584 | if (err) |
| 585 | error_exit(err, __func__); |
Paolo Bonzini | 55541c8 | 2010-06-03 15:20:32 +0200 | [diff] [blame] | 586 | |
| 587 | pthread_sigmask(SIG_SETMASK, &oldset, NULL); |
Jan Kiszka | 8763046 | 2011-12-12 17:21:32 +0100 | [diff] [blame] | 588 | |
| 589 | pthread_attr_destroy(&attr); |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 590 | } |
| 591 | |
David Hildenbrand | 7730f32 | 2022-10-14 15:47:15 +0200 | [diff] [blame] | 592 | int qemu_thread_set_affinity(QemuThread *thread, unsigned long *host_cpus, |
| 593 | unsigned long nbits) |
| 594 | { |
| 595 | #if defined(CONFIG_PTHREAD_AFFINITY_NP) |
| 596 | const size_t setsize = CPU_ALLOC_SIZE(nbits); |
| 597 | unsigned long value; |
| 598 | cpu_set_t *cpuset; |
| 599 | int err; |
| 600 | |
| 601 | cpuset = CPU_ALLOC(nbits); |
| 602 | g_assert(cpuset); |
| 603 | |
| 604 | CPU_ZERO_S(setsize, cpuset); |
| 605 | value = find_first_bit(host_cpus, nbits); |
| 606 | while (value < nbits) { |
| 607 | CPU_SET_S(value, setsize, cpuset); |
| 608 | value = find_next_bit(host_cpus, nbits, value + 1); |
| 609 | } |
| 610 | |
| 611 | err = pthread_setaffinity_np(thread->thread, setsize, cpuset); |
| 612 | CPU_FREE(cpuset); |
| 613 | return err; |
| 614 | #else |
| 615 | return -ENOSYS; |
| 616 | #endif |
| 617 | } |
| 618 | |
| 619 | int qemu_thread_get_affinity(QemuThread *thread, unsigned long **host_cpus, |
| 620 | unsigned long *nbits) |
| 621 | { |
| 622 | #if defined(CONFIG_PTHREAD_AFFINITY_NP) |
| 623 | unsigned long tmpbits; |
| 624 | cpu_set_t *cpuset; |
| 625 | size_t setsize; |
| 626 | int i, err; |
| 627 | |
| 628 | tmpbits = CPU_SETSIZE; |
| 629 | while (true) { |
| 630 | setsize = CPU_ALLOC_SIZE(tmpbits); |
| 631 | cpuset = CPU_ALLOC(tmpbits); |
| 632 | g_assert(cpuset); |
| 633 | |
| 634 | err = pthread_getaffinity_np(thread->thread, setsize, cpuset); |
| 635 | if (err) { |
| 636 | CPU_FREE(cpuset); |
| 637 | if (err != -EINVAL) { |
| 638 | return err; |
| 639 | } |
| 640 | tmpbits *= 2; |
| 641 | } else { |
| 642 | break; |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | /* Convert the result into a proper bitmap. */ |
| 647 | *nbits = tmpbits; |
| 648 | *host_cpus = bitmap_new(tmpbits); |
| 649 | for (i = 0; i < tmpbits; i++) { |
| 650 | if (CPU_ISSET(i, cpuset)) { |
| 651 | set_bit(i, *host_cpus); |
| 652 | } |
| 653 | } |
| 654 | CPU_FREE(cpuset); |
| 655 | return 0; |
| 656 | #else |
| 657 | return -ENOSYS; |
| 658 | #endif |
| 659 | } |
| 660 | |
Jan Kiszka | b7680cb | 2011-03-12 17:43:51 +0100 | [diff] [blame] | 661 | void qemu_thread_get_self(QemuThread *thread) |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 662 | { |
| 663 | thread->thread = pthread_self(); |
| 664 | } |
| 665 | |
Andreas Färber | 2d797b6 | 2012-05-02 17:21:31 +0200 | [diff] [blame] | 666 | bool qemu_thread_is_self(QemuThread *thread) |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 667 | { |
Jan Kiszka | b7680cb | 2011-03-12 17:43:51 +0100 | [diff] [blame] | 668 | return pthread_equal(pthread_self(), thread->thread); |
aliguori | e5d355d | 2009-04-24 18:03:15 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Corentin Chary | 313b1d6 | 2010-07-07 20:58:01 +0200 | [diff] [blame] | 671 | void qemu_thread_exit(void *retval) |
| 672 | { |
| 673 | pthread_exit(retval); |
| 674 | } |
Jan Kiszka | 8763046 | 2011-12-12 17:21:32 +0100 | [diff] [blame] | 675 | |
| 676 | void *qemu_thread_join(QemuThread *thread) |
| 677 | { |
| 678 | int err; |
| 679 | void *ret; |
| 680 | |
| 681 | err = pthread_join(thread->thread, &ret); |
| 682 | if (err) { |
| 683 | error_exit(err, __func__); |
| 684 | } |
| 685 | return ret; |
| 686 | } |