lib/murasaki/event_loop.rb in murasaki-0.2.1.1 vs lib/murasaki/event_loop.rb in murasaki-0.2.2
- old
+ new
@@ -13,10 +13,12 @@
@ios = Hash.new
# IO queue
@queue = Hash.new
# Root fiber
@root_fiber = Fiber.current
+ # Thread for timer
+ @timer_thread = nil
nil
end
end
end
@@ -93,11 +95,10 @@
# @return [nil] nil
def run_once
@selector.select(0.2) do |monitor| # Timeout for 0.2 secs
@ios[monitor.io][:callback].call(monitor)
end
- timer_once
nil
end
# Run timer once
# @return [nil] nil
@@ -116,19 +117,21 @@
# Start the event loop
# @return [nil] nil
def start
return if running?
@stop = false
+ @timer_thread = Thread.new { loop { sleep 0.5; timer_once }}
until @stop
run_once
end
@stop = nil
end
# Set the stop flag
# @return [nil] nil
def stop
@stop = true
+ Thread.kill(@timer_thread)
nil
end
# Detect the stop flag
# @return [Boolean] return if eventloop is set to be stopped