U:RDoc::AnyMethod[iI"handle_interrupt:ETI"Thread::handle_interrupt;TT:publico:RDoc::Markup::Document:@parts[2o:RDoc::Markup::Paragraph; [I"+Changes asynchronous interrupt timing.;To:RDoc::Markup::BlankLine o;
; [ I"F_interrupt_ means asynchronous event and corresponding procedure ;TI"Cby Thread#raise, Thread#kill, signal trap (not supported yet) ;TI"Fand main thread termination (if main thread terminates, then all ;TI""other thread will be killed).;T@o;
; [I"=The given +hash+ has pairs like ExceptionClass => ;TI"P:TimingSymbol
. Where the ExceptionClass is the interrupt handled by ;TI"Kthe given block. The TimingSymbol can be one of the following symbols:;T@o:RDoc::Markup::List:
@type:
LABEL:@items[o:RDoc::Markup::ListItem:@label[I"+:immediate+;T; [o;
; [I"#Invoke interrupts immediately.;To;;[I"+:on_blocking+;T; [o;
; [I"1Invoke interrupts while _BlockingOperation_.;To;;[I"
+:never+;T; [o;
; [I"!Never invoke all interrupts.;T@o;
; [I"Q_BlockingOperation_ means that the operation will block the calling thread, ;TI"Rsuch as read and write. On CRuby implementation, _BlockingOperation_ is any ;TI"$operation executed without GVL.;T@o;
; [I"HMasked asynchronous interrupts are delayed until they are enabled. ;TI".This method is similar to sigprocmask(3).;T@S:RDoc::Markup::Heading:
leveli: textI" NOTE;T@o;
; [I"2Asynchronous interrupts are difficult to use.;T@o;
; [I"bIf you need to communicate between threads, please consider to use another way such as Queue.;T@o;
; [I";Or use them with deep understanding about this method.;T@S;;i;I"
Usage;T@o;
; [I"@In this example, we can guard from Thread#raise exceptions.;T@o;
; [I"OUsing the +:never+ TimingSymbol the RuntimeError exception will always be ;TI"Bignored in the first block of the main thread. In the second ;TI"Q::handle_interrupt block we can purposefully handle RuntimeError exceptions.;T@o:RDoc::Markup::Verbatim; [I"th = Thread.new do
;TI"9 Thread.handle_interrupt(RuntimeError => :never) {
;TI" begin
;TI"< # You can write resource allocation code safely.
;TI"A Thread.handle_interrupt(RuntimeError => :immediate) {
;TI" # ...
;TI"
}
;TI" ensure
;TI"> # You can write resource deallocation code safely.
;TI"
end
;TI" }
;TI" end
;TI"Thread.pass
;TI"# ...
;TI"th.raise "stop"
;T:@format0o;
; [I"NWhile we are ignoring the RuntimeError exception, it's safe to write our ;TI"Mresource allocation code. Then, the ensure block is where we can safely ;TI"deallocate your resources.;T@S;;i ;I"!Guarding from Timeout::Error;T@o;
; [ I"PIn the next example, we will guard from the Timeout::Error exception. This ;TI"Swill help prevent from leaking resources when Timeout::Error exceptions occur ;TI"Jduring normal ensure clause. For this example we use the help of the ;TI"2standard library Timeout, from lib/timeout.rb;T@o;; [I"require 'timeout'
;TI"9Thread.handle_interrupt(Timeout::Error => :never) {
;TI" timeout(10){
;TI"- # Timeout::Error doesn't occur here
;TI"C Thread.handle_interrupt(Timeout::Error => :on_blocking) {
;TI"5 # possible to be killed by Timeout::Error
;TI"& # while blocking operation
;TI" }
;TI"- # Timeout::Error doesn't occur here
;TI" }
;TI"}
;T;0o;
; [ I"SIn the first part of the +timeout+ block, we can rely on Timeout::Error being ;TI"Qignored. Then in the Timeout::Error => :on_blocking
block, any ;TI"Foperation that will block the calling thread is susceptible to a ;TI"+Timeout::Error exception being raised.;T@S;;i ;I"Stack control settings;T@o;
; [I"RIt's possible to stack multiple levels of ::handle_interrupt blocks in order ;TI"Hto control more than one ExceptionClass and TimingSymbol at a time.;T@o;; [
I"3Thread.handle_interrupt(FooError => :never) {
;TI"5 Thread.handle_interrupt(BarError => :never) {
;TI"2 # FooError and BarError are prohibited.
;TI" }
;TI"}
;T;0S;;i ;I"$Inheritance with ExceptionClass;T@o;
; [I"SAll exceptions inherited from the ExceptionClass parameter will be considered.;T@o;; [I"4Thread.handle_interrupt(Exception => :never) {
;TI"A # all exceptions inherited from Exception are prohibited.
;TI"};T;0:
@fileI"
thread.c;T:0@omit_headings_from_table_of_contents_below0I"BThread.handle_interrupt(hash) { ... } -> result of the block
;T0[ I" (p1);T@’FI"Thread;TcRDoc::NormalClass00