222: def EventMachine::run blk=nil, tail=nil, &block
223: @tails ||= []
224: tail and @tails.unshift(tail)
225:
226: if reactor_running?
227: (b = blk || block) and b.call
228: else
229: @conns = {}
230: @acceptors = {}
231: @timers = {}
232: @wrapped_exception = nil
233: begin
234: @reactor_running = true
235: initialize_event_machine
236: (b = blk || block) and add_timer(0, b)
237: if @next_tick_queue && !@next_tick_queue.empty?
238: add_timer(0) { signal_loopbreak }
239: end
240: run_machine
241: ensure
242: begin
243: release_machine
244: ensure
245: if @threadpool
246: @threadpool.each { |t| t.exit }
247: @threadpool.each { |t| t.kill! if t.alive? }
248: @threadqueue = nil
249: @resultqueue = nil
250: end
251: @threadpool = nil
252: end
253: @reactor_running = false
254: end
255:
256: until @tails.empty?
257: @tails.pop.call
258: end
259:
260: raise @wrapped_exception if @wrapped_exception
261: end
262: end