Class: ConditionVariable

Inherits:
Object show all
Defined in:
lib/phusion_passenger/utils.rb

Instance Method Summary collapse

Instance Method Details

#timed_wait(mutex, secs) ⇒ 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.



882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
# File 'lib/phusion_passenger/utils.rb', line 882

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) ⇒ Object

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



927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
# File 'lib/phusion_passenger/utils.rb', line 927

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