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.



511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
# File 'lib/phusion_passenger/utils.rb', line 511

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.



546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
# File 'lib/phusion_passenger/utils.rb', line 546

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