#1 2024-11-04 13:28:31

Javierus
Member
Registered: 2019-09-18
Posts: 55

threadvar // do not publish for compilation within Delphi packages

In practice, what does it mean with "do not..."?

Offline

#2 2024-11-04 14:29:25

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,655
Website

Re: threadvar // do not publish for compilation within Delphi packages

It means "do not access directly this threadvar when used within Delphi packages".
Or it would fail compiling.

This is a limitation of Delphi packages: to properly load and link a package, any threadvar variable should not be accessed directly, but through a function which returns a pointer to the current threadvar.
AFAIK this is because threadvar uses some kind of lookup table (TLS = Thread Local Storage) which changes for each compiled module.

Offline

#3 2024-11-04 16:53:02

Javierus
Member
Registered: 2019-09-18
Posts: 55

Re: threadvar // do not publish for compilation within Delphi packages

Please, excuse my newbieness in this respect... Does it mean threadvars inside packages are "safe" if they are declared inside the implementation?
Because all the threadvars in mormot2 are declared inside the implementation, so unless you modify the mormot2 sources, they would be safe

Last edited by Javierus (2024-11-04 16:56:11)

Offline

#4 2024-11-04 20:46:06

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,655
Website

Re: threadvar // do not publish for compilation within Delphi packages

It may not be enough, if they are used in an inlined function for instance...

Offline

#5 2024-11-05 08:46:34

Javierus
Member
Registered: 2019-09-18
Posts: 55

Re: threadvar // do not publish for compilation within Delphi packages

So we can not compile mORMot2 as-is in a package and use it safely?

Offline

#6 2024-11-05 08:57:45

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,655
Website

Re: threadvar // do not publish for compilation within Delphi packages

You can use it in a package: it tries to properly compile and avoid any linking issue.

If you have any actual problem, feel free to report.

Offline

#7 2024-11-05 09:57:26

Javierus
Member
Registered: 2019-09-18
Posts: 55

Re: threadvar // do not publish for compilation within Delphi packages

Thank you; it does compile perfectly
I didn't understood you, but finally did

Offline

#8 2024-11-05 13:34:21

danielkuettner
Member
From: Germany
Registered: 2014-08-06
Posts: 357

Re: threadvar // do not publish for compilation within Delphi packages

Threadvar's are created in a special memory area (thread-local storage). At compile time the addresses of that space for a thread created at runtime isn't fix.
Therefore libraries linked at compile time (which packages are) could have an issue with threadvar's. Dynamically loaded libraries are fine, because they will loaded at runtime, when the threads run.

Because of this it doesn't matter whether a threadvar is declared in interface or implementation part.

Offline

Board footer

Powered by FluxBB