summaryrefslogtreecommitdiffstats
path: root/src/corelib/thread/qsemaphore.cpp
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2023-06-07 22:46:59 -0700
committerThiago Macieira <thiago.macieira@intel.com>2023-06-09 10:53:11 -0700
commitfbc491230fe62f739925e8113d05f4108e2738ef (patch)
treeec3ffc987c06392ae71af512313638e764dead70 /src/corelib/thread/qsemaphore.cpp
parent0f9894f79bd5162858830f5c6e665fa7d381def5 (diff)
Q{Semaphore,ReadWriteLock}Private: reorganize the members
They are now ordered so that the mutex and the wait condition are in different cache lines, to avoid false sharing situations, if the types are holding the actual threading primitive structures, not mere pointers to some other structure elsewhere. For 64-bit systems: OS | mutex | cond | Remark --------------------+-------+-------+------------------ Darwin | 64 | 48 | FreeBSD | 8 | 8 | INTEGRITY | 8 | 8 | QMutex & QWaitCondition Linux | 24 | 48 | Always uses futex MinGW (Winpthreads) | 8 | 8 | Always uses futex MSVC (MS STL) | 32 | 16 | Always uses futex NetBSD | 48 | 40 | OpenBSD | 8 | 8 | QNX | ? | ? | Change-Id: I63b988479db546dabffcfffd176698e4f0097e90 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io> Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'src/corelib/thread/qsemaphore.cpp')
-rw-r--r--src/corelib/thread/qsemaphore.cpp29
1 files changed, 23 insertions, 6 deletions
diff --git a/src/corelib/thread/qsemaphore.cpp b/src/corelib/thread/qsemaphore.cpp
index 73e88cf42c..4915bde25a 100644
--- a/src/corelib/thread/qsemaphore.cpp
+++ b/src/corelib/thread/qsemaphore.cpp
@@ -251,14 +251,31 @@ futexSemaphoreTryAcquire(QBasicAtomicInteger<quintptr> &u, int n, T timeout)
return false;
}
-class QSemaphorePrivate {
-public:
- explicit QSemaphorePrivate(int n) : avail(n) { }
+namespace { namespace QtSemaphorePrivate {
+using namespace QtPrivate;
+struct Layout1
+{
+ alignas(IdealMutexAlignment) QtPrivate::mutex mutex;
+ qsizetype avail = 0;
+ alignas(IdealMutexAlignment) QtPrivate::condition_variable cond;
+};
+
+struct Layout2
+{
+ alignas(IdealMutexAlignment) QtPrivate::mutex mutex;
+ alignas(IdealMutexAlignment) QtPrivate::condition_variable cond;
+ qsizetype avail = 0;
+};
- QtPrivate::mutex mutex;
- QtPrivate::condition_variable cond;
+// Choose Layout1 if it is smaller than Layout2. That happens for platforms
+// where sizeof(mutex) is 64.
+using Members = std::conditional_t<sizeof(Layout1) <= sizeof(Layout2), Layout1, Layout2>;
+} } // namespace QtSemaphorePrivate
- int avail;
+class QSemaphorePrivate : public QtSemaphorePrivate::Members
+{
+public:
+ explicit QSemaphorePrivate(qsizetype n) { avail = n; }
};
/*!