Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
34 changes: 14 additions & 20 deletions src/fiber/execution_context.cr
Original file line number Diff line number Diff line change
Expand Up @@ -56,46 +56,40 @@ 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)
Comment thread
ysbaddaden marked this conversation as resolved.
# ```
#
# 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

# 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).
# Returns the number of threads to start in the default parallel execution
# context. Respects the `CRYSTAL_WORKERS` environment variable, if present and
# valid, and otherwise defaults to the number of CPUs detected on the running
# computer.
def self.default_workers_count : Int32
ENV["CRYSTAL_WORKERS"]?.try(&.to_i?) || Math.min(System.cpu_count.to_i, 32)
ENV["CRYSTAL_WORKERS"]?.try(&.to_i?) || Math.max(System.cpu_count.to_i, 32)
Comment thread
ysbaddaden marked this conversation as resolved.
Outdated
end

# :nodoc:
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