Class: Actuator::ConditionVariable

Inherits:
Object
  • Object
show all
Defined in:
lib/actuator/mutex.rb

Instance Method Summary collapse

Constructor Details

#initializeConditionVariable

Returns a new instance of ConditionVariable.



77
78
79
# File 'lib/actuator/mutex.rb', line 77

def initialize
  @waiters = []
end

Instance Method Details

#broadcastObject



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/actuator/mutex.rb', line 103

def broadcast
  waiters = @waiters
  @waiters = []
  waiters.each do |waiter|
    job = waiter[1]
    next unless job.alive?
    Actuator.next_tick do
      waiter[0]._wake_up(job) if job.alive?
    end
  end
  self
end

#signalObject



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/actuator/mutex.rb', line 91

def signal
  while waiter = @waiters.shift
    job = waiter[1]
    next unless job.alive?
    Actuator.next_tick do
      waiter[0]._wake_up(job) if job.alive?
    end
    break
  end
  self
end

#wait(mutex, timeout = nil) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/actuator/mutex.rb', line 81

def wait(mutex, timeout=nil)
  job = Job.current
  waiter = [mutex, job]
  @waiters << waiter
  mutex.sleep timeout
  self
ensure
  @waiters.delete waiter
end