1+ # frozen_string_literal: true
12#--
23# = timeout.rb
34#
@@ -47,10 +48,11 @@ class Error < RuntimeError
4748 # Represents +thr+ asking for it to be timeout at in +secs+
4849 # seconds. At timeout, raise +exc+.
4950 class TimeoutRequest
50- def initialize ( secs , thr , exc )
51+ def initialize ( secs , thr , exc , message )
5152 @left = secs
5253 @thread = thr
5354 @exception = exc
55+ @message = message
5456 end
5557
5658 attr_reader :thread , :left
@@ -65,7 +67,7 @@ def elapsed(time)
6567 # Raise @exception if @thread.
6668 def cancel
6769 if @thread and @thread . alive?
68- @thread . raise @exception , 'execution expired'
70+ @thread . raise @exception , @message
6971 end
7072
7173 @left = 0
@@ -126,8 +128,8 @@ def self.ensure_timeout_thread_running
126128 end
127129 end
128130
129- def self . add_timeout ( time , exc )
130- r = TimeoutRequest . new ( time , Thread . current , exc )
131+ def self . add_timeout ( time , exc , message )
132+ r = TimeoutRequest . new ( time , Thread . current , exc , message )
131133 @chan << r
132134 ensure_timeout_thread_running
133135 r
@@ -136,7 +138,7 @@ def self.add_timeout(time, exc)
136138
137139 if Truffle ::Boot . single_threaded?
138140
139- def timeout ( sec , exception = Error )
141+ def timeout ( sec , exception = Error , message = nil )
140142 Truffle ::Debug . log_warning 'threads are disabled, so timeout is being ignored'
141143 yield sec
142144 end
@@ -152,12 +154,11 @@ def timeout(sec, exception=Error)
152154 # Timeout' into your classes so they have a #timeout method, as well as a
153155 # module method, so you can call it directly as Timeout.timeout().
154156
155- def timeout ( sec , exception = Error )
157+ def timeout ( sec , exception = Error , message = nil )
156158 return yield if sec == nil or sec . zero?
157- raise ThreadError , 'timeout within critical session' if Thread . respond_to? ( :critical ) && Thread . critical
158-
159- req = Timeout . add_timeout sec , exception
160159
160+ message ||= 'execution expired'
161+ req = Timeout . add_timeout sec , exception , message
161162 begin
162163 yield sec
163164 ensure
0 commit comments