Sha256: 150bd608b01f334b4ed3ab2e62cf57f61ec6ec19a307b92717aee1759edb5787

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

require "timeout/extensions/version"

# Core extensions to Thread
#
# Adds accessors to customize timeout and sleep within a thread,
# if you have a better friendlier implementation of both methods,
# or if your code breaks with the stdlib implementations.
#
class Thread
  attr_accessor :timeout_handler
  attr_accessor :sleep_handler
end

module Timeout::Extensions
  module TimeoutMethods
    def timeout(*args, &block)
      if (timeout_handler = Thread.current.timeout_handler)
        timeout_handler.call(*args, &block)
      else
        super
      end
    end
  end

  module KernelMethods
    def sleep(*args)
      if (sleep_handler = Thread.current.sleep_handler)
        sleep_handler.call(*args)
      else
        super
      end
    end
  end

  # in order for prepend to work, I have to do it in the Timeout module singleton class
  class << ::Timeout
    prepend TimeoutMethods
  end

  # ditto for Kernel
  class << ::Kernel
    prepend KernelMethods
  end

  # this is an hack so that calling "sleep(2)" works. Amazingly, the message doesn't get
  # sent to Kernel.sleep code path.
  # https://bugs.ruby-lang.org/issues/12535
  #
  ::Object.prepend KernelMethods
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
timeout-extensions-0.1.0 lib/timeout/extensions.rb