From ccd3f2369a514a40c4d0427415bd7fcad9382002 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 23 Feb 2023 10:28:28 -0800 Subject: [PATCH] QWaitCondition/Unix: Modernize the call to pthread_condattr_setclock This avoids creating and destroying the pthread_condattr_t on systems without a monotonic clock (INTEGRITY) or for which we can't ask that pthread_cond_t use it (Darwin). Change-Id: Ieec322d73c1e40ad95c8fffd1746878316ab8708 Reviewed-by: Edward Welbourne Reviewed-by: Qt CI Bot Reviewed-by: Marc Mutz Reviewed-by: Ahmad Samir --- src/corelib/thread/qwaitcondition_unix.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/corelib/thread/qwaitcondition_unix.cpp b/src/corelib/thread/qwaitcondition_unix.cpp index 443753bea5..68fb92a0c8 100644 --- a/src/corelib/thread/qwaitcondition_unix.cpp +++ b/src/corelib/thread/qwaitcondition_unix.cpp @@ -41,20 +41,23 @@ static void report_error(int code, const char *where, const char *what) void qt_initialize_pthread_cond(pthread_cond_t *cond, const char *where) { + pthread_condattr_t *attrp = nullptr; + +#if defined(CLOCK_MONOTONIC) && !defined(Q_OS_DARWIN) pthread_condattr_t condattr; + attrp = &condattr; pthread_condattr_init(&condattr); -#ifdef CLOCK_MONOTONIC + auto destroy = qScopeGuard([&] { pthread_condattr_destroy(&condattr); }); #if defined(Q_OS_ANDROID) if (local_condattr_setclock) local_condattr_setclock(&condattr, CLOCK_MONOTONIC); -#elif !defined(Q_OS_DARWIN) - // Darwin doesn't have this function +#else pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC); #endif #endif - report_error(pthread_cond_init(cond, &condattr), where, "cv init"); - pthread_condattr_destroy(&condattr); + + report_error(pthread_cond_init(cond, attrp), where, "cv init"); } void qt_abstime_for_timeout(timespec *ts, QDeadlineTimer deadline)