Projects

Ticket #572 (new defect)

Opened 6 months ago

Last modified 6 months ago

ConditionVariable#wait should accept a timeout argument

Reported by: honglilai@… Owned by: lsansonetti@…
Priority: blocker Milestone:
Component: MacRuby Keywords:
Cc:

Description

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.

Change History

Changed 6 months ago by lsansonetti@…

Let's see what the ruby-core folks will respond, but I agree that the jruby approach makes more sense. In any case, we should remove ConditionVariable from thread.rb and rewrite it as a wrapper to pthread_cond_t.

Note: See TracTickets for help on using tickets.