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 376
    376:         def timed_wait(mutex, secs)
    377:                 if secs > 100000000
    378:                         # NOTE: If one calls timeout() on FreeBSD 5 with an
    379:                         # argument of more than 100000000, then MRI will become
    380:                         # stuck in an infite loop, blocking all threads. It seems
    381:                         # that MRI uses select() to implement sleeping.
    382:                         # I think that a value of more than 100000000 overflows
    383:                         # select()'s data structures, causing it to behave incorrectly.
    384:                         # So we just make sure we can't sleep more than 100000000
    385:                         # seconds.
    386:                         secs = 100000000
    387:                 end
    388:                 if defined?(RUBY_ENGINE) && RUBY_ENGINE == "jruby"
    389:                         if secs > 0
    390:                                 return wait(mutex, secs)
    391:                         else
    392:                                 return wait(mutex)
    393:                         end
    394:                 else
    395:                         require 'timeout' unless defined?(Timeout)
    396:                         if secs > 0
    397:                                 Timeout.timeout(secs) do
    398:                                         wait(mutex)
    399:                                 end
    400:                         else
    401:                                 wait(mutex)
    402:                         end
    403:                         return true
    404:                 end
    405:         rescue Timeout::Error
    406:                 return false
    407:         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 411
    411:         def timed_wait!(mutex, secs)
    412:                 require 'timeout' unless defined?(Timeout)
    413:                 if secs > 100000000
    414:                         # See the corresponding note for timed_wait().
    415:                         secs = 100000000
    416:                 end
    417:                 if defined?(RUBY_ENGINE) && RUBY_ENGINE == "jruby"
    418:                         if secs > 0
    419:                                 if !wait(mutex, secs)
    420:                                         raise Timeout::Error, "Timeout"
    421:                                 end
    422:                         else
    423:                                 wait(mutex)
    424:                         end
    425:                 else
    426:                         if secs > 0
    427:                                 Timeout.timeout(secs) do
    428:                                         wait(mutex)
    429:                                 end
    430:                         else
    431:                                 wait(mutex)
    432:                         end
    433:                 end
    434:                 return nil
    435:         end