def self.run blk=nil, tail=nil, &block
if reactor_running? and @reactor_pid != Process.pid
stop_event_loop
release_machine
@reactor_running = false
end
tail and @tails.unshift(tail)
if reactor_running?
(b = blk || block) and b.call
else
@conns = {}
@acceptors = {}
@timers = {}
@wrapped_exception = nil
@next_tick_queue ||= []
@tails ||= []
begin
@reactor_pid = Process.pid
@reactor_running = true
initialize_event_machine
(b = blk || block) and add_timer(0, b)
if @next_tick_queue && !@next_tick_queue.empty?
add_timer(0) { signal_loopbreak }
end
@reactor_thread = Thread.current
run_machine
ensure
until @tails.empty?
@tails.pop.call
end
begin
release_machine
ensure
if @threadpool
@threadpool.each { |t| t.exit }
@threadpool.each do |t|
next unless t.alive?
begin
t.kill!
rescue NoMethodError, NotImplementedError
t.kill
end
end
@threadqueue = nil
@resultqueue = nil
@threadpool = nil
@all_threads_spawned = false
end
@next_tick_queue = []
end
@reactor_running = false
@reactor_thread = nil
end
raise @wrapped_exception if @wrapped_exception
end
end