Skip to content

Commit 1ae9414

Browse files
oprypinRX14
authored andcommittedJul 21, 2017
Add documentation to Signal
1 parent f4178bc commit 1ae9414

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed
 

‎src/signal.cr

+29-3
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,43 @@ require "c/unistd"
7272
end
7373
{% end %}
7474

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.
7896
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).
79102
def trap(block : Signal ->)
80103
trap &block
81104
end
82105

106+
# ditto
83107
def trap(&block : Signal ->)
84108
Event::SignalHandler.add_handler self, block
85109
end
86110

111+
# Resets the handler for this signal to the OS default.
87112
def reset
88113
case self
89114
when CHLD
@@ -97,6 +122,7 @@ enum Signal
97122
end
98123
end
99124

125+
# Clears the handler for this signal and prevents the OS default action.
100126
def ignore
101127
del_handler Proc(Int32, Void).new(Pointer(Void).new(1_u64), Pointer(Void).null)
102128
end

0 commit comments

Comments
 (0)
Please sign in to comment.