Class ConditionVariable |
In: | lib/phusion_passenger/utils.rb
Parent: | Object

Methods

- timed_wait
- timed_wait!

Public Instance methods
timed_wait(mutex, secs)

This is like ConditionVariable.wait(), but allows one to wait a maximum amount of time. Returns true if this condition was signaled, false if a timeout occurred.

[ show source ]

         # File lib/phusion_passenger/utils.rb, line 464
    464:         def timed_wait(mutex, secs)
    465:                 if secs > 100000000
    466:                         # NOTE: If one calls timeout() on FreeBSD 5 with an
    467:                         # argument of more than 100000000, then MRI will become
    468:                         # stuck in an infite loop, blocking all threads. It seems
    469:                         # that MRI uses select() to implement sleeping.
    470:                         # I think that a value of more than 100000000 overflows
    471:                         # select()'s data structures, causing it to behave incorrectly.
    472:                         # So we just make sure we can't sleep more than 100000000
    473:                         # seconds.
    474:                         secs = 100000000
    475:                 end
    476:                 if defined?(RUBY_ENGINE) && RUBY_ENGINE == "jruby"
    477:                         if secs > 0
    478:                                 return wait(mutex, secs)
    479:                         else
    480:                                 return wait(mutex)
    481:                         end
    482:                 else
    483:                         require 'timeout' unless defined?(Timeout)
    484:                         if secs > 0
    485:                                 Timeout.timeout(secs) do
    486:                                         wait(mutex)
    487:                                 end
    488:                         else
    489:                                 wait(mutex)
    490:                         end
    491:                         return true
    492:                 end
    493:         rescue Timeout::Error
    494:                 return false
    495:         end

timed_wait!(mutex, secs)

This is like ConditionVariable.wait(), but allows one to wait a maximum amount of time. Raises Timeout::Error if the timeout has elapsed.

[ show source ]

         # File lib/phusion_passenger/utils.rb, line 499
    499:         def timed_wait!(mutex, secs)
    500:                 require 'timeout' unless defined?(Timeout)
    501:                 if secs > 100000000
    502:                         # See the corresponding note for timed_wait().
    503:                         secs = 100000000
    504:                 end
    505:                 if defined?(RUBY_ENGINE) && RUBY_ENGINE == "jruby"
    506:                         if secs > 0
    507:                                 if !wait(mutex, secs)
    508:                                         raise Timeout::Error, "Timeout"
    509:                                 end
    510:                         else
    511:                                 wait(mutex)
    512:                         end
    513:                 else
    514:                         if secs > 0
    515:                                 Timeout.timeout(secs) do
    516:                                         wait(mutex)
    517:                                 end
    518:                         else
    519:                                 wait(mutex)
    520:                         end
    521:                 end
    522:                 return nil
    523:         end