Class: Amigo::ThreadingEvent

Inherits:
Object
  • Object
show all
Defined in:
lib/amigo/threading_event.rb

Overview

Threading event on Concurrent::Event, ManualResetEvent, etc. Efficient way to sleep and wake up.

Instance Method Summary collapse

Constructor Details

#initialize(initial = false) ⇒ ThreadingEvent

Returns a new instance of ThreadingEvent.



7
8
9
10
11
# File 'lib/amigo/threading_event.rb', line 7

def initialize(initial=false)
  @mutex = Mutex.new
  @cv = ConditionVariable.new
  @signaled = initial
end

Instance Method Details

#resetObject

Reset the event back to its original state.



36
37
38
39
40
41
# File 'lib/amigo/threading_event.rb', line 36

def reset
  #       _debug("reset")
  @mutex.synchronize do
    @signaled = false
  end
end

#setObject

Signal the event. The waiting threads will wake up.



24
25
26
27
28
29
30
# File 'lib/amigo/threading_event.rb', line 24

def set
  #       _debug("set")
  @mutex.synchronize do
    @signaled = true
    @cv.broadcast # wake up all waiters
  end
end

#set?Boolean

True if set has been called.

Returns:

  • (Boolean)


33
# File 'lib/amigo/threading_event.rb', line 33

def set? = @signaled

#wait(timeout = nil) ⇒ Object

Sleep the current thread until set is called by another thread.

Parameters:

  • timeout (Numeric, nil) (defaults to: nil)

    Passed to Mutex#sleep.

Returns:

  • See Mutex#sleep.



16
17
18
19
20
21
# File 'lib/amigo/threading_event.rb', line 16

def wait(timeout=nil)
  # _debug("wait")
  @mutex.synchronize do
    @cv.wait(@mutex, timeout)
  end
end