Skip to content

Instantly share code, notes, and snippets.

@TwP
Created November 23, 2009 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TwP/241224 to your computer and use it in GitHub Desktop.
Save TwP/241224 to your computer and use it in GitHub Desktop.
STDOUT.sync = true
require 'enumerator'
module Kernel
def interruptible_select( rd, wr = nil, er = nil, timeout = nil )
t = Thread.current
t[:select_signal_pipe] = IO.pipe if t[:select_signal_pipe].nil?
p = t[:select_signal_pipe]
p.first.read_nonblock rescue nil
ary = select([p.first].concat(rd), wr, er, timeout)
unless ary.nil? or ary.first.empty?
ary.first.delete(p.first)
ary = nil if ary.all? {|a| a.empty?}
end
return ary
end
end
class Thread
alias :original_wakeup :wakeup
def wakeup
if self[:select_signal_pipe]
p = self[:select_signal_pipe]
p.first.read_nonblock rescue nil
p.last.write_nonblock "\000" rescue nil
end
original_wakeup
end
end
rd, wr = IO.pipe
t = Thread.new {
ary = Kernel.interruptible_select([rd], nil, nil, nil) rescue nil
puts "Thread is about to exit ..."
puts ary.inspect
}
sleep 2
t.wakeup
t.join
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment