pub struct Semaphore { /* private fields */ }Expand description
Counting semaphore built on Mutex + Condvar so blocked acquirers park in
the kernel rather than spinning on an atomic.
Replaces the prior pattern in pipeline::chunked::exec:
while atomic.load(Relaxed) >= max { thread::sleep(50ms); }
atomic.fetch_add(1, Relaxed);which polled 20 times/sec per blocked worker. Under N parallel chunks and a long-running export this burned ~N × 20 wake-ups per second doing nothing.
With Condvar::wait, blocked threads consume zero CPU until a release()
notifies. The mutex is uncontended whenever traffic is light, so the lock
path adds no measurable overhead vs the atomic.
The permit ceiling is mutable at runtime via Semaphore::resize so the
OPT-2 concurrency governor can back parallelism off (and recover it) under
source pressure without tearing down the worker pool.
Implementations§
Source§impl Semaphore
impl Semaphore
pub fn new(max: usize) -> Self
Sourcepub fn resize(&self, new_max: usize)
pub fn resize(&self, new_max: usize)
Change the permit ceiling at runtime.
Raising it wakes every parked acquirer so the freshly available permits
are taken promptly. Lowering it takes effect lazily: in-flight permits
are honored to completion, and new acquire() calls block until count
falls below the new ceiling. The caller is responsible for keeping
new_max >= 1 (a 0 ceiling parks all acquirers indefinitely).