Class: Shikibu::Notify::WakeEvent
- Inherits:
-
Object
- Object
- Shikibu::Notify::WakeEvent
- Defined in:
- lib/shikibu/notify/wake_event.rb
Overview
Thread-safe wake event for interrupting sleep with NOTIFY signals Uses ConditionVariable for efficient signaling between threads
Instance Method Summary collapse
-
#clear ⇒ Object
Clear any pending signal without waiting.
-
#initialize ⇒ WakeEvent
constructor
A new instance of WakeEvent.
-
#signal ⇒ Object
Signal the wake event (non-blocking) Wakes up any thread waiting on this event.
-
#signaled? ⇒ Boolean
Check if there is a pending signal.
-
#wait(timeout_seconds) ⇒ Boolean
Wait for signal with timeout.
Constructor Details
#initialize ⇒ WakeEvent
Returns a new instance of WakeEvent.
8 9 10 11 12 |
# File 'lib/shikibu/notify/wake_event.rb', line 8 def initialize @mutex = Mutex.new @condition = ConditionVariable.new @signaled = false end |
Instance Method Details
#clear ⇒ Object
Clear any pending signal without waiting
45 46 47 |
# File 'lib/shikibu/notify/wake_event.rb', line 45 def clear @mutex.synchronize { @signaled = false } end |
#signal ⇒ Object
Signal the wake event (non-blocking) Wakes up any thread waiting on this event
16 17 18 19 20 21 |
# File 'lib/shikibu/notify/wake_event.rb', line 16 def signal @mutex.synchronize do @signaled = true @condition.signal end end |
#signaled? ⇒ Boolean
Check if there is a pending signal
51 52 53 |
# File 'lib/shikibu/notify/wake_event.rb', line 51 def signaled? @mutex.synchronize { @signaled } end |
#wait(timeout_seconds) ⇒ Boolean
Wait for signal with timeout
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/shikibu/notify/wake_event.rb', line 26 def wait(timeout_seconds) @mutex.synchronize do if @signaled @signaled = false return true end @condition.wait(@mutex, timeout_seconds) if @signaled @signaled = false true else false end end end |