What is safe to do in an extern (C) function and how can I test this?

Jaime benjamin.i.mccann at gmail.com
Tue Jan 25 03:03:44 UTC 2022


On Tuesday, 25 January 2022 at 01:41:03 UTC, Steven Schveighoffer 
wrote:
> On 1/24/22 8:31 PM, Jaime wrote:
>> Can I, for instance, safely call Fiber.yield in a C callback 
>> that I know will be run in a Fiber?
>
> I would *imagine* it's fine, all the fiber context switch is 
> doing (WRT the stack) is swapping out the fiber portion of the 
> stack (i.e. back to the `Fiber.call` invokation), which should 
> include the C stack portions as well.
>
> There's a lot of low-level details in the Fiber.d file itself:
>
> https://github.com/dlang/druntime/blob/e390ba7e0a1f80f15e72ca773fca7252057ba4c5/src/core/thread/fiber.d#L387
>
> -Steve

On Tuesday, 25 January 2022 at 02:02:27 UTC, H. S. Teoh wrote:
> I haven't tested this myself, but I *think* it should be OK.
>
> One thing to watch out for, though, is the size of the Fiber's 
> stack (this can be specified when you first create the Fiber).  
> If the C part of the code uses up too much stack space (e.g. if 
> somewhere in the C code it tries to allocate a large object on 
> the stack), you may inadvertently overflow the Fiber's stack 
> and cause a crash or abort. Increasing the stack size of the 
> Fiber when it is created should fix this problem.
>
>
> T

The insight and vote of confidence are much appreciated, you two. 
Steven, good call to check out the implementation of Fiber, I'll 
do that.


More information about the Digitalmars-d-learn mailing list