Sha256: 62f6bef3eb21a3ef91c9a272fd1ae3d1d315ca1370d311779ad51e430a8b37db

Contents?: true

Size: 1.28 KB

Versions: 27

Compression:

Stored size: 1.28 KB

Contents

if RUBY_VERSION =~ /1\.8/
  # Useful when `timeout.rb`, which, on M.R.I 1.8, relies on green threads, does not work consistently.
  begin
    require 'system_timer'
    AppleTvConverter::Timer = SystemTimer
  rescue LoadError
    require 'timeout'
    AppleTvConverter::Timer = Timeout
  end
else
  require 'timeout'
  AppleTvConverter::Timer = Timeout
end

require 'win32/process' if RUBY_PLATFORM =~ /(win|w)(32|64)$/

#
# Monkey Patch timeout support into the IO class
#
class IO
  def each_with_timeout(pid, seconds, sep_string=$/)
    sleeping_queue = Queue.new
    thread = nil

    timer_set = lambda do
      thread = new_thread(pid) { AppleTvConverter::Timer.timeout(seconds) { sleeping_queue.pop } }
    end

    timer_cancel = lambda do
      thread.kill if thread rescue nil
    end

    timer_set.call
    each(sep_string) do |buffer|
      timer_cancel.call
      yield buffer
      timer_set.call
    end
  ensure
    timer_cancel.call
  end

  private
  def new_thread(pid, &block)
    current_thread = Thread.current
    Thread.new do
      begin
        block.call
      rescue Exception => e
        current_thread.raise e
        if RUBY_PLATFORM =~ /(win|w)(32|64)$/
          Process.kill(1, pid)
        else
          Process.kill('SIGKILL', pid)
        end
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
apple-tv-converter-0.3.2 lib/apple_tv_converter/io_patch.rb
apple-tv-converter-0.3.1 lib/apple_tv_converter/io_patch.rb
apple-tv-converter-0.3.0 lib/apple_tv_converter/io_patch.rb
apple-tv-converter-0.2.0 lib/apple_tv_converter/io_patch.rb
apple-tv-converter-0.1.0 lib/apple_tv_converter/io_patch.rb
apple-tv-converter-0.0.2 lib/apple_tv_converter/io_patch.rb
apple-tv-converter-0.0.1 lib/apple_tv_converter/io_patch.rb