summaryrefslogtreecommitdiffstats
path: root/src/corelib/thread/qmutex.h
diff options
context:
space:
mode:
authorGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2022-04-10 12:29:47 +0200
committerGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2022-04-11 23:26:01 +0200
commitb2233f19c9be9b87f4a49a4169aab6f5e7617563 (patch)
tree134f283d5efd27d02e461a9405ff6ea26ab0c347 /src/corelib/thread/qmutex.h
parente9962537743333059b4857ce63e9888cb9591774 (diff)
Annotate QMutex with TSAN annotations
The Q(Basic)Mutex fast paths are entirely inline in the caller, which means we need to annotate its operations directly or TSAN doesn't know what's going on. Also annotate QRecursiveMutex. The tryLock code could be in principle simplified via a QScopeGuard but I didn't want to make a central class like QMutex depend on it. [ChangeLog][QtCore][QMutex] QMutex now has annotations for ThreadSanitizer. Change-Id: Ibb130404e63a5ec9bcef9675f9addd16a2c38b7f Reviewed-by: David Faure <david.faure@kdab.com> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/thread/qmutex.h')
-rw-r--r--src/corelib/thread/qmutex.h41
1 files changed, 37 insertions, 4 deletions
diff --git a/src/corelib/thread/qmutex.h b/src/corelib/thread/qmutex.h
index 9423585f641..85279413bfb 100644
--- a/src/corelib/thread/qmutex.h
+++ b/src/corelib/thread/qmutex.h
@@ -42,6 +42,7 @@
#include <QtCore/qglobal.h>
#include <QtCore/qatomic.h>
+#include <QtCore/qtsan_impl.h>
#include <new>
#if __has_include(<chrono>)
@@ -104,19 +105,37 @@ public:
// BasicLockable concept
inline void lock() QT_MUTEX_LOCK_NOEXCEPT {
+ QtTsan::mutexPreLock(this, 0u);
+
if (!fastTryLock())
lockInternal();
+
+ QtTsan::mutexPostLock(this, 0u, 0);
}
// BasicLockable concept
inline void unlock() noexcept {
Q_ASSERT(d_ptr.loadRelaxed()); //mutex must be locked
+
+ QtTsan::mutexPreUnlock(this, 0u);
+
if (!fastTryUnlock())
unlockInternal();
+
+ QtTsan::mutexPostUnlock(this, 0u);
}
bool tryLock() noexcept {
- return fastTryLock();
+ unsigned tsanFlags = QtTsan::TryLock;
+ QtTsan::mutexPreLock(this, tsanFlags);
+
+ const bool success = fastTryLock();
+
+ if (!success)
+ tsanFlags |= QtTsan::TryLockFailed;
+ QtTsan::mutexPostLock(this, tsanFlags, 0);
+
+ return success;
}
// Lockable concept
@@ -168,9 +187,23 @@ public:
using QBasicMutex::tryLock;
bool tryLock(int timeout) QT_MUTEX_LOCK_NOEXCEPT
{
- if (fastTryLock())
- return true;
- return lockInternal(timeout);
+ unsigned tsanFlags = QtTsan::TryLock;
+ QtTsan::mutexPreLock(this, tsanFlags);
+
+ bool success = fastTryLock();
+
+ if (success) {
+ QtTsan::mutexPostLock(this, tsanFlags, 0);
+ return success;
+ }
+
+ success = lockInternal(timeout);
+
+ if (!success)
+ tsanFlags |= QtTsan::TryLockFailed;
+ QtTsan::mutexPostLock(this, tsanFlags, 0);
+
+ return success;
}
// TimedLockable concept