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