[fpc-devel] Comparison FPC 2.6.2 - Delphi 7

Michael Schnell mschnell at lumino.de
Wed Mar 6 11:12:07 CET 2013


On 03/04/2013 12:47 PM, Michael Schnell wrote:
>
> In all fpc enabled patforms I examined (X86 Windows, X86 Linux, X64-64 
> Windows, X86-64 Linux, ARM Linux (32 Bits) the platform provides as 
> well library calls as dedicated threadvar pointer registers. In 
> windows the registers seem to be undocumented but obviously "stable", 
> as a change would break a lot existing C/C++ software.
>
> All C/C++ compilers I examined used the threadvar pointer Registers 
> for speed..
>

FWIW:
I rechecked this and can add these results:

With ARM, the pre-Cortex Arch on Linux uses A9 as a pointer to the TLS 
(Thread local Storage http://en.wikipedia.org/wiki/Thread-local_storage 
, that holds the threadvars). So (as with all x86 variants) no library 
or system call is necessary.

With Cortex, this has changed to the use of the CP15 Register (freeing 
A9 to allow for better optimization). Now, CP15 is in fact not very 
suitable for this purpose, as it can only be accessed with privileged 
instructions and thus not in user mode (where the TLS in fact is 
invented for). Thus a system call (or trap) is needed to get the address 
of a threadvar. As a system call supposedly is rather costly on ARM  
(invalidating the cache, as here, (other than with x86) the cache is 
closer to the CPU than the MMU) using a library call will not do 
additional harm.




Generally:

The frequent access to threadvars is necessary with threaded programs 
that use the same code in multiple threads to allow for each thread to 
find easily out "who I am" even in a deeply nested function environment.

But in an object driven language, this can easily be overcome by 
creating an instance of an object for each thread and with that, you 
always have the "Self" pointer as a thread specific entity. Happily 
"Self" resides on the stack that is dedicated for each thread, anyway, 
and is created automatically for any "procedure or object". So the use 
of any threadvar is necessary only when creating the thread-related 
object but it's not necessary to access any threadvar frequently.




Conclusion:

With FPC, the optimization of threadvar access is not necessary. Instead 
it is highly recommended to always use thread specific instances of the 
classes that do the threaded work. TThread of course provides this out 
of the box.

-Michael




More information about the fpc-devel mailing list