1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

merge revision(s) 15464:

* lib/timeout.rb (Timeout::timeout): made sensitive to location on the
	  stack.  [ruby-core:15458]


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8_6@17158 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
shyouhei 2008-06-14 07:51:55 +00:00
parent 1454c9812b
commit c6c5a44bcb
3 changed files with 29 additions and 7 deletions

View file

@ -1,3 +1,8 @@
Sat Jun 14 16:49:41 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
* lib/timeout.rb (Timeout::timeout): made sensitive to location on the
stack. [ruby-core:15458]
Fri Jun 13 13:14:31 2008 Yukihiro Matsumoto <matz@ruby-lang.org> Fri Jun 13 13:14:31 2008 Yukihiro Matsumoto <matz@ruby-lang.org>
* ext/dl/ptr.c (dlmem_each_i): typo fixed. a patch from IKOMA * ext/dl/ptr.c (dlmem_each_i): typo fixed. a patch from IKOMA

View file

@ -32,8 +32,13 @@ module Timeout
## ##
# Raised by Timeout#timeout when the block times out. # Raised by Timeout#timeout when the block times out.
class Error<Interrupt class Error < Interrupt
end end
class ExitException < ::Exception # :nodoc:
end
THIS_FILE = /\A#{Regexp.quote(__FILE__)}:/o
CALLER_OFFSET = ((c = caller[0]) && THIS_FILE =~ c) ? 1 : 0
## ##
# Executes the method's block. If the block execution terminates before +sec+ # Executes the method's block. If the block execution terminates before +sec+
@ -44,9 +49,10 @@ module Timeout
# Timeout' into your classes so they have a #timeout method, as well as a # Timeout' into your classes so they have a #timeout method, as well as a
# module method, so you can call it directly as Timeout.timeout(). # module method, so you can call it directly as Timeout.timeout().
def timeout(sec, exception=Error) def timeout(sec, klass = nil)
return yield if sec == nil or sec.zero? return yield if sec == nil or sec.zero?
raise ThreadError, "timeout within critical session" if Thread.critical raise ThreadError, "timeout within critical session" if Thread.critical
exception = klass || Class.new(ExitException)
begin begin
x = Thread.current x = Thread.current
y = Thread.start { y = Thread.start {
@ -55,6 +61,17 @@ module Timeout
} }
yield sec yield sec
# return true # return true
rescue exception => e
rej = /\A#{Regexp.quote(__FILE__)}:#{__LINE__-4}\z/o
(bt = e.backtrace).reject! {|m| rej =~ m}
level = -caller(CALLER_OFFSET).size
while THIS_FILE =~ bt[level]
bt.delete_at(level)
level += 1
end
raise if klass # if exception class is specified, it
# would be expected outside.
raise Error, e.message, e.backtrace
ensure ensure
y.kill if y and y.alive? y.kill if y and y.alive?
end end
@ -72,7 +89,7 @@ end
# Defined for backwards compatibility with earlier versions of timeout.rb, see # Defined for backwards compatibility with earlier versions of timeout.rb, see
# Timeout#timeout. # Timeout#timeout.
def timeout(n, e=Timeout::Error, &block) # :nodoc: def timeout(n, e = nil, &block) # :nodoc:
Timeout::timeout(n, e, &block) Timeout::timeout(n, e, &block)
end end

View file

@ -1,15 +1,15 @@
#define RUBY_VERSION "1.8.6" #define RUBY_VERSION "1.8.6"
#define RUBY_RELEASE_DATE "2008-06-13" #define RUBY_RELEASE_DATE "2008-06-14"
#define RUBY_VERSION_CODE 186 #define RUBY_VERSION_CODE 186
#define RUBY_RELEASE_CODE 20080613 #define RUBY_RELEASE_CODE 20080614
#define RUBY_PATCHLEVEL 175 #define RUBY_PATCHLEVEL 176
#define RUBY_VERSION_MAJOR 1 #define RUBY_VERSION_MAJOR 1
#define RUBY_VERSION_MINOR 8 #define RUBY_VERSION_MINOR 8
#define RUBY_VERSION_TEENY 6 #define RUBY_VERSION_TEENY 6
#define RUBY_RELEASE_YEAR 2008 #define RUBY_RELEASE_YEAR 2008
#define RUBY_RELEASE_MONTH 6 #define RUBY_RELEASE_MONTH 6
#define RUBY_RELEASE_DAY 13 #define RUBY_RELEASE_DAY 14
#ifdef RUBY_EXTERN #ifdef RUBY_EXTERN
RUBY_EXTERN const char ruby_version[]; RUBY_EXTERN const char ruby_version[];