@@ -72,18 +72,43 @@ require "c/unistd"
72
72
end
73
73
{% end % }
74
74
75
- # Signals are processed through the event loop and run in their own Fiber.
76
- # Signals may be lost if the event loop doesn't run before exit.
77
- # An uncaught exceptions in a signal handler is a fatal error.
75
+ # This module provides a way to handle OS signals passed to the process.
76
+ #
77
+ # ```
78
+ # puts "Ctrl+C still has the OS default action (stops the program)"
79
+ # sleep 3
80
+ #
81
+ # Signal::INT.trap do
82
+ # puts "Gotcha!"
83
+ # end
84
+ # puts "Ctrl+C will be caught from now on"
85
+ # sleep 3
86
+ #
87
+ # Signal::INT.reset
88
+ # puts "Ctrl+C is back to the OS default action"
89
+ # sleep 3
90
+ # ```
91
+ #
92
+ # Note:
93
+ # - Signals are processed through the event loop and run in their own Fiber.
94
+ # - Signals may be lost if the event loop doesn't run before exit.
95
+ # - An uncaught exception in a signal handler is a fatal error.
78
96
enum Signal
97
+ # Sets the handler for this signal to the passed function.
98
+ #
99
+ # After executing this, whenever the current process receives the
100
+ # corresponding signal, the passed function will be run (instead of the OS
101
+ # default).
79
102
def trap (block : Signal - > )
80
103
trap & block
81
104
end
82
105
106
+ # ditto
83
107
def trap (& block : Signal - > )
84
108
Event ::SignalHandler .add_handler self , block
85
109
end
86
110
111
+ # Resets the handler for this signal to the OS default.
87
112
def reset
88
113
case self
89
114
when CHLD
@@ -97,6 +122,7 @@ enum Signal
97
122
end
98
123
end
99
124
125
+ # Clears the handler for this signal and prevents the OS default action.
100
126
def ignore
101
127
del_handler Proc (Int32 , Void ).new(Pointer (Void ).new(1 _u64 ), Pointer (Void ).null)
102
128
end
0 commit comments