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 877
    877:         def timed_wait(mutex, secs)
    878:                 ruby_engine = defined?(RUBY_ENGINE) ? RUBY_ENGINE : "ruby"
    879:                 if secs > 100000000
    880:                         # NOTE: If one calls timeout() on FreeBSD 5 with an
    881:                         # argument of more than 100000000, then MRI will become
    882:                         # stuck in an infite loop, blocking all threads. It seems
    883:                         # that MRI uses select() to implement sleeping.
    884:                         # I think that a value of more than 100000000 overflows
    885:                         # select()'s data structures, causing it to behave incorrectly.
    886:                         # So we just make sure we can't sleep more than 100000000
    887:                         # seconds.
    888:                         secs = 100000000
    889:                 end
    890:                 if ruby_engine == "jruby"
    891:                         if secs > 0
    892:                                 return wait(mutex, secs)
    893:                         else
    894:                                 return wait(mutex)
    895:                         end
    896:                 elsif RUBY_VERSION >= '1.9.2'
    897:                         if secs > 0
    898:                                 t1 = Time.now
    899:                                 wait(mutex, secs)
    900:                                 t2 = Time.now
    901:                                 return t2.to_f - t1.to_f < secs
    902:                         else
    903:                                 wait(mutex)
    904:                                 return true
    905:                         end
    906:                 else
    907:                         if secs > 0
    908:                                 Timeout.timeout(secs) do
    909:                                         wait(mutex)
    910:                                 end
    911:                         else
    912:                                 wait(mutex)
    913:                         end
    914:                         return true
    915:                 end
    916:         rescue Timeout::Error
    917:                 return false
    918:         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 922
    922:         def timed_wait!(mutex, secs)
    923:                 ruby_engine = defined?(RUBY_ENGINE) ? RUBY_ENGINE : "ruby"
    924:                 if secs > 100000000
    925:                         # See the corresponding note for timed_wait().
    926:                         secs = 100000000
    927:                 end
    928:                 if ruby_engine == "jruby"
    929:                         if secs > 0
    930:                                 if !wait(mutex, secs)
    931:                                         raise Timeout::Error, "Timeout"
    932:                                 end
    933:                         else
    934:                                 wait(mutex)
    935:                         end
    936:                 elsif RUBY_VERSION >= '1.9.2'
    937:                         if secs > 0
    938:                                 t1 = Time.now
    939:                                 wait(mutex, secs)
    940:                                 t2 = Time.now
    941:                                 if t2.to_f - t1.to_f >= secs
    942:                                         raise Timeout::Error, "Timeout"
    943:                                 end
    944:                         else
    945:                                 wait(mutex)
    946:                         end
    947:                 else
    948:                         if secs > 0
    949:                                 Timeout.timeout(secs) do
    950:                                         wait(mutex)
    951:                                 end
    952:                         else
    953:                                 wait(mutex)
    954:                         end
    955:                 end
    956:                 return nil
    957:         end