Class: Shikibu::Notify::WakeEvent

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeWakeEvent

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

#clearObject

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

#signalObject

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

Returns:

  • (Boolean)

    true if signaled



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

Parameters:

  • timeout_seconds (Numeric)

    Maximum time to wait in seconds

Returns:

  • (Boolean)

    true if signaled, false if 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