-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
handle_interrupt.rb
62 lines (47 loc) · 939 Bytes
/
handle_interrupt.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env ruby
# frozen_string_literal: true
# Released under the MIT License.
# Copyright, 2021-2024, by Samuel Williams.
require "io/event"
require "fiber"
require "benchmark"
count = 0
thread = Thread.new do
input, output = IO.pipe
selector = Event::Selector.new(Fiber.current)
fiber = Fiber.new do
while true
selector.io_wait(Fiber.current, input, IO::READABLE)
input.read(1)
end
end
fiber.transfer
Thread.handle_interrupt(Interrupt => :never) do
while true
$stderr.puts "Selecting"
begin
selector.select(1)
rescue Errno::EINTR
# Ignore.
end
Fiber.new do
sleep 5
end.resume
# sleep 1
count += 1
begin
Thread.handle_interrupt(Interrupt => :immediate) {}
rescue Interrupt
$stderr.puts "Interrupted"
end
end
end
end
sleep 2
10.times do
sleep 1
$stderr.puts "Sending interrupt"
thread.raise(Interrupt)
end
sleep 1000
thread.join