--Apple-Mail-1-999253821
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
delsp=yes;
format=flowed
I took the concept of negative sleeping as guaranteeing that the
thread will not sleep or relinquish control for that time period.
Normal sleep means that the thread concedes to the other threads of
the program for the specified time, so negative sleeping is forcing
all other threads to concede control to the current thread for the
specified time instead. That said, here is my code:
module Kernel
alias normal_sleep sleep
def sleep(seconds)
normal_sleep(seconds) and return if seconds >= 0
priorities = {}
(Thread.list - [Thread.current]).each do |thread|
priorities[thread] = thread.priority
thread.priority = Thread.current.priority-1
end
Thread.new do
normal_sleep(-seconds)
priorities.each do |thread, priority|
thread.priority = priority
end
end
end
end
This is difficult to test cleanly since positive sleeping within the
current thread gives the de-prioritized threads a chance to run for a
short time (though I do see that as appropriate behavior... a thread
that is negative sleeping should still be able to positive sleep for
a short time). It is testable if you are willing to read extremely
long outputs or are willing to put up with using some long, empty
loops to minimize output.
- Jake McArthur
--Apple-Mail-1-999253821--