Debian Codegraph @

doc/guide/src/MLtonSignal.adoc

raw
1 = MLtonSignal
2
3 [source,sml]
4 ----
5 signature MLTON_SIGNAL =
6 sig
7 type t = Posix.Signal.signal
8 type signal = t
9
10 structure Handler:
11 sig
12 type t
13
14 val default: t
15 val handler: (Thread.Runnable.t -> Thread.Runnable.t) -> t
16 val ignore: t
17 val isDefault: t -> bool
18 val isIgnore: t -> bool
19 val simple: (unit -> unit) -> t
20 end
21
22 structure Mask:
23 sig
24 type t
25
26 val all: t
27 val allBut: signal list -> t
28 val block: t -> unit
29 val getBlocked: unit -> t
30 val isMember: t * signal -> bool
31 val none: t
32 val setBlocked: t -> unit
33 val some: signal list -> t
34 val unblock: t -> unit
35 end
36
37 val getHandler: t -> Handler.t
38 val handled: unit -> Mask.t
39 val prof: t
40 val restart: bool ref
41 val setHandler: t * Handler.t -> unit
42 val suspend: Mask.t -> unit
43 val vtalrm: t
44 end
45 ----
46
47 Signals handlers are functions from (runnable) threads to (runnable)
48 threads. When a signal arrives, the corresponding signal handler is
49 invoked, its argument being the thread that was interrupted by the
50 signal. The signal handler runs asynchronously, in its own thread.
51 The signal handler returns the thread that it would like to resume
52 execution (this is often the thread that it was passed). It is an
53 error for a signal handler to raise an exception that is not handled
54 within the signal handler itself.
55
56 A signal handler is never invoked while the running thread is in a
57 critical section (see <<MLtonThread#>>). Invoking a signal handler
58 implicitly enters a critical section and the normal return of a signal
59 handler implicitly exits the critical section; hence, a signal handler
60 is never interrupted by another signal handler.
61
62 * `type t`
63 +
64 the type of signals.
65
66 * `type Handler.t`
67 +
68 the type of signal handlers.
69
70 * `Handler.default`
71 +
72 handles the signal with the default action.
73
74 * `Handler.handler f`
75 +
76 returns a handler `h` such that when a signal `s` is handled by `h`,
77 `f` will be passed the thread that was interrupted by `s` and should
78 return the thread that will resume execution.
79
80 * `Handler.ignore`
81 +
82 is a handler that will ignore the signal.
83
84 * `Handler.isDefault`
85 +
86 returns true if the handler is the default handler.
87
88 * `Handler.isIgnore`
89 +
90 returns true if the handler is the ignore handler.
91
92 * `Handler.simple f`
93 +
94 returns a handler that executes `f ()` and does not switch threads.
95
96 * `type Mask.t`
97 +
98 the type of signal masks, which are sets of blocked signals.
99
100 * `Mask.all`
101 +
102 a mask of all signals.
103
104 * `Mask.allBut l`
105 +
106 a mask of all signals except for those in `l`.
107
108 * `Mask.block m`
109 +
110 blocks all signals in `m`.
111
112 * `Mask.getBlocked ()`
113 +
114 gets the signal mask `m`, i.e. a signal is blocked if and only if it
115 is in `m`.
116
117 * `Mask.isMember (m, s)`
118 +
119 returns true if the signal `s` is in `m`.
120
121 * `Mask.none`
122 +
123 a mask of no signals.
124
125 * `Mask.setBlocked m`
126 +
127 sets the signal mask to `m`, i.e. a signal is blocked if and only if
128 it is in `m`.
129
130 * `Mask.some l`
131 +
132 a mask of the signals in `l`.
133
134 * `Mask.unblock m`
135 +
136 unblocks all signals in `m`.
137
138 * `getHandler s`
139 +
140 returns the current handler for signal `s`.
141
142 * `handled ()`
143 +
144 returns the signal mask `m` corresponding to the currently handled
145 signals; i.e., a signal is handled if and only if it is in `m`.
146
147 * `prof`
148 +
149 `SIGPROF`, the profiling signal.
150
151 * `restart`
152 +
153 dynamically determines the behavior of interrupted system calls; when
154 `true`, interrupted system calls are restarted; when `false`,
155 interrupted system calls raise `OS.SysError`.
156
157 * `setHandler (s, h)`
158 +
159 sets the handler for signal `s` to `h`.
160
161 * `suspend m`
162 +
163 temporarily sets the signal mask to `m` and suspends until an unmasked
164 signal is received and handled, at which point `suspend` resets the
165 mask and returns.
166
167 * `vtalrm`
168 +
169 `SIGVTALRM`, the signal for virtual timers.
170
171
172 == Interruptible System Calls
173
174 Signal handling interacts in a non-trivial way with those functions in
175 the <<BasisLibrary#,Basis Library>> that correspond directly to
176 interruptible system calls (a subset of those functions that may raise
177 `OS.SysError`). The desire is that these functions should have
178 predictable semantics. The principal concerns are:
179
180 1. System calls that are interrupted by signals should, by default, be
181 restarted; the alternative is to raise
182 +
183 [source,sml]
184 ----
185 OS.SysError (Posix.Error.errorMsg Posix.Error.intr,
186 SOME Posix.Error.intr)
187 ----
188 +
189 This behavior is determined dynamically by the value of `Signal.restart`.
190
191 2. Signal handlers should always get a chance to run (when outside a
192 critical region). If a system call is interrupted by a signal, then
193 the signal handler will run before the call is restarted or
194 `OS.SysError` is raised; that is, before the `Signal.restart` check.
195
196 3. A system call that must be restarted while in a critical section
197 will be restarted with the handled signals blocked (and the previously
198 blocked signals remembered). This encourages the system call to
199 complete, allowing the program to make progress towards leaving the
200 critical section where the signal can be handled. If the system call
201 completes, the set of blocked signals are restored to those previously
202 blocked.