Class: Concurrent::Selectable::Latch

Inherits:
Base
  • Object
show all
Defined in:
lib/concurrent/selectable/latch.rb

Overview

A latch is a toggle which starts out unset. Threads can wait for the latch to become set by calling Latch#wait (inherited from Base). The latch can be reset to its intial state using Latch#reset.

Instance Method Summary collapse

Methods inherited from Base

#close, #to_io, #wait

Constructor Details

#initializeLatch

Returns a new instance of Latch.



45
46
47
48
49
# File 'lib/concurrent/selectable/latch.rb', line 45

def initialize
  super()
  @lock = ::Mutex.new
  @set = false
end

Instance Method Details

#resetObject

Resets the latch, blocking any subsequent waiting threads until the latch is set again



70
71
72
73
74
75
76
# File 'lib/concurrent/selectable/latch.rb', line 70

def reset
  @lock.synchronize do
    internal_reset if @set
    @set = false
  end
  self
end

#setObject

Sets the latch, allowing any current and future waiting threads to continue



53
54
55
56
57
58
59
# File 'lib/concurrent/selectable/latch.rb', line 53

def set
  @lock.synchronize do
    internal_set unless @set
    @set = true
  end
  self
end

#set?Boolean

Non-blocking test whether the latch is currently set (additional external synchronization will usually be required if the latch can be set or reset by other threads)

Returns:

  • (Boolean)


64
65
66
# File 'lib/concurrent/selectable/latch.rb', line 64

def set?
  @lock.synchronize { @set }
end