core.thread.fiber - D Programming Language

Menu

- Learn
- Documentation
  - Language Reference
  - Library Reference
  - Command-line Reference
  - Feature Overview
  - Articles
- Downloads
- Packages
- Community
  - Blog
  - Orgs using D
  - Twitter
  - Calendar
  - Forums
  - IRC
  - Community Discord
  - Wiki
  - GitHub
  - Issues
  - Get involved
  - Contributors
  - Foundation
  - Security Team
  - Donate
  - Sponsors
- Resources
  - Tour
  - Books
  - Tutorials
  - Tools
  - Editors
  - IDEs
  - run.dlang.io
  - Visual D
  - Acknowledgments
  - D Style
  - Glossary
  - Sitemap

Search
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

# core.thread.fiber

The fiber module provides OS-indepedent lightweight threads aka fibers.
License:
Distributed under the Boost Software License 1.0. (See accompanying file LICENSE)
Authors:
Sean Kelly, Walter Bright, Alex Rønne Petersen, Martin Nowak

Source core/thread/fiber.d

class `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.

  Authors:
  Based on a design by Mikola Lysenko.
  Examples:

      int counter;

      class DerivedFiber : Fiber
      {
          this()
          {
              super( &run );
          }

      private :
          void run()
          {
              counter += 2;
          }
      }

      void fiberFunc()
      {
          counter += 4;
          Fiber.yield();
          counter += 8;
      }

      // create instances of each type
      Fiber derived = new DerivedFiber();
      Fiber composed = new Fiber( &fiberFunc );

      assert( counter == 0 );

      derived.call();
      assert( counter == 2, "Derived fiber increment." );

      composed.call();
      assert( counter == 6, "First composed fiber increment." );

      counter += 16;
      assert( counter == 22, "Calling context increment." );

      composed.call();
      assert( counter == 30, "Second composed fiber increment." );

      // since each fiber has run to completion, each should have state TERM
      assert( derived.state == Fiber.State.TERM );
      assert( composed.state == Fiber.State.TERM );

  enum int `defaultStackPages`;
  nothrow 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.
    Parameters:

    void function() `fn` | The fiber function.
    size_t `sz` | The stack size for this fiber.
    size_t `guardPageSize` | size of the guard page to trap fiber's stack overflows. Beware that using this will increase the number of mmaped regions on platforms using mmap so an OS-imposed limit may be hit.

    In fn must not be null.

  nothrow 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.
    Parameters:

    void delegate() `dg` | The fiber function.
    size_t `sz` | The stack size for this fiber.
    size_t `guardPageSize` | size of the guard page to trap fiber's stack overflows. Beware that using this will increase the number of mmaped regions on platforms using mmap so an OS-imposed limit may be hit.

    In dg must not be null.

  final Throwable `call`(Rethrow `rethrow` = Rethrow.yes);
  final Throwable `call`(Rethrow 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.
    Parameters:

    Rethrow `rethrow` | Rethrow any unhandled exception which may have caused this fiber to terminate.

    In This fiber must be in state HOLD.

    Throws:
    Any exception not handled by the joined thread.
    Returns:
    Any exception not handled by this fiber if rethrow = false, null otherwise.
  enum `Rethrow`: bool;
    Flag to control rethrow behavior of call
  final nothrow @nogc void `reset`();
  final nothrow @nogc void `reset`(void function() `fn`);
  final nothrow @nogc 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.

    In This fiber must be in state TERM or HOLD.

  enum `State`: int;
    A fiber may occupy one of three states: HOLD, EXEC, and TERM.
    `HOLD`
      The HOLD state applies to any fiber that is suspended and ready to be called.
    `EXEC`
      The EXEC state will be set for any fiber that is currently executing.
    `TERM`
      The TERM state is set when a fiber terminates. Once a fiber terminates, it must be reset before it may be called again.
  final const pure nothrow @nogc @property @safe State `state`();
    Gets the current state of this fiber.
    Returns:
    The state of this fiber as an enumerated value.
  static nothrow @nogc void `yield`();
    Forces a context switch to occur away from the calling fiber.
  static nothrow @nogc void `yieldAndThrow`(Throwable `t`);
    Forces a context switch to occur away from the calling fiber and then throws obj in the calling fiber.
    Parameters:

    Throwable `t` | The object to throw.

    In t must not be null.

  static nothrow @nogc @safe Fiber `getThis`();
    Provides a reference to the calling fiber or null if no fiber is currently active.
    Returns:
    The fiber object representing the calling fiber or null if no fiber is currently active within this thread. The result of deleting this object is undefined.

Copyright © 1999-2020 by the D Language Foundation | Page generated by Ddoc on Mon May 11 10:57:51 2020