Fiber

This class provides a cooperative concurrency mechanism integrated with the threading and garbage collection functionality. Calling a fiber may be considered a blocking operation that returns when the fiber yields (via Fiber.yield()). Execution occurs within the context of the calling thread so synchronization is not necessary to guarantee memory visibility so long as the same thread calls the fiber each time. Please note that there is no requirement that a fiber be bound to one specific thread. Rather, fibers may be freely passed between threads so long as they are not currently executing. Like threads, a new fiber thread may be created using either derivation or composition, as in the following example.

Warning: Status registers are not saved by the current implementations. This means floating point exception status bits (overflow, divide by 0), rounding mode and similar stuff is set per-thread, not per Fiber!

Warning: On ARM FPU registers are not saved if druntime was compiled as ARM_SoftFloat. If such a build is used on a ARM_SoftFP system which actually has got a FPU and other libraries are using the FPU registers (other code is compiled as ARM_SoftFP) this can cause problems. Druntime must be compiled as ARM_SoftFP in this case.

Constructors

this
this(void function() fn, size_t sz = PAGESIZE * defaultStackPages, size_t guardPageSize = PAGESIZE)

Initializes a fiber object which is associated with a static D function.

this
this(void delegate() dg, size_t sz = PAGESIZE * defaultStackPages, size_t guardPageSize = PAGESIZE)

Initializes a fiber object which is associated with a dynamic D function.

Destructor

~this
~this()

Cleans up any remaining resources used by this object.

Members

Enums

Rethrow
enum Rethrow

Flag to control rethrow behavior of $(LREF call)

State
enum State

//////////////////////////////////////////////////////////////////////// A fiber may occupy one of three states: HOLD, EXEC, and TERM.

Functions

call
Throwable call(Rethrow rethrow = Rethrow.yes)
Throwable call()
Throwable call(bool rethrow)

Transfers execution to this fiber object. The calling context will be suspended until the fiber calls Fiber.yield() or until it terminates via an unhandled exception.

reset
void reset()
void reset(void function() fn)
void reset(void delegate() dg)

Resets this fiber so that it may be re-used, optionally with a new function/delegate. This routine should only be called for fibers that have terminated, as doing otherwise could result in scope-dependent functionality that is not executed. Stack-based classes, for example, may not be cleaned up properly if a fiber is reset before it has terminated.

Manifest constants

defaultStackPages
enum defaultStackPages;

////////////////////////////////////////////////////////////////////////

Properties

state
State state [@property getter]

Gets the current state of this fiber.

Static functions

getThis
Fiber getThis()

Provides a reference to the calling fiber or null if no fiber is currently active.

yield
void yield()

Forces a context switch to occur away from the calling fiber.

yieldAndThrow
void yieldAndThrow(Throwable t)

Forces a context switch to occur away from the calling fiber and then throws obj in the calling fiber.

Examples

1 int counter;
2 
3 class DerivedFiber : Fiber
4 {
5     this()
6     {
7         super( &run );
8     }
9 
10 private :
11     void run()
12     {
13         counter += 2;
14     }
15 }
16 
17 void fiberFunc()
18 {
19     counter += 4;
20     Fiber.yield();
21     counter += 8;
22 }
23 
24 // create instances of each type
25 Fiber derived = new DerivedFiber();
26 Fiber composed = new Fiber( &fiberFunc );
27 
28 assert( counter == 0 );
29 
30 derived.call();
31 assert( counter == 2, "Derived fiber increment." );
32 
33 composed.call();
34 assert( counter == 6, "First composed fiber increment." );
35 
36 counter += 16;
37 assert( counter == 22, "Calling context increment." );
38 
39 composed.call();
40 assert( counter == 30, "Second composed fiber increment." );
41 
42 // since each fiber has run to completion, each should have state TERM
43 assert( derived.state == Fiber.State.TERM );
44 assert( composed.state == Fiber.State.TERM );

Meta

Authors

Based on a design by Mikola Lysenko.

Suggestion Box / Bug Report