From 600da2b97e937b9c96791c291cb5e08cd8526bdd Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 7 Aug 2020 08:57:01 +0200 Subject: Add ability to allocate additional active threads to current thread --- libbuild2/scheduler.cxx | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) (limited to 'libbuild2/scheduler.cxx') diff --git a/libbuild2/scheduler.cxx b/libbuild2/scheduler.cxx index c660bba..03842ec 100644 --- a/libbuild2/scheduler.cxx +++ b/libbuild2/scheduler.cxx @@ -144,8 +144,8 @@ namespace build2 if (collision) stat_wait_collisions_++; - // If we have spare active threads, then become active. Otherwise it - // enters the ready queue. + // If we have spare active threads, then become active. Otherwise we enter + // the ready queue. // if (external) external_--; @@ -186,6 +186,36 @@ namespace build2 } size_t scheduler:: + allocate (size_t n) + { + if (max_active_ == 1) // Serial execution. + return 0; + + lock l (mutex_); + + if (active_ < max_active_) + { + size_t d (max_active_ - active_); + if (n == 0 || d < n) + n = d; + active_ -= n; + return n; + } + else + return 0; + } + + void scheduler:: + deallocate (size_t n) + { + if (max_active_ == 1) // Serial execution. + return; + + lock l (mutex_); + active_ += n; + } + + size_t scheduler:: suspend (size_t start_count, const atomic_count& task_count) { wait_slot& s ( -- cgit v1.1