#572: ConditionVariable#wait should accept a timeout argument ---------------------------------+------------------------------------------ Reporter: hongli...@… | Owner: lsansone...@… Type: defect | Status: new Priority: blocker | Milestone: Component: MacRuby | Keywords: ---------------------------------+------------------------------------------ Right now, ConditionVariable#wait waits forever until the condition is signaled. There should be a way to wait on a condition variable for a bounded time. A typical use case is as follows:
There is a background thread which performs some cleaning function every x seconds. We also want to be able to tell the thread to clean now, or to exit (i.e. quitting its main loop so that we can join the thread). This thread waits on a condition variable for x seconds, and then checks whether there was a timeout on the wait, or whether the wait as signaled. In case of the former it will run the cleanup code. In case of the latter it'll check whether @quit is set, and then either stop the loop or run the cleanup code. This support for bounded waiting should be implemented by supporting an extra 'timeout' argument in ConditionVariable#wait. In fact JRuby and MRI 1.9.2dev already support it, though differenly. The call seqs are as follows: {{{ MRI <= 1.9.1: ConditionVariable#wait(mutex) No native support for bounded time waiting. In Phusion Passenger we emulate this with timeout.rb which is very hacky, though it seems to work. MRI 1.9.2dev: ConditionVariable#wait(mutex, timeout) => integer Waits for at most 'timeout' time. 'timeout' may be a floating point number. Returns the number of *seconds* spent waiting. So even if it actually spent 3.5 seconds waiting, it'll either return 3 or 4. There is no way to check whether the wait was signaled or timed out, you have to guess based on the time waited. JRuby: ConditionVariable#wait(mutex, timeout) => boolean Waits for at most 'timeout' time. 'timeout' may be a floating point number. Returns true if condition was signaled, false if it timed out. }}} I think the JRuby approach makes most sense. We should also convince the MRI developers to change the behavior to match JRuby's before 1.9.2 is released. Bounded wait can be implemented with pthread_cond_timedwait. -- Ticket URL: <http://www.macruby.org/trac/ticket/572> MacRuby <http://macruby.org/> _______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel