Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #140 from ibc/master

EM.synchrony refactorized
  • Loading branch information...
commit a8b6b8bf2b3cc77eb256b0258d32c54bdf50f5e8 2 parents ed76f2a + 9d48683
@igrigorik authored
Showing with 20 additions and 5 deletions.
  1. +20 −5 lib/em-synchrony.rb
View
25 lib/em-synchrony.rb
@@ -18,14 +18,29 @@
module EventMachine
- # A convenience method for wrapping EM.run body within
+ # A convenience method for wrapping a given block within
# a Ruby Fiber such that async operations can be transparently
# paused and resumed based on IO scheduling.
- def self.synchrony(blk=nil, tail=nil, &block)
- blk ||= block
- context = Proc.new { Fiber.new { blk.call }.resume }
+ # It detects whether EM is running or not.
+ def self.synchrony(blk=nil, tail=nil)
+ # EM already running.
+ if reactor_running?
+ if block_given?
+ Fiber.new { yield }.resume
+ else
+ Fiber.new { blk.call }.resume
+ end
+ tail && add_shutdown_hook(tail)
+
+ # EM not running.
+ else
+ if block_given?
+ run(nil, tail) { Fiber.new { yield }.resume }
+ else
+ run(Proc.new { Fiber.new { blk.call }.resume }, tail)
+ end
- self.run(context, tail)
+ end
end
module Synchrony
Please sign in to comment.
Something went wrong with that request. Please try again.