From f02879a97a6e7587a7e116f85d6a153f616a5d3f Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Mon, 25 Apr 2022 14:21:34 -0700 Subject: QFutureInterface: insert x86 PAUSE in tight CMPXCHG loop From the Intel Software Optimization Manual[1], Chapter 11 ("Multi-core and Hyper-Threading Technology"), offers: User/Source Coding Rule 14. (M impact, H generality) Insert the PAUSE instruction in fast spin loops and keep the number of loop repetitions to a minimum to improve overall system performance. See section 11.4.2 for an explanation of why this is a good idea. [1] https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html#inpage-nav-5 Pick-to: 6.3 Change-Id: I7fb65b80b7844c8d8f26fffd16e94088dad1ceee Reviewed-by: Sona Kurazyan --- src/corelib/thread/qfutureinterface.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'src/corelib/thread/qfutureinterface.cpp') diff --git a/src/corelib/thread/qfutureinterface.cpp b/src/corelib/thread/qfutureinterface.cpp index 71d6670538..a54caa878b 100644 --- a/src/corelib/thread/qfutureinterface.cpp +++ b/src/corelib/thread/qfutureinterface.cpp @@ -45,6 +45,10 @@ #include #include +#ifdef Q_PROCESSOR_X86 +# include // for _mm_pause() +#endif + #ifdef interface # undef interface #endif @@ -106,9 +110,14 @@ static inline int switch_from_to(QAtomicInt &a, int from, int to) { int newValue; int expected = a.loadRelaxed(); - do { + for (;;) { newValue = (expected & ~from) | to; - } while (!a.testAndSetRelaxed(expected, newValue, expected)); + if (a.testAndSetRelaxed(expected, newValue, expected)) + break; +#ifdef Q_PROCESSOR_X86 + _mm_pause(); +#endif + } return newValue; } -- cgit v1.2.3