Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add :max_threads(*|Inf) as option to ThreadPoolScheduler #4931

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 19 additions & 13 deletions src/core.c/ThreadPoolScheduler.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,8 @@ my class ThreadPoolScheduler does Scheduler {
}

# Initial and maximum threads allowed.
has uint $.initial_threads;
has uint $.max_threads;
has uint $.initial_threads is built(False);
has uint $!max_threads;

# All of the worker and queue state below is guarded by this lock.
has Lock $!state-lock = Lock.new;
Expand Down Expand Up @@ -772,13 +772,18 @@ my class ThreadPoolScheduler does Scheduler {
}
}

submethod BUILD(
Int() :$!initial_threads = 0,
Int() :$!max_threads =
%*ENV<RAKUDO_MAX_THREADS> // (Kernel.cpu-cores * 8 max 64)
--> Nil) {
submethod TWEAK(:$initial_threads, :$max_threads --> Nil) {
$!initial_threads = .Int with $initial_threads;
$!max_threads = nqp::istype($max_threads,Whatever)
?? 9223372036854775807 # XXX should be -1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a suggestion. Perhaps, make the number a constant?

!! $max_threads.defined
?? $max_threads == Inf
?? 9223372036854775807 # XXX should be -1
!! $max_threads.Int
!! (%*ENV<RAKUDO_MAX_THREADS> // (Kernel.cpu-cores * 8 max 64)).Int;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also just a thought. Maybe set to "infinite" if the environment is -1 or just any negative? Could be helpful in detecting some problems too.


die "Initial thread pool threads ($!initial_threads) must be less than or equal to maximum threads ($!max_threads)"
if $!initial_threads > $!max_threads;
if $!initial_threads > $!max_threads;

$!general-workers := nqp::create(IterationBuffer);
$!timer-workers := nqp::create(IterationBuffer);
Expand All @@ -787,13 +792,10 @@ my class ThreadPoolScheduler does Scheduler {
if $!initial_threads > 0 {
# We've been asked to make some initial threads; we interpret this
# as general workers.
$!general-queue := nqp::create(Queue);
$!general-queue := nqp::create(Queue);
nqp::push(
$!general-workers,
GeneralWorker.new(
queue => $!general-queue,
scheduler => self
)
GeneralWorker.new(queue => $!general-queue, scheduler => self)
) for ^$!initial_threads;
scheduler-debug "Created scheduler with $!initial_threads initial general workers";
self!maybe-start-supervisor();
Expand All @@ -803,6 +805,10 @@ my class ThreadPoolScheduler does Scheduler {
}
}

method max_threads(ThreadPoolScheduler:D:) {
$!max_threads == 9223372036854775807 ?? Inf !! $!max_threads
}

method queue(Bool :$hint-time-sensitive, :$hint-affinity) {
if $hint-affinity {
self!affinity-queue()
Expand Down