Thread overview
Fiber Local Storage
Jul 26, 2013
Manu
Jul 26, 2013
Vladimir Panteleev
Jul 26, 2013
deadalnix
Jul 29, 2013
Martin Nowak
Jul 30, 2013
deadalnix
Aug 15, 2013
Martin Nowak
Jul 29, 2013
Luís Marques
Aug 15, 2013
Martin Nowak
July 26, 2013
Is there an FLS implementation floating around?
If not, it's probably something that should be considered for std.thread.


July 26, 2013
On Friday, 26 July 2013 at 04:30:18 UTC, Manu wrote:
> Is there an FLS implementation floating around?
> If not, it's probably something that should be considered for std.thread.

Maybe something like this:

----------------------------------

private struct FLSEntry
{
    void** ptr;
    size_t start;
    immutable void[] initData;
}

private __gshared FLSEntry[] flsEntries;
private __gshared size_t flsSize;

void registerFLS(void** ptr, immutable void[] initData)
{
    enum ALIGN = size_t.sizeof;
    auto size = (initData.length + ALIGN-1) % ALIGN;
    flsEntries ~= FLSEntry(ptr, flsSize, initData);
    flsSize += size;
}

// called by core.thread on fiber creation
void initFLS(Fiber fiber)
{
    fiber.fls = new void[flsSize];
    void* p = fiber.fls.ptr;
    foreach (entry; flsEntries)
        with (entry)
            p[start..start+initData.length] = initData[];
}

// called by core.thread when switching to a fiber
void switchFLS(Fiber to)
{
    void* p = to.fls.ptr;
    foreach (entry; flsEntries)
        *entry.ptr = p + entry.start;
}

struct FLS(T)
{
    shared static this()
    {
        //registerFLS(&data, (&T.init)[0..1]);
        static immutable T init;
        registerFLS(cast(void**)&data, (&init)[0..1]);
    }

    static T* data;

    alias data this;
}

// ***************************************

struct MyData
{
    int i;
}

FLS!MyData fiberData;

----------------------------------

A linker segment would be better, but not sure how feasible that would be.
July 26, 2013
On Friday, 26 July 2013 at 04:30:18 UTC, Manu wrote:
> Is there an FLS implementation floating around?
> If not, it's probably something that should be considered for std.thread.

I'd also love to have one ! (in fact I do think it should be the default).
July 29, 2013
On 07/26/2013 08:06 AM, deadalnix wrote:
> On Friday, 26 July 2013 at 04:30:18 UTC, Manu wrote:
>> Is there an FLS implementation floating around?
>> If not, it's probably something that should be considered for std.thread.
>
> I'd also love to have one ! (in fact I do think it should be the default).

What do you mean by that, FLS instead of TLS and everything runs as Fiber?
July 29, 2013
On Friday, 26 July 2013 at 04:30:18 UTC, Manu wrote:
> Is there an FLS implementation floating around?
> If not, it's probably something that should be considered for std.thread.

Nice to see this asked. For a simulation library I was developing I was thinking that fiber support had to be done by asking the library user to swap a struct every time a fiber context-switched. If that could be automated then that would be great!

July 30, 2013
On Monday, 29 July 2013 at 15:25:49 UTC, Martin Nowak wrote:
> On 07/26/2013 08:06 AM, deadalnix wrote:
>> On Friday, 26 July 2013 at 04:30:18 UTC, Manu wrote:
>>> Is there an FLS implementation floating around?
>>> If not, it's probably something that should be considered for std.thread.
>>
>> I'd also love to have one ! (in fact I do think it should be the default).
>
> What do you mean by that, FLS instead of TLS and everything runs as Fiber?

Yes, with a scheduler int the runtime.
August 15, 2013
On Tuesday, 30 July 2013 at 01:45:45 UTC, deadalnix wrote:
> On Monday, 29 July 2013 at 15:25:49 UTC, Martin Nowak wrote:
>> What do you mean by that, FLS instead of TLS and everything runs as Fiber?
>
> Yes, with a scheduler int the runtime.

I don't think FLS can be as fast as TLS because the latter benefits from many optimizations due to linker and OS support.
August 15, 2013
On Friday, 26 July 2013 at 04:30:18 UTC, Manu wrote:
> Is there an FLS implementation floating around?
> If not, it's probably something that should be considered for std.thread.

- There is WorkerLocalStorage in std.parallelism Tasks.
- Apparently you can easily capture variables in the context of the delegate.
- If you use thread affinity when executing Fibers you can access TLS.