lib/arachni/rpc/em/em.rb in arachni-rpc-em-0.1.1 vs lib/arachni/rpc/em/em.rb in arachni-rpc-em-0.1.2
- old
+ new
@@ -10,91 +10,56 @@
module RPC
#
# Provides some convenient methods for EventMachine's Reactor.
#
-# @author: Tasos "Zapotek" Laskos
-# <tasos.laskos@gmail.com>
-# <zapotek@segfault.gr>
-# @version: 0.1
+# @author: Tasos "Zapotek" Laskos <tasos.laskos@gmail.com>
#
module EM
-module Synchrony
+ module Synchrony
+ def run( &block )
+ Fiber.new{ block.call }.resume
+ end
- def run( &block )
- @@root_f = Fiber.new {
- block.call
- }.resume
+ extend self
end
- extend self
-
-end
-
#
- # Inits method variables for the Reactor tasks and its Mutex.
+ # Schedules a block to be run in the EM reactor.
#
- def init
- @@reactor_tasks_mutex ||= Mutex.new
- @@reactor_tasks ||= []
- end
-
+ # @param [Proc] &block
#
- # Adds a block in the Reactor.
- #
- # @param [Proc] &block block to be included in the Reactor loop
- #
- def add_to_reactor( &block )
-
- self.init
-
- # if we're already in the Reactor thread just run the block straight up.
- if ::EM::reactor_thread?
- block.call
- else
- @@reactor_tasks_mutex.lock
- @@reactor_tasks << block
-
- ensure_em_running!
- @@reactor_tasks_mutex.unlock
- end
-
+ def schedule( &block )
+ ensure_em_running
+ ::EM.schedule( &block )
end
#
# Blocks until the Reactor stops running
#
- def block!
+ def block
# beware of deadlocks, we can't join our own thread
::EM.reactor_thread.join if ::EM.reactor_thread && !::EM::reactor_thread?
end
#
# Puts the Reactor in its own thread and runs it.
#
- # It also runs all blocks sent to {#add_to_reactor}.
- #
- def ensure_em_running!
- self.init
-
+ def ensure_em_running
if !::EM::reactor_running?
- q = Queue.new
Thread.new do
::EM::run do
-
::EM.error_handler do |e|
$stderr.puts "Exception raised during event loop: " +
"#{e.message} (#{e.class})\n#{(e.backtrace ||
[])[0..5].join("\n")}"
end
-
- @@reactor_tasks.each { |task| task.call }
- q << true
end
end
- q.pop
+
+ sleep 0.1 while !::EM.reactor_running?
end
end
extend self
end