Class: QuackConcurrency::Sleeper
- Inherits:
-
Object
- Object
- QuackConcurrency::Sleeper
- Defined in:
- lib/quack_concurrency/sleeper.rb
Overview
Direct Known Subclasses
Instance Method Summary collapse
-
#initialize ⇒ Sleeper
constructor
Creates a new Sleeper concurrency tool.
-
#sleep(timeout = nil) ⇒ Float
Puts this thread to sleep.
-
#wake ⇒ void
Wake it’s sleeping thread, if one exists.
Constructor Details
#initialize ⇒ Sleeper
Creates a new QuackConcurrency::Sleeper concurrency tool.
10 11 12 13 14 15 16 |
# File 'lib/quack_concurrency/sleeper.rb', line 10 def initialize @state = :initial @mutex = ::Mutex.new @condition_variable = ::ConditionVariable.new @sleep_called = false @wake_called = false end |
Instance Method Details
#sleep(timeout = nil) ⇒ Float
Puts this thread to sleep. Will be skipped if #wake has already been called. If called without a timeout it will sleep forever. It can only be called once.
28 29 30 31 32 33 34 35 36 |
# File 'lib/quack_concurrency/sleeper.rb', line 28 def sleep(timeout = nil) timeout = process_timeout(timeout) enforce_sleep_call_limit @mutex.synchronize do timer do @condition_variable.wait(@mutex, timeout) unless @wake_called end end end |
#wake ⇒ void
This method returns an undefined value.
Wake it’s sleeping thread, if one exists. It can only be called once.
42 43 44 45 46 47 48 |
# File 'lib/quack_concurrency/sleeper.rb', line 42 def wake @mutex.synchronize do enforce_wake_call_limit @condition_variable.signal end nil end |