[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 41/53] thread-posix: use monotonic clock for QemuCond and QemuSema
From: |
Paolo Bonzini |
Subject: |
[PULL 41/53] thread-posix: use monotonic clock for QemuCond and QemuSemaphore |
Date: |
Tue, 19 Apr 2022 07:50:57 +0200 |
From: "Longpeng(Mike)" <longpeng2@huawei.com>
Use CLOCK_MONOTONIC, so the timeout isn't affected by changes to
the system time. It depends on the pthread_condattr_setclock(),
while some systems(e.g. mac os) does not support it, so the behavior
won't change in these systems.
Signed-off-by: Longpeng(Mike) <longpeng2@huawei.com>
Message-Id: <20220222090507.2028-3-longpeng2@huawei.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
meson.build | 11 +++++++++
util/qemu-thread-posix.c | 53 ++++++++++++++++++++++++++++++++++------
2 files changed, 57 insertions(+), 7 deletions(-)
diff --git a/meson.build b/meson.build
index 526e9dc03b..6ba60950c8 100644
--- a/meson.build
+++ b/meson.build
@@ -1780,6 +1780,17 @@ config_host_data.set('CONFIG_PTHREAD_SETNAME_NP_WO_TID',
cc.links(gnu_source_pre
pthread_create(&thread, 0, f, 0);
return 0;
}''', dependencies: threads))
+config_host_data.set('CONFIG_PTHREAD_CONDATTR_SETCLOCK',
cc.links(gnu_source_prefix + '''
+ #include <pthread.h>
+ #include <time.h>
+
+ int main(void)
+ {
+ pthread_condattr_t attr
+ pthread_condattr_init(&attr);
+ pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
+ return 0;
+ }''', dependencies: threads))
config_host_data.set('CONFIG_SIGNALFD', cc.links(gnu_source_prefix + '''
#include <sys/signalfd.h>
diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c
index 1ad2503607..f6a34eed2f 100644
--- a/util/qemu-thread-posix.c
+++ b/util/qemu-thread-posix.c
@@ -38,12 +38,20 @@ static void error_exit(int err, const char *msg)
abort();
}
+static inline clockid_t qemu_timedwait_clockid(void)
+{
+#ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
+ return CLOCK_MONOTONIC;
+#else
+ return CLOCK_REALTIME;
+#endif
+}
+
static void compute_abs_deadline(struct timespec *ts, int ms)
{
- struct timeval tv;
- gettimeofday(&tv, NULL);
- ts->tv_nsec = tv.tv_usec * 1000 + (ms % 1000) * 1000000;
- ts->tv_sec = tv.tv_sec + ms / 1000;
+ clock_gettime(qemu_timedwait_clockid(), ts);
+ ts->tv_nsec += (ms % 1000) * 1000000;
+ ts->tv_sec += ms / 1000;
if (ts->tv_nsec >= 1000000000) {
ts->tv_sec++;
ts->tv_nsec -= 1000000000;
@@ -147,11 +155,27 @@ void qemu_rec_mutex_unlock_impl(QemuRecMutex *mutex,
const char *file, int line)
void qemu_cond_init(QemuCond *cond)
{
+ pthread_condattr_t attr;
int err;
- err = pthread_cond_init(&cond->cond, NULL);
- if (err)
+ err = pthread_condattr_init(&attr);
+ if (err) {
error_exit(err, __func__);
+ }
+#ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
+ err = pthread_condattr_setclock(&attr, qemu_timedwait_clockid());
+ if (err) {
+ error_exit(err, __func__);
+ }
+#endif
+ err = pthread_cond_init(&cond->cond, &attr);
+ if (err) {
+ error_exit(err, __func__);
+ }
+ err = pthread_condattr_destroy(&attr);
+ if (err) {
+ error_exit(err, __func__);
+ }
cond->initialized = true;
}
@@ -217,16 +241,31 @@ bool qemu_cond_timedwait_impl(QemuCond *cond, QemuMutex
*mutex, int ms,
void qemu_sem_init(QemuSemaphore *sem, int init)
{
+ pthread_condattr_t attr;
int rc;
rc = pthread_mutex_init(&sem->lock, NULL);
if (rc != 0) {
error_exit(rc, __func__);
}
- rc = pthread_cond_init(&sem->cond, NULL);
+ rc = pthread_condattr_init(&attr);
if (rc != 0) {
error_exit(rc, __func__);
}
+#ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
+ rc = pthread_condattr_setclock(&attr, qemu_timedwait_clockid());
+ if (rc != 0) {
+ error_exit(rc, __func__);
+ }
+#endif
+ rc = pthread_cond_init(&sem->cond, &attr);
+ if (rc != 0) {
+ error_exit(rc, __func__);
+ }
+ rc = pthread_condattr_destroy(&attr);
+ if (rc < 0) {
+ error_exit(rc, __func__);
+ }
if (init < 0) {
error_exit(EINVAL, __func__);
}
--
2.35.1
- [PULL 06/53] meson: use chardev_ss dependencies, (continued)
- [PULL 06/53] meson: use chardev_ss dependencies, Paolo Bonzini, 2022/04/19
- [PULL 07/53] meson: add util dependency for oslib-posix on freebsd, Paolo Bonzini, 2022/04/19
- [PULL 16/53] Replace qemu_real_host_page variables with inlined functions, Paolo Bonzini, 2022/04/19
- [PULL 14/53] include/qapi: add g_autoptr support for qobject types, Paolo Bonzini, 2022/04/19
- [PULL 19/53] util: rename qemu-error.c to match its header name, Paolo Bonzini, 2022/04/19
- [PULL 21/53] include: move TFR to osdep.h, Paolo Bonzini, 2022/04/19
- [PULL 24/53] include: move coroutine IO functions to coroutine.h, Paolo Bonzini, 2022/04/19
- [PULL 27/53] include: move cpu_exec* declarations to cpu-common.h, Paolo Bonzini, 2022/04/19
- [PULL 37/53] build-sys: drop ntddscsi.h check, Paolo Bonzini, 2022/04/19
- [PULL 38/53] build-sys: simplify AF_VSOCK check, Paolo Bonzini, 2022/04/19
- [PULL 41/53] thread-posix: use monotonic clock for QemuCond and QemuSemaphore,
Paolo Bonzini <=
- [PULL 40/53] thread-posix: remove the posix semaphore support, Paolo Bonzini, 2022/04/19
- [PULL 44/53] hyperv: SControl is optional to enable SynIc, Paolo Bonzini, 2022/04/19
- [PULL 52/53] target/i386: do not access beyond the low 128 bits of SSE registers, Paolo Bonzini, 2022/04/19
- [PULL 30/53] include: move qemu_get_vm_name() to sysemu.h, Paolo Bonzini, 2022/04/19
- [PULL 05/53] oslib: drop qemu_gettimeofday(), Paolo Bonzini, 2022/04/19
- [PULL 03/53] qga: replace qemu_gettimeofday() with g_get_real_time(), Paolo Bonzini, 2022/04/19
- [PULL 04/53] Replace qemu_gettimeofday() with g_get_real_time(), Paolo Bonzini, 2022/04/19
- [PULL 13/53] osdep: poison {HOST,TARGET}_WORDS_BIGENDIAN, Paolo Bonzini, 2022/04/19
- [PULL 09/53] meson: remove test-qdev-global-props dependency on testqapi, Paolo Bonzini, 2022/04/19
- [PULL 10/53] char: move qemu_openpty_raw from util/ to char/, Paolo Bonzini, 2022/04/19