| Class | MonitorMixin::ConditionVariable |
| In: |
lib/monitor.rb
|
| Parent: | Object |
FIXME: This isn‘t documented in Nutshell.
Since MonitorMixin.new_cond returns a ConditionVariable, and the example above calls while_wait and signal, this class should be documented.
# File lib/monitor.rb, line 162
162: def initialize(monitor)
163: @monitor = monitor
164: @waiters = []
165: end
Wake up all the waiters.
# File lib/monitor.rb, line 145
145: def broadcast
146: @monitor.instance_eval {mon_check_owner()}
147: Thread.critical = true
148: for t in @waiters
149: t.wakeup
150: end
151: @waiters.clear
152: Thread.critical = false
153: Thread.pass
154: end
Wake up and run the next waiter
# File lib/monitor.rb, line 135
135: def signal
136: @monitor.instance_eval {mon_check_owner()}
137: Thread.critical = true
138: t = @waiters.shift
139: t.wakeup if t
140: Thread.critical = false
141: Thread.pass
142: end
Create a new timer with the argument timeout, and add the current thread to the list of waiters. Then the thread is stopped. It will be resumed when a corresponding signal occurs.
# File lib/monitor.rb, line 93
93: def wait(timeout = nil)
94: @monitor.instance_eval {mon_check_owner()}
95: timer = create_timer(timeout)
96:
97: Thread.critical = true
98: count = @monitor.instance_eval {mon_exit_for_cond()}
99: @waiters.push(Thread.current)
100:
101: begin
102: Thread.stop
103: return true
104: rescue Timeout
105: return false
106: ensure
107: Thread.critical = true
108: if timer && timer.alive?
109: Thread.kill(timer)
110: end
111: if @waiters.include?(Thread.current) # interrupted?
112: @waiters.delete(Thread.current)
113: end
114: @monitor.instance_eval {mon_enter_for_cond(count)}
115: Thread.critical = false
116: end
117: end