class QuackConcurrency::Waiter
{Waiter} is similar to {ConditionVariable}.
A few differences include:
-
the ability to force any future request to {#wait} to return immediately
-
every call to {#wait} can only be resumed via the {Waiter} (not with +Thread#run+, +Thread#wakeup+, etc.)
-
{#wait} does not accept a mutex
-
some methods have been renamed to be more intuitive
@api private
Public Class Methods
Creates a new {Waiter} concurrency tool. @return [Waiter]
# File lib/quack_concurrency/waiter.rb, line 16 def initialize @condition_variable = SafeConditionVariable.new @resume_all_indefinitely = false @mutex = ::Mutex.new end
Public Instance Methods
Checks if any threads are waiting on it. @return [Boolean]
# File lib/quack_concurrency/waiter.rb, line 24 def any_waiting_threads? @condition_variable.any_waiting_threads? end
Resumes all threads waiting on it. @return [void]
# File lib/quack_concurrency/waiter.rb, line 30 def resume_all @condition_variable.broadcast end
Resumes all threads waiting on it and will cause
any future calls to {#wait} to return immediately.
@return [void]
# File lib/quack_concurrency/waiter.rb, line 37 def resume_all_indefinitely @mutex.synchronize do @resume_all_indefinitely = true resume_all end end
Resumes next thread waiting on it if one exists. @return [void]
# File lib/quack_concurrency/waiter.rb, line 46 def resume_next @condition_variable.signal end
Puts this thread to sleep until another thread resumes it via this {Waiter}. @note Will block until resumed. @return [void]
# File lib/quack_concurrency/waiter.rb, line 53 def wait @mutex.synchronize do return if @resume_all_indefinitely @condition_variable.wait(@mutex) end end
Returns the number of threads waiting on it. @return [Integer]
# File lib/quack_concurrency/waiter.rb, line 62 def waiting_threads_count @condition_variable.waiting_threads_count end