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