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