timeout.rb.diff

Matthias Wächter, 01/18/2011 02:36 AM

Download (1.12 KB)

View differences:

timeout.rb 2011-01-17 18:35:28.976375000 +0100
21 21
#
22 22
# Copyright:: (C) 2000  Network Applied Communication Laboratory, Inc.
23 23
# Copyright:: (C) 2000  Information-technology Promotion Agency, Japan
24

  
24
require 'thread'
25 25
module Timeout
26 26
  # Raised by Timeout#timeout when the block times out.
27 27
  class Error < RuntimeError
......
45 45
    exception = klass || Class.new(ExitException)
46 46
    begin
47 47
      x = Thread.current
48
      y = Thread.start {
49
        begin
50
          sleep sec
51
        rescue => e
52
          x.raise e
53
        else
54
          x.raise exception, "execution expired" if x.alive?
55
        end
48
      m = Mutex.new
49
      m.synchronize {
50
        y = Thread.start {
51
          m.synchronize {} # ensure variable y is pointing to the thread
52
                           # before y starts the timer
53
          begin
54
            sleep sec
55
          rescue => e
56
            x.raise e
57
          else
58
            x.raise exception, "execution expired" if x.alive?
59
          end
60
        }
56 61
      }
57 62
      return yield(sec)
58 63
    rescue exception => e