module Fiber::ExecutionContext

Overview

An execution context creates and manages a dedicated pool of 1 or more threads where fibers can be executed into. Each context manages the rules to run, suspend and swap fibers internally.

EXPERIMENTAL Execution contexts are an experimental feature, implementing RFC 2. It's opt-in and requires the compiler flags -Dpreview_mt -Dexecution_context.

Applications can create any number of execution contexts in parallel. These contexts are isolated but they can communicate with the usual thread-safe synchronization primitives (e.g. Channel, Mutex).

An execution context groups fibers together. Instead of associating a fiber to a specific thread, we associate a fiber to an execution context, abstracting which thread(s) they actually run on.

When spawning a fiber with ::spawn, it spawns into the execution context of the current fiber. Thus child fibers execute in the same context as their parent (unless told otherwise).

Once spawned, a fiber cannot move to another execution context. It always resumes in the same execution context.

Context types

The standard library provides a number of execution context implementations for common use cases.

The default execution context

The Crystal runtime starts with a single threaded execution context, available in Fiber::ExecutionContext.default.

Fiber::ExecutionContext.default.class # => Fiber::ExecutionContext::SingleThreaded

NOTE The single threaded default context is required for backwards compatibility. It may change to a multi-threaded default context in the future.

EXPERIMENTAL

Direct including types

Defined in:

fiber/execution_context.cr
fiber/execution_context/global_queue.cr
fiber/execution_context/isolated.cr
fiber/execution_context/monitor.cr
fiber/execution_context/multi_threaded.cr
fiber/execution_context/multi_threaded/scheduler.cr
fiber/execution_context/runnables.cr
fiber/execution_context/scheduler.cr
fiber/execution_context/single_threaded.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.current : ExecutionContext #

Returns the ExecutionContext the current fiber is running in.


[View source]
def self.default : ExecutionContext #

Returns the default ExecutionContext for the process, automatically started when the program started.

NOTE The default context is a SingleThreaded context for backwards compatibility reasons. It may change to a multi-threaded default context in the future.


[View source]

Class Method Detail

def self.default_workers_count : Int32 #

Returns the number of threads to start in the default multi threaded execution context. Respects the CRYSTAL_WORKERS environment variable and otherwise returns the potential parallelism of the CPU (number of hardware threads).

Currently unused because the default context is single threaded for now (this might change later with compilation flags).


[View source]
def self.each(&) : Nil #

Iterates all execution contexts.


[View source]

Instance Method Detail

def spawn(*, name : String | Nil = nil, &block : -> ) : Fiber #

Creates a new fiber then calls #enqueue to add it to the execution context.

May be called from any ExecutionContext (i.e. must be thread-safe).


[View source]