bmx-ng / bcc

A next-generation bcc parser for BlitzMax
zlib License
33 stars 13 forks source link

[Suggestion] Expose gcc's "__thread" specifier #538

Closed AXKuhta closed 3 years ago

AXKuhta commented 3 years ago

GCC features a __thread variable specifier that will make the variable unique to the current thread. I've managed to harness it's power by using an external .c file that declares the variable along with set/get functions and Externing the set/get functions from BlitzMax.

Here's the relevant code in my small web server: https://github.com/AXKuhta/ObscureServer/blob/master/thread_local_storage.c#L4-L12 https://github.com/AXKuhta/ObscureServer/blob/master/Parameters.bmx#L5-L8 https://github.com/AXKuhta/ObscureServer/blob/master/ServeThread.bmx#L8-L10

What do you think about making this feature usable directly from BlitzMax itself? This should remove the overhead of calling the set/get function (which is like 5 instructions, but still).

Perhaps a Threadlocal keyword could be used to declare a __thread variable. Or maybe Thread Local. Or would that be Threadglobal?

woollybah commented 3 years ago

Added ThreadedGlobal variable scope, which implements thread local storage. e.g.

ThreadedGlobal counter:Int

' ...

Function ThreadFunc:Object(obj:Object)
    counter = 0

    For Local i:Int = 0 until 100
        counter :+ 1
    Next

End Function