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 465 465: def timed_wait(mutex, secs) 466: if secs > 100000000 467: # NOTE: If one calls timeout() on FreeBSD 5 with an 468: # argument of more than 100000000, then MRI will become 469: # stuck in an infite loop, blocking all threads. It seems 470: # that MRI uses select() to implement sleeping. 471: # I think that a value of more than 100000000 overflows 472: # select()'s data structures, causing it to behave incorrectly. 473: # So we just make sure we can't sleep more than 100000000 474: # seconds. 475: secs = 100000000 476: end 477: if defined?(RUBY_ENGINE) && RUBY_ENGINE == "jruby" 478: if secs > 0 479: return wait(mutex, secs) 480: else 481: return wait(mutex) 482: end 483: else 484: require 'timeout' unless defined?(Timeout) 485: if secs > 0 486: Timeout.timeout(secs) do 487: wait(mutex) 488: end 489: else 490: wait(mutex) 491: end 492: return true 493: end 494: rescue Timeout::Error 495: return false 496: 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 500 500: def timed_wait!(mutex, secs) 501: require 'timeout' unless defined?(Timeout) 502: if secs > 100000000 503: # See the corresponding note for timed_wait(). 504: secs = 100000000 505: end 506: if defined?(RUBY_ENGINE) && RUBY_ENGINE == "jruby" 507: if secs > 0 508: if !wait(mutex, secs) 509: raise Timeout::Error, "Timeout" 510: end 511: else 512: wait(mutex) 513: end 514: else 515: if secs > 0 516: Timeout.timeout(secs) do 517: wait(mutex) 518: end 519: else 520: wait(mutex) 521: end 522: end 523: return nil 524: end