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 <sona.kurazyan@qt.io>
This commit is contained in:
Thiago Macieira 2022-04-25 14:21:34 -07:00
parent f3640d9086
commit f02879a97a

View File

@ -45,6 +45,10 @@
#include <QtCore/qthread.h>
#include <private/qthreadpool_p.h>
#ifdef Q_PROCESSOR_X86
# include <immintrin.h> // 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;
}