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

J. Gareth Moreton gareth at moreton-family.com
Fri May 3 23:10:58 CEST 2019


A good question.  I did already partially answer it:

"For reasons of safety, the compiler should probably not consider values 
thread-safe across the boundary of a try...except or a try...finally 
block, not because of the risk of another thread modifying it, but 
because an interrupt might, caused by an exception."

When it comes to just reading non-local values, this will pose no 
problem, since if control is returned to the procedure, it will be in 
the except block and then the code that follows... different blocks.  If 
the non-local value is written to prior to the exception though, then it 
becomes a bit more complicated - one can't just write back to the global 
storage prior to an exception-prone call because that would be both hard 
to detect and very inefficient.

The safe answer would be that using local storage should only apply to 
non-local values that are only read, not written to. Extending it to 
modifying non-local values would require deeper research at best.

Gareth aka. Kit

On 03/05/2019 20:45, Michael Van Canneyt wrote:
>
>
> On Fri, 3 May 2019, J. Gareth Moreton wrote:
>
>> This is something I've been thinking about for a while, and I wonder 
>> how practical it is or if it's a good idea - the ability to mark 
>> specific routines as thread-safe with a directive named "safe" or 
>> "threadsafe".
>>
>> By telling the compiler that the procedure (or maybe a whole class) 
>> is thread-safe, you are telling it that you can guarantee that any 
>> objects, fields or global variables that you access are guaranteed to 
>> not suddenly change mid-routine (because another thread has modified 
>> it).  This would allow the compiler to move commonly-accessed fields 
>> into local registers or the stack for faster access, especially if 
>> the fields are only read and not written, since they'll be guaranteed 
>> to contain a constant value.
>
> What about exceptions ?
>
> Michael.
>
> _______________________________________________
> fpc-devel maillist  -  fpc-devel at lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20190503/4eabb138/attachment.html>


More information about the fpc-devel mailing list