Support for "class threadvar" sections inside class and record types

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Support for "class threadvar" sections inside class and record types

Free Pascal - General mailing list
Hello together!

Announcing a small feature this time, but some might appreciate it:
FPC now supports the declaration of threadvars inside class and record
types.

=== code begin ===

{$mode objfpc}
{$modeswitch advancedrecords}

type
   TTestClass = class
   public class threadvar
     Test: LongInt;
   end;

   TTestRecord = record
   public class threadvar
     Test: LongInt;
   end;

begin
   TTestClass.Test := 42;
   TTestRecord.Test := 21;
end.

=== code end ===

A threadvar section is started using "class threadvar" and the "class"
specifier is required. They are accessed like ordinary class variables
with the difference that they'll hold different values per thread - the
same as global threadvars after all.

This feature is Delphi compatible (and was added, because I noticed that
Delphi supported it and FPC did not 😅)

Regards,
Sven
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Support for "class threadvar" sections inside class and record types

Ryan Joseph


> On Jun 23, 2018, at 8:55 PM, Sven Barth via fpc-pascal <[hidden email]> wrote:
>
> A threadvar section is started using "class threadvar" and the "class" specifier is required. They are accessed like ordinary class variables with the difference that they'll hold different values per thread - the same as global threadvars after all.
>
> This feature is Delphi compatible (and was added, because I noticed that Delphi supported it and FPC did not 😅)

That’s an interesting idea I wonder how it could be used. Does it do anything to help with shared data between threads or is just a way to get quick access to thread safe memory?

Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Support for "class threadvar" sections inside class and record types

Marco van de Voort
In our previous episode, Ryan Joseph said:
> >
> > A threadvar section is started using "class threadvar" and the "class" specifier is required. They are accessed like ordinary class variables with the difference that they'll hold different values per thread - the same as global threadvars after all.
> >
> > This feature is Delphi compatible (and was added, because I noticed that Delphi supported it and FPC did not ??
>
> That?s an interesting idea I wonder how it could be used. Does it do
> anything to help with shared data between threads or is just a way to get
> quick access to thread safe memory?

More probably they needed it for the TMonitor implementation, which is used
as a kind of spinlock in front of every OS synchronization primitive.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Support for "class threadvar" sections inside class and record types

Free Pascal - General mailing list
In reply to this post by Ryan Joseph
Ryan Joseph <[hidden email]> schrieb am Sa., 23. Juni 2018, 16:04:


> On Jun 23, 2018, at 8:55 PM, Sven Barth via fpc-pascal <[hidden email]> wrote:
>
> A threadvar section is started using "class threadvar" and the "class" specifier is required. They are accessed like ordinary class variables with the difference that they'll hold different values per thread - the same as global threadvars after all.
>
> This feature is Delphi compatible (and was added, because I noticed that Delphi supported it and FPC did not 😅)

That’s an interesting idea I wonder how it could be used. Does it do anything to help with shared data between threads or is just a way to get quick access to thread safe memory?

It's merely a scoped threadvar, so nothing special there. And in case you don't know about threadvars: the concept of them is that every thread has its own "instance" of the variable. 


Regards, 
Sven 

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Support for "class threadvar" sections inside class and record types

Ryan Joseph


> On Jun 23, 2018, at 9:45 PM, Sven Barth via fpc-pascal <[hidden email]> wrote:
>
> See also here: https://www.freepascal.org/docs-html/ref/refse26.html

I had no idea this existed. When was it added?

Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Support for "class threadvar" sections inside class and record types

Free Pascal - General mailing list
Am 23.06.2018 um 16:51 schrieb Ryan Joseph:
>
>> On Jun 23, 2018, at 9:45 PM, Sven Barth via fpc-pascal <[hidden email]> wrote:
>>
>> See also here: https://www.freepascal.org/docs-html/ref/refse26.html
> I had no idea this existed. When was it added?
Probably around 2.0 when Delphi support was added... 🤷‍♀️ So quite a
long time ago

Regards,
Sven
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Support for "class threadvar" sections inside class and record types

R0b0t1
In reply to this post by Ryan Joseph
On Saturday, June 23, 2018, Ryan Joseph <[hidden email]> wrote:

>
>
>> On Jun 23, 2018, at 9:45 PM, Sven Barth via fpc-pascal <[hidden email]> wrote:
>>
>> See also here: https://www.freepascal.org/docs-html/ref/refse26.html
>
> I had no idea this existed. When was it added?
>
> Regards,
>         Ryan Joseph
>

I found them one day as well and was confused. The note that they implement TMonitor is helpful. I know of no other uses.

Cheers,
    R0b0t1

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Support for "class threadvar" sections inside class and record types

Anthony Walter-3
Much thanks Sven! I'm note sure when I'll use it, but the option is always nice to have. A small question, since it's available in a class, does "class threadvar" support generics?

{$mode delphi}

type
  TAnimal<T: TTrainer> = class
  public class threadvar Trainer: T;
  end;

For everyone else, threadvar is the language integrated simplification of thread local storage.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Support for "class threadvar" sections inside class and record types

Free Pascal - General mailing list
Anthony Walter <[hidden email]> schrieb am Sa., 23. Juni 2018, 22:56:
Much thanks Sven! I'm note sure when I'll use it, but the option is always nice to have. A small question, since it's available in a class, does "class threadvar" support generics?

{$mode delphi}

type
  TAnimal<T: TTrainer> = class
  public class threadvar Trainer: T;
  end;

For everyone else, threadvar is the language integrated simplification of thread local storage.

In theory yes, but class variables inside generics currently don't work as one expects (they never did) and will need quite an intensive fix. 

Regards, 
Sven 

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Support for "class threadvar" sections inside class and record types

Free Pascal - General mailing list
In reply to this post by R0b0t1
R0b0t1 <[hidden email]> schrieb am Sa., 23. Juni 2018, 18:50:
On Saturday, June 23, 2018, Ryan Joseph <[hidden email]> wrote:

>
>
>> On Jun 23, 2018, at 9:45 PM, Sven Barth via fpc-pascal <[hidden email]> wrote:
>>
>> See also here: https://www.freepascal.org/docs-html/ref/refse26.html
>
> I had no idea this existed. When was it added?
>
> Regards,
>         Ryan Joseph
>

I found them one day as well and was confused. The note that they implement TMonitor is helpful. I know of no other uses.

The TMonitor was a guess by Michael. 
Threadvars themselves have many uses: the per thread heap management that FPC's heap uses, one output file per thread, so that StdOut doesn't cross, the current TThread instance using TThread.CurrentThread (which Delphi probably implemented using a private class threadvar while for FPC I used a global threadvar in the implementation section) and so on. 

Regards,
Sven 

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal