1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/lib/timeout.rb
matz fe13785cc6 * marshal.c (w_object): if object responds to 'marshal_dump',
Marshal.dump uses it to dump object.  unlike '_dump',
  marshal_dump returns any kind of object.

* marshal.c (r_object0): restore instance by calling
  'marshal_load' method.  unlike '_load', it's an instance
  method, to handle cyclic reference.

* marshal.c (marshal_load): all objects read from file should be
  tainted. [ruby-core:01325]

* lib/timeout.rb (Timeout::timeout): execute immediately if sec is
  zero.

* ext/socket/socket.c (socks_init): typo fixed. [ruby-talk:77232]

* ext/socket/extconf.rb: the default value for --enable-socks is
  taken from ENV["SOCKS_SERVER"]. [ruby-talk:77232]

* ruby.c (proc_options): add -W option. -W0 to shut up all warning
  messages. [ruby-talk:77227]

* error.c (rb_warn): no message will be printed if the value of
  $VERBOSE is "nil", i.e. perfect silence.

* ruby.c (verbose_setter): $VERBOSE value is either true, false,
  or nil.

* io.c (Init_IO): no "read" check for $stdin.  in addition some
  function names has been changed.

* regex.c (re_match_exec): incorrect multibyte match.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4220 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2003-07-29 18:26:55 +00:00

78 lines
1.4 KiB
Ruby

#
# timeout.rb -- execution timeout
#
# Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
# Copyright (C) 2000 Information-technology Promotion Agency, Japan
#
#= SYNOPSIS
#
# require 'timeout'
# status = timeout(5) {
# # something may take time
# }
#
#= DESCRIPTION
#
# timeout executes the block. If the block execution terminates successfully
# before timeout, it returns true. If not, it terminates the execution and
# raise TimeoutError exception.
#
#== Parameters
#
# : timout
#
# The time in seconds to wait for block teminatation.
#
# : [exception]
#
# The exception classs to be raised on timeout.
#
#=end
module Timeout
class Error<Interrupt
end
def timeout(sec, exception=Error)
return yield if sec == nil or sec.zero?
begin
x = Thread.current
y = Thread.start {
sleep sec
x.raise exception, "execution expired" if x.alive?
}
yield sec
# return true
ensure
y.kill if y and y.alive?
end
end
module_function :timeout
end
# compatible
def timeout(n, e=Timeout::Error, &block)
Timeout::timeout(n, e, &block)
end
TimeoutError = Timeout::Error
if __FILE__ == $0
p timeout(5) {
45
}
p timeout(5, TimeoutError) {
45
}
p timeout(nil) {
54
}
p timeout(0) {
54
}
p timeout(5) {
loop {
p 10
sleep 1
}
}
end