Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions src/fiber/execution_context.cr
Original file line number Diff line number Diff line change
Expand Up @@ -56,34 +56,31 @@ require "./execution_context/*"
#
# ## The default execution context
#
# The Crystal runtime starts with a single threaded execution context, available
# as `Fiber::ExecutionContext.default`:
# The Crystal runtime starts a default execution context exposed as
# `Fiber::ExecutionContext.default`. This is where the main fiber is running.
#
# Its parallelism is set to 1 for backwards compatibility reasons; Crystal used
# to be single-threaded and concurrent only. You can increase the parallelism at
# any time using `Parallel#resize`, for example:
#
# ```
# Fiber::ExecutionContext.default.class # => Fiber::ExecutionContext::Concurrent
# count = Fiber::ExecutionContext.default_workers_count
# Fiber::ExecutionContext.default.resize(count)
# ```
#
# NOTE: The default context is a `Concurrent` context for backwards
# compatibility reasons. It might change to a `Parallel` context in the
# future.
@[Experimental]
module Fiber::ExecutionContext
@@default : ExecutionContext?

# Returns the default `ExecutionContext` for the process, automatically
# started when the program started.
#
# NOTE: The default context is a `Concurrent` context for backwards
# compatibility reasons. It might change to a `Parallel` context in the
# future.
@[AlwaysInline]
def self.default : ExecutionContext
@@default.not_nil!("expected default execution context to have been setup")
end

# :nodoc:
def self.init_default_context : Nil
@@default = Concurrent.default
@@default = Parallel.default(1)
@@monitor = Monitor.new
end

Expand Down
8 changes: 6 additions & 2 deletions src/fiber/execution_context/parallel.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ require "./parallel/scheduler"
module Fiber::ExecutionContext
# Parallel execution context.
#
# Fibers running in the same context run both concurrently and in parallel to each
# others, in addition to the other fibers running in other execution contexts.
# Fibers running in the same context run both concurrently and in parallel to
# each others, in addition to the other fibers running in other execution
# contexts.
#
# The context internally keeps a number of fiber schedulers, each scheduler
# being able to start running on a system thread, so multiple schedulers can
Expand All @@ -18,6 +19,9 @@ module Fiber::ExecutionContext
# example not enough fibers, the schedulers will pause themselves and
# parallelism will decrease.
#
# The parallelism can be as low as 1, in which case the context becomes a
# concurrent context (no parallelism) until resized.
#
# For example: we can start a parallel context to run consumer fibers, while
# the default context produces values. Because the consumer fibers can run in
# parallel, we must protect accesses to the shared *value* variable. Running
Expand Down