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.

Methods

Classes and Modules

Class MonitorMixin::ConditionVariable::Timeout

Public Class methods

[Source]

     # File lib/monitor.rb, line 165
165:     def initialize(monitor)
166:       @monitor = monitor
167:       @waiters = []
168:     end

Public Instance methods

Wake up all the waiters.

[Source]

     # 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

[Source]

     # File lib/monitor.rb, line 159
159:     def count_waiters
160:       return @waiters.length
161:     end

Wake up and run the next waiter

[Source]

     # 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.

[Source]

     # 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

call wait until the supplied block returns true.

[Source]

     # File lib/monitor.rb, line 131
131:     def wait_until
132:       until yield
133:         wait
134:       end
135:     end

call wait while the supplied block returns true.

[Source]

     # File lib/monitor.rb, line 124
124:     def wait_while
125:       while yield
126:         wait
127:       end
128:     end

Private Instance methods

[Source]

     # File lib/monitor.rb, line 170
170:     def create_timer(timeout)
171:       if timeout
172:         waiter = Thread.current
173:         return Thread.start {
174:           Thread.pass
175:           sleep(timeout)
176:           Thread.critical = true
177:           waiter.raise(Timeout.new)
178:         }
179:       else
180:         return nil
181:       end
182:     end

[Validate]