diff --git a/src/fiber/execution_context.cr b/src/fiber/execution_context.cr index 7208a1b5bd10..8578983d9fea 100644 --- a/src/fiber/execution_context.cr +++ b/src/fiber/execution_context.cr @@ -56,26 +56,23 @@ 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") @@ -83,7 +80,7 @@ module Fiber::ExecutionContext # :nodoc: def self.init_default_context : Nil - @@default = Concurrent.default + @@default = Parallel.default(1) @@monitor = Monitor.new end diff --git a/src/fiber/execution_context/parallel.cr b/src/fiber/execution_context/parallel.cr index 1fa32eed5cd1..a02b6460e999 100644 --- a/src/fiber/execution_context/parallel.cr +++ b/src/fiber/execution_context/parallel.cr @@ -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 @@ -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