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 165 165: def initialize(monitor) 166: @monitor = monitor 167: @waiters = [] 168: end
Wake up all the waiters.
# File lib/monitor.rb, line 148 148: def broadcast 149: @monitor.instance_eval {mon_check_owner()} 150: Thread.critical = true 151: for t in @waiters 152: t.wakeup 153: end 154: @waiters.clear 155: Thread.critical = false 156: Thread.pass 157: end
Wake up and run the next waiter
# File lib/monitor.rb, line 138 138: def signal 139: @monitor.instance_eval {mon_check_owner()} 140: Thread.critical = true 141: t = @waiters.shift 142: t.wakeup if t 143: Thread.critical = false 144: Thread.pass 145: 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: begin 109: if timer && timer.alive? 110: Thread.kill(timer) 111: end 112: if @waiters.include?(Thread.current) # interrupted? 113: @waiters.delete(Thread.current) 114: end 115: @monitor.instance_eval {mon_enter_for_cond(count)} 116: ensure 117: Thread.critical = false 118: end 119: end 120: end