[fpc-devel] Possible idea... "safe" subroutines/methods

Jonas Maebe jonas at freepascal.org
Sun May 5 20:35:46 CEST 2019

On 05/05/2019 19:34, J. Gareth Moreton wrote:

> For the volatile intrinsic, was there talk of "volatile" being an 
> attribute as well? I'm guessing this means declaring something as, for 
> example "var FTerminated: Boolean volatile;"

and the linked article from that entry for more about that.

> My fear with allowing the promotion of global variables to registers is 
> that it will break old code written when the "volatile" intrinsic didn't 
> exist.

There is very little thread-safe code that can be broken by this change. 
The reason is that just waiting on a global variable to change its value 
is not nearly enough to synchronise two threads, unless they don't 
exchange any other data besides that one single variable. You need 
memory barriers and on certain architectures also acquire/release, all 
of which cannot be analysed by the compiler and hence it will have to 
assume that all global data may have changed across them (and hence it 
will reload all global data from memory).

Additionally, spinning on a volatile variable (without calling "yield" 
or something similar in the loop, which would stop this change from 
breaking anything) is about the most inefficient way in existence to 
synchronise two threads.

>   Then again, it can simply be an option that is only present in 
> -O4 or if the programmer specifically asks for it (and maybe as an 
> option in Lazarus' "Project Options" dialog).

That is not possible for LLVM at least. It's just a generic optimisation 
there. I don't consider this to be a big problem, because proper 
multi-threading code won't be affected by it. Broken code will also 
break in a very obvious and easy to locate way: it will just sit in an 
endless loop at the location where the loop was.


More information about the fpc-devel mailing list