Module: EventMachine

Defined in:
lib/ext/eventmachine.rb

Class Method Summary collapse

Class Method Details

.barrier(&blk) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ext/eventmachine.rb', line 56

def self.barrier(&blk)
  # Presumably, Thread.main will return the EM main loop thread.
  if EM.reactor_thread?
    return blk.call
  end

  raise "Eventmachine is not ready to accept the next_tick() call." unless self.reactor_running?

  q = ::Queue.new
  time_start = ::Time.now

  self.next_tick {
  #self.add_timer(0) {
    begin
      res = blk.call
      q << [true, res]
    rescue => e
      q << [false, e]
    end
  }
  
  res = q.shift
  time_elapsed = ::Time.now - time_start
  Wakame.log.debug("EM.barrier: elapsed time for #{blk}: #{time_elapsed} sec (#{$eventmachine_library})") if time_elapsed > 0.05
  if res[0] == false && res[1].is_a?(Exception)
    raise res[1]
  end
  res[1]
end

.spawn_threadpoolObject

Redefine EM’s threadpool



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ext/eventmachine.rb', line 37

def self.spawn_threadpool
  until @threadpool.size == 30
    thread = Thread.new {
      loop {
        op, cback = *@threadqueue.pop
        begin
          result = op.call
          @resultqueue << [result, cback]
        rescue => e
          puts "#{e} in EM defer thread pool : #{Thread.current}"
        ensure
          EventMachine.signal_loopbreak
        end
      }
    }
    @threadpool << thread
  end
end