class ConditionVariable

Public Instance Methods

timed_wait(mutex, secs) click to toggle source

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.

# File lib/phusion_passenger/utils.rb, line 872
def timed_wait(mutex, secs)
        ruby_engine = defined?(RUBY_ENGINE) ? RUBY_ENGINE : "ruby"
        if secs > 100000000
                # NOTE: If one calls timeout() on FreeBSD 5 with an
                # argument of more than 100000000, then MRI will become
                # stuck in an infite loop, blocking all threads. It seems
                # that MRI uses select() to implement sleeping.
                # I think that a value of more than 100000000 overflows
                # select()'s data structures, causing it to behave incorrectly.
                # So we just make sure we can't sleep more than 100000000
                # seconds.
                secs = 100000000
        end
        if ruby_engine == "jruby"
                if secs > 0
                        return wait(mutex, secs)
                else
                        return wait(mutex)
                end
        elsif RUBY_VERSION >= '1.9.2'
                if secs > 0
                        t1 = Time.now
                        wait(mutex, secs)
                        t2 = Time.now
                        return t2.to_f - t1.to_f < secs
                else
                        wait(mutex)
                        return true
                end
        else
                if secs > 0
                        Timeout.timeout(secs) do
                                wait(mutex)
                        end
                else
                        wait(mutex)
                end
                return true
        end
rescue Timeout::Error
        return false
end
timed_wait!(mutex, secs) click to toggle source

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

# File lib/phusion_passenger/utils.rb, line 917
def timed_wait!(mutex, secs)
        ruby_engine = defined?(RUBY_ENGINE) ? RUBY_ENGINE : "ruby"
        if secs > 100000000
                # See the corresponding note for timed_wait().
                secs = 100000000
        end
        if ruby_engine == "jruby"
                if secs > 0
                        if !wait(mutex, secs)
                                raise Timeout::Error, "Timeout"
                        end
                else
                        wait(mutex)
                end
        elsif RUBY_VERSION >= '1.9.2'
                if secs > 0
                        t1 = Time.now
                        wait(mutex, secs)
                        t2 = Time.now
                        if t2.to_f - t1.to_f >= secs
                                raise Timeout::Error, "Timeout"
                        end
                else
                        wait(mutex)
                end
        else
                if secs > 0
                        Timeout.timeout(secs) do
                                wait(mutex)
                        end
                else
                        wait(mutex)
                end
        end
        return nil
end