@@ -8,12 +8,73 @@ require "./execution_context/*"
8
8
{% raise " ERROR: execution contexts require the `preview_mt` compilation flag" unless flag?(:preview_mt ) || flag?(:docs ) % }
9
9
{% raise " ERROR: execution contexts require the `execution_context` compilation flag" unless flag?(:execution_context ) || flag?(:docs ) % }
10
10
11
+ # An execution context creates and manages a dedicated pool of 1 or more threads
12
+ # where fibers can be executed into. Each context manages the rules to run,
13
+ # suspend and swap fibers internally.
14
+ #
15
+ # EXPERIMENTAL: Execution contexts are an experimental feature, implementing
16
+ # [RFC 2](https://github.com/crystal-lang/rfcs/pull/2). It's opt-in and requires
17
+ # the compiler flags `-Dpreview_mt -Dexecution_context`.
18
+ #
19
+ # Applications can create any number of execution contexts in parallel. These
20
+ # contexts are isolated but they can communicate with the usual thread-safe
21
+ # synchronization primitives (e.g. `Channel`, `Mutex`).
22
+ #
23
+ # An execution context groups fibers together. Instead of associating a fiber to
24
+ # a specific thread, we associate a fiber to an execution context, abstracting
25
+ # which thread(s) they actually run on.
26
+ #
27
+ # When spawning a fiber with `::spawn`, it spawns into the execution context of
28
+ # the current fiber. Thus child fibers execute in the same context as their
29
+ # parent (unless told otherwise).
30
+ #
31
+ # Once spawned, a fiber cannot _move_ to another execution context. It always
32
+ # resumes in the same execution context.
33
+ #
34
+ # ## Context types
35
+ #
36
+ # The standard library provides a number of execution context implementations
37
+ # for common use cases.
38
+ #
39
+ # * `ExecutionContext::SingleThreaded`: Fully concurrent with limited
40
+ # parallelism. Fibers run concurrently in a single thread and never in parallel.
41
+ # They can use simpler and faster synchronization primitives internally (no
42
+ # atomics, no thread safety). Communication with fibers in other contexts
43
+ # requires thread-safe primitives. A blocking fiber blocks the entire thread and
44
+ # all other fibers in the context.
45
+ # * `ExecutionContext::MultiThreaded`: Fully concurrent, fully parallel. Fibers
46
+ # running in this context can be resumed by any thread in this context. They run
47
+ # concurrently and in parallel to each other, in addition to running in parallel
48
+ # to any fibers in other contexts. Schedulers steal work from each other. The
49
+ # number of threads can grow and shrink dynamically.
50
+ # * `ExecutionContext::Isolated`: Single fiber in a single thread without
51
+ # concurrency. This is useful for tasks that can block thread execution for a
52
+ # long time (e.g. a GUI main loop, a game loop, or CPU heavy computation). The
53
+ # event-loop works normally (when the fiber sleeps, it pauses the thread).
54
+ # Communication with fibers in other contexts requires thread-safe primitives.
55
+ #
56
+ # ## The default execution context
57
+ #
58
+ # The Crystal runtime starts with a single threaded execution context, available
59
+ # in `Fiber::ExecutionContext.default`.
60
+ #
61
+ # ```
62
+ # Fiber::ExecutionContext.default.class # => Fiber::ExecutionContext::SingleThreaded
63
+ # ```
64
+ #
65
+ # NOTE: The single threaded default context is required for backwards
66
+ # compatibility. It may change to a multi-threaded default context in the
67
+ # future.
11
68
@[Experimental ]
12
69
module Fiber::ExecutionContext
13
70
@@default : ExecutionContext ?
14
71
15
72
# Returns the default `ExecutionContext` for the process, automatically
16
73
# started when the program started.
74
+ #
75
+ # NOTE: The default context is a `SingleThreaded` context for backwards
76
+ # compatibility reasons. It may change to a multi-threaded default context in
77
+ # the future.
17
78
@[AlwaysInline ]
18
79
def self.default : ExecutionContext
19
80
@@default .not_nil!(" expected default execution context to have been setup" )
0 commit comments