OSDN Git Service

change prefix to '/data/ruby'
[splhack/AndroidRuby.git] / lib / ruby-1.9.1-rc1 / lib / timeout.rb
1 # = timeout.rb
2 #
3 # execution timeout
4 #
5 # = Synopsis
6 #
7 #   require 'timeout'
8 #   status = Timeout::timeout(5) {
9 #     # Something that should be interrupted if it takes too much time...
10 #   }
11 #
12 # = Description
13 #
14 # A way of performing a potentially long-running operation in a thread, and terminating
15 # it's execution if it hasn't finished by a fixed amount of time.
16 #
17 # Previous versions of timeout didn't provide use a module for namespace. This version
18 # provides both Timeout.timeout, and a backwards-compatible #timeout.
19 #
20 # = Copyright
21 #
22 # Copyright:: (C) 2000  Network Applied Communication Laboratory, Inc.
23 # Copyright:: (C) 2000  Information-technology Promotion Agency, Japan
24
25 module Timeout
26   # Raised by Timeout#timeout when the block times out.
27   class Error < RuntimeError
28   end
29   class ExitException < ::Exception # :nodoc:
30   end
31
32   THIS_FILE = /\A#{Regexp.quote(__FILE__)}:/o
33   CALLER_OFFSET = ((c = caller[0]) && THIS_FILE =~ c) ? 1 : 0
34
35   # Executes the method's block.  If the block execution terminates before
36   # +sec+ seconds has passed, it returns the result value of the block.
37   # If not, it terminates the execution and raises +exception+ (which defaults
38   # to Timeout::Error).
39   #
40   # Note that this is both a method of module Timeout, so you can 'include Timeout'
41   # into your classes so they have a #timeout method, as well as a module method,
42   # so you can call it directly as Timeout.timeout().
43   def timeout(sec, klass = nil)   #:yield: +sec+
44     return yield(sec) if sec == nil or sec.zero?
45     exception = klass || Class.new(ExitException)
46     begin
47       x = Thread.current
48       y = Thread.start {
49         sleep sec
50         x.raise exception, "execution expired" if x.alive?
51       }
52       return yield(sec)
53     rescue exception => e
54       rej = /\A#{Regexp.quote(__FILE__)}:#{__LINE__-4}\z/o
55       (bt = e.backtrace).reject! {|m| rej =~ m}
56       level = -caller(CALLER_OFFSET).size
57       while THIS_FILE =~ bt[level]
58         bt.delete_at(level)
59         level += 1
60       end
61       raise if klass            # if exception class is specified, it
62                                 # would be expected outside.
63       raise Error, e.message, e.backtrace
64     ensure
65       if y and y.alive?
66         y.kill 
67         y.join # make sure y is dead.
68       end
69     end
70   end
71
72   module_function :timeout
73 end
74
75 # Identical to:
76 #
77 #   Timeout::timeout(n, e, &block).
78 #
79 # Defined for backwards compatibility with earlier versions of timeout.rb, see
80 # Timeout#timeout.
81 def timeout(n, e = nil, &block)
82   Timeout::timeout(n, e, &block)
83 end
84
85 # Another name for Timeout::Error, defined for backwards compatibility with
86 # earlier versions of timeout.rb.
87 TimeoutError = Timeout::Error
88
89 if __FILE__ == $0
90   p timeout(5) {
91     45
92   }
93   p timeout(5, TimeoutError) {
94     45
95   }
96   p timeout(nil) {
97     54
98   }
99   p timeout(0) {
100     54
101   }
102   p timeout(5) {
103     loop {
104       p 10
105       sleep 1
106     }
107   }
108 end