[fpc-pascal] cpu_relax() in freepascal

Karoly Balogh (Charlie/SGR) charlie at scenergy.dfmk.hu
Fri Nov 3 11:43:29 CET 2017


On Fri, 3 Nov 2017, Matias Vara wrote:

> Hello,  I think it would be better if I put my question in context. In
> order to tell the processor that we are in a loop, the pause instruction
> could be used (also I think the "rep nop" produces the same opcode).
> This tells the processor that we are in a loop so it improves the access
> to the cache and the branching mechanism. My question is if the compiler
> is doing any improvement of this kind in a loop or I should write my own
> "pause" procedure/macro. For more info
> https://software.intel.com/en-us/articles/benefitting-power-and-performance-sleep-loops

This feature seems to be a compiler intrinsic at least in the Intel C
compiler. We don't have anything similar, so you have to fall back either
to an asm procedure or a line of inline assembler. The compiler is also
not doing an optimization similar to this. Free Pascal's inline assembler
supports the PAUSE instruction though.

But unless you plan to use this in a multithreaded spinloop because you're
loading multiple cores of the CPU, it's better to let the operating system
handle the wait. This instruction is certainly not the way to fix a "my
app uses 100% CPU" scenario, just saying.


More information about the fpc-pascal mailing list