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.



465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# File 'lib/phusion_passenger/utils.rb', line 465

def timed_wait(mutex, secs)
	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 defined?(RUBY_ENGINE) && RUBY_ENGINE == "jruby"
		if secs > 0
			return wait(mutex, secs)
		else
			return wait(mutex)
		end
	else
		require 'timeout' unless defined?(Timeout)
		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.



500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
# File 'lib/phusion_passenger/utils.rb', line 500

def timed_wait!(mutex, secs)
	require 'timeout' unless defined?(Timeout)
	if secs > 100000000
		# See the corresponding note for timed_wait().
		secs = 100000000
	end
	if defined?(RUBY_ENGINE) && RUBY_ENGINE == "jruby"
		if secs > 0
			if !wait(mutex, 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