Created
November 23, 2009 17:48
-
-
Save TwP/241224 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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