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 162
162:     def initialize(monitor)
163:       @monitor = monitor
164:       @waiters = []
165:     end

Public Instance methods

Wake up all the waiters.

[Source]

     # 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

[Source]

     # File lib/monitor.rb, line 156
156:     def count_waiters
157:       return @waiters.length
158:     end

Wake up and run the next waiter

[Source]

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

[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:         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

call wait until the supplied block returns true.

[Source]

     # File lib/monitor.rb, line 128
128:     def wait_until
129:       until yield
130:         wait
131:       end
132:     end

call wait while the supplied block returns true.

[Source]

     # File lib/monitor.rb, line 121
121:     def wait_while
122:       while yield
123:         wait
124:       end
125:     end

Private Instance methods

[Source]

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

[Validate]