File: kernel.rb

package info (click to toggle)
ruby-em-synchrony 1.0.5-3.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 572 kB
  • sloc: ruby: 3,458; sh: 37; sql: 7; makefile: 2
file content (27 lines) | stat: -rw-r--r-- 614 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# encoding: UTF-8
require 'em-synchrony'

# Monkey-patch
module Kernel
  alias_method :orig_sleep, :sleep

  class << self
    attr_accessor :em_synchrony_sleep_hook
  end

  # Monkey-patch
  def sleep(sleep_time)
    if Kernel.em_synchrony_sleep_hook &&
       EM.reactor_thread? &&
       !Thread.current[:em_synchrony_sleep_hook_called]
      begin
        Thread.current[:em_synchrony_sleep_hook_called] = true
        Kernel.em_synchrony_sleep_hook.call(sleep_time)
      ensure
        Thread.current[:em_synchrony_sleep_hook_called] = false
      end
    else
      orig_sleep(sleep_time)
    end
  end
end