1 | \chapter{Exception Features}
|
---|
2 |
|
---|
3 | This chapter covers the design and user interface of the \CFA
|
---|
4 | exception-handling mechanism.
|
---|
5 |
|
---|
6 | \section{Virtuals}
|
---|
7 | Virtual types and casts are not part of the exception system nor are they
|
---|
8 | required for an exception system. But an object-oriented style hierarchy is a
|
---|
9 | great way of organizing exceptions so a minimal virtual system has been added
|
---|
10 | to \CFA.
|
---|
11 |
|
---|
12 | The pattern of a simple hierarchy was borrowed from object-oriented
|
---|
13 | programming was chosen for several reasons.
|
---|
14 | The first is that it allows new exceptions to be added in user code
|
---|
15 | and in libraries independently of each other. Another is it allows for
|
---|
16 | different levels of exception grouping (all exceptions, all IO exceptions or
|
---|
17 | a particular IO exception). Also it also provides a simple way of passing
|
---|
18 | data back and forth across the throw.
|
---|
19 |
|
---|
20 | Virtual types and casts are not required for a basic exception-system but are
|
---|
21 | useful for advanced exception features. However, \CFA is not object-oriented so
|
---|
22 | there is no obvious concept of virtuals. Hence, to create advanced exception
|
---|
23 | features for this work, I needed to design and implement a virtual-like
|
---|
24 | system for \CFA.
|
---|
25 |
|
---|
26 | % NOTE: Maybe we should but less of the rational here.
|
---|
27 | Object-oriented languages often organized exceptions into a simple hierarchy,
|
---|
28 | \eg Java.
|
---|
29 | \begin{center}
|
---|
30 | \setlength{\unitlength}{4000sp}%
|
---|
31 | \begin{picture}(1605,612)(2011,-1951)
|
---|
32 | \put(2100,-1411){\vector(1, 0){225}}
|
---|
33 | \put(3450,-1411){\vector(1, 0){225}}
|
---|
34 | \put(3550,-1411){\line(0,-1){225}}
|
---|
35 | \put(3550,-1636){\vector(1, 0){150}}
|
---|
36 | \put(3550,-1636){\line(0,-1){225}}
|
---|
37 | \put(3550,-1861){\vector(1, 0){150}}
|
---|
38 | \put(2025,-1490){\makebox(0,0)[rb]{\LstBasicStyle{exception}}}
|
---|
39 | \put(2400,-1460){\makebox(0,0)[lb]{\LstBasicStyle{arithmetic}}}
|
---|
40 | \put(3750,-1460){\makebox(0,0)[lb]{\LstBasicStyle{underflow}}}
|
---|
41 | \put(3750,-1690){\makebox(0,0)[lb]{\LstBasicStyle{overflow}}}
|
---|
42 | \put(3750,-1920){\makebox(0,0)[lb]{\LstBasicStyle{zerodivide}}}
|
---|
43 | \end{picture}%
|
---|
44 | \end{center}
|
---|
45 | The hierarchy provides the ability to handle an exception at different degrees
|
---|
46 | of specificity (left to right). Hence, it is possible to catch a more general
|
---|
47 | exception-type in higher-level code where the implementation details are
|
---|
48 | unknown, which reduces tight coupling to the lower-level implementation.
|
---|
49 | Otherwise, low-level code changes require higher-level code changes, \eg,
|
---|
50 | changing from raising @underflow@ to @overflow@ at the low level means changing
|
---|
51 | the matching catch at the high level versus catching the general @arithmetic@
|
---|
52 | exception. In detail, each virtual type may have a parent and can have any
|
---|
53 | number of children. A type's descendants are its children and its children's
|
---|
54 | descendants. A type may not be its own descendant.
|
---|
55 |
|
---|
56 | The exception hierarchy allows a handler (@catch@ clause) to match multiple
|
---|
57 | exceptions, \eg a base-type handler catches both base and derived
|
---|
58 | exception-types.
|
---|
59 | \begin{cfa}
|
---|
60 | try {
|
---|
61 | ...
|
---|
62 | } catch(arithmetic &) {
|
---|
63 | ... // handle arithmetic, underflow, overflow, zerodivide
|
---|
64 | }
|
---|
65 | \end{cfa}
|
---|
66 | Most exception mechanisms perform a linear search of the handlers and select
|
---|
67 | the first matching handler, so the order of handers is now important because
|
---|
68 | matching is many to one.
|
---|
69 |
|
---|
70 | Each virtual type needs an associated virtual table. A virtual table is a
|
---|
71 | structure with fields for all the virtual members of a type. A virtual type has
|
---|
72 | all the virtual members of its parent and can add more. It may also update the
|
---|
73 | values of the virtual members and often does.
|
---|
74 |
|
---|
75 | While much of the virtual infrastructure is created, it is currently only used
|
---|
76 | internally for exception handling. The only user-level feature is the virtual
|
---|
77 | cast, which is the same as the \Cpp \lstinline[language=C++]|dynamic_cast|.
|
---|
78 | \label{p:VirtualCast}
|
---|
79 | \begin{cfa}
|
---|
80 | (virtual TYPE)EXPRESSION
|
---|
81 | \end{cfa}
|
---|
82 | Note, the syntax and semantics matches a C-cast, rather than the function-like
|
---|
83 | \Cpp syntax for special casts. Both the type of @EXPRESSION@ and @TYPE@ must be
|
---|
84 | a pointer to a virtual type.
|
---|
85 | The cast dynamically checks if the @EXPRESSION@ type is the same or a subtype
|
---|
86 | of @TYPE@, and if true, returns a pointer to the
|
---|
87 | @EXPRESSION@ object, otherwise it returns @0p@ (null pointer).
|
---|
88 |
|
---|
89 | \section{Exception}
|
---|
90 | % Leaving until later, hopefully it can talk about actual syntax instead
|
---|
91 | % of my many strange macros. Syntax aside I will also have to talk about the
|
---|
92 | % features all exceptions support.
|
---|
93 |
|
---|
94 | Exceptions are defined by the trait system; there are a series of traits, and
|
---|
95 | if a type satisfies them, then it can be used as an exception. The following
|
---|
96 | is the base trait all exceptions need to match.
|
---|
97 | \begin{cfa}
|
---|
98 | trait is_exception(exceptT &, virtualT &) {
|
---|
99 | virtualT const & get_exception_vtable(exceptT *);
|
---|
100 | };
|
---|
101 | \end{cfa}
|
---|
102 | The trait is defined over two types, the exception type and the virtual table
|
---|
103 | type. This should be one-to-one, each exception type has only one virtual
|
---|
104 | table type and vice versa. The only assertion in the trait is
|
---|
105 | @get_exception_vtable@, which takes a pointer of the exception type and
|
---|
106 | returns a reference to the virtual table type instance.
|
---|
107 |
|
---|
108 | The function @get_exception_vtable@ is actually a constant function.
|
---|
109 | Recardless of the value passed in (including the null pointer) it should
|
---|
110 | return a reference to the virtual table instance for that type.
|
---|
111 | The reason it is a function instead of a constant is that it make type
|
---|
112 | annotations easier to write as you can use the exception type instead of the
|
---|
113 | virtual table type; which usually has a mangled name.
|
---|
114 | % Also \CFA's trait system handles functions better than constants and doing
|
---|
115 | % it this way
|
---|
116 |
|
---|
117 | % I did have a note about how it is the programmer's responsibility to make
|
---|
118 | % sure the function is implemented correctly. But this is true of every
|
---|
119 | % similar system I know of (except Agda's I guess) so I took it out.
|
---|
120 |
|
---|
121 | \section{Raise}
|
---|
122 | \CFA provides two kinds of exception raise: termination
|
---|
123 | \see{\VRef{s:Termination}} and resumption \see{\VRef{s:Resumption}}, which are
|
---|
124 | specified with the following traits.
|
---|
125 | \begin{cfa}
|
---|
126 | trait is_termination_exception(
|
---|
127 | exceptT &, virtualT & | is_exception(exceptT, virtualT)) {
|
---|
128 | void defaultTerminationHandler(exceptT &);
|
---|
129 | };
|
---|
130 | \end{cfa}
|
---|
131 | The function is required to allow a termination raise, but is only called if a
|
---|
132 | termination raise does not find an appropriate handler.
|
---|
133 |
|
---|
134 | Allowing a resumption raise is similar.
|
---|
135 | \begin{cfa}
|
---|
136 | trait is_resumption_exception(
|
---|
137 | exceptT &, virtualT & | is_exception(exceptT, virtualT)) {
|
---|
138 | void defaultResumptionHandler(exceptT &);
|
---|
139 | };
|
---|
140 | \end{cfa}
|
---|
141 | The function is required to allow a resumption raise, but is only called if a
|
---|
142 | resumption raise does not find an appropriate handler.
|
---|
143 |
|
---|
144 | Finally there are three convenience macros for referring to the these traits:
|
---|
145 | @IS_EXCEPTION@, @IS_TERMINATION_EXCEPTION@ and @IS_RESUMPTION_EXCEPTION@.
|
---|
146 | All three traits are hard to use while naming the virtual table as it has an
|
---|
147 | internal mangled name. These macros take the exception name as their first
|
---|
148 | argument and do the mangling. They all take a second argument for polymorphic
|
---|
149 | types which is the parenthesized list of polymorphic arguments. These
|
---|
150 | arguments are passed to both the exception type and the virtual table type as
|
---|
151 | the arguments do have to match.
|
---|
152 |
|
---|
153 | For example consider a function that is polymorphic over types that have a
|
---|
154 | defined arithmetic exception:
|
---|
155 | \begin{cfa}
|
---|
156 | forall(Num | IS_EXCEPTION(Arithmetic, (Num)))
|
---|
157 | void some_math_function(Num & left, Num & right);
|
---|
158 | \end{cfa}
|
---|
159 |
|
---|
160 | \subsection{Termination}
|
---|
161 | \label{s:Termination}
|
---|
162 |
|
---|
163 | Termination raise, called ``throw'', is familiar and used in most programming
|
---|
164 | languages with exception handling. The semantics of termination is: search the
|
---|
165 | stack for a matching handler, unwind the stack frames to the matching handler,
|
---|
166 | execute the handler, and continue execution after the handler. Termination is
|
---|
167 | used when execution \emph{cannot} return to the throw. To continue execution,
|
---|
168 | the program must \emph{recover} in the handler from the failed (unwound)
|
---|
169 | execution at the raise to safely proceed after the handler.
|
---|
170 |
|
---|
171 | A termination raise is started with the @throw@ statement:
|
---|
172 | \begin{cfa}
|
---|
173 | throw EXPRESSION;
|
---|
174 | \end{cfa}
|
---|
175 | The expression must return a reference to a termination exception, where the
|
---|
176 | termination exception is any type that satifies @is_termination_exception@
|
---|
177 | at the call site.
|
---|
178 | Through \CFA's trait system the functions in the traits are passed into the
|
---|
179 | throw code. A new @defaultTerminationHandler@ can be defined in any scope to
|
---|
180 | change the throw's behavior (see below).
|
---|
181 |
|
---|
182 | At runtime, the exception returned by the expression
|
---|
183 | is copied into managed memory (heap) to ensure it remains in
|
---|
184 | scope during unwinding. It is the user's responsibility to ensure the original
|
---|
185 | exception object at the throw is freed when it goes out of scope. Being
|
---|
186 | allocated on the stack is sufficient for this.
|
---|
187 |
|
---|
188 | Then the exception system searches the stack starting from the throw and
|
---|
189 | proceeding towards the base of the stack, from callee to caller. At each stack
|
---|
190 | frame, a check is made for termination handlers defined by the @catch@ clauses
|
---|
191 | of a @try@ statement.
|
---|
192 | \begin{cfa}
|
---|
193 | try {
|
---|
194 | GUARDED_BLOCK
|
---|
195 | } catch (EXCEPTION_TYPE$\(_1\)$ * NAME$\(_1\)$) { // termination handler 1
|
---|
196 | HANDLER_BLOCK$\(_1\)$
|
---|
197 | } catch (EXCEPTION_TYPE$\(_2\)$ * NAME$\(_2\)$) { // termination handler 2
|
---|
198 | HANDLER_BLOCK$\(_2\)$
|
---|
199 | }
|
---|
200 | \end{cfa}
|
---|
201 | The statements in the @GUARDED_BLOCK@ are executed. If those statements, or any
|
---|
202 | functions invoked from those statements, throws an exception, and the exception
|
---|
203 | is not handled by a try statement further up the stack, the termination
|
---|
204 | handlers are searched for a matching exception type from top to bottom.
|
---|
205 |
|
---|
206 | Exception matching checks the representation of the thrown exception-type is
|
---|
207 | the same or a descendant type of the exception types in the handler clauses. If
|
---|
208 | it is the same of a descendent of @EXCEPTION_TYPE@$_i$ then @NAME@$_i$ is
|
---|
209 | bound to a pointer to the exception and the statements in @HANDLER_BLOCK@$_i$
|
---|
210 | are executed. If control reaches the end of the handler, the exception is
|
---|
211 | freed and control continues after the try statement.
|
---|
212 |
|
---|
213 | The default handler visible at the throw statement is used if no matching
|
---|
214 | termination handler is found after the entire stack is searched. At that point,
|
---|
215 | the default handler is called with a reference to the exception object
|
---|
216 | generated at the throw. If the default handler returns, control continues
|
---|
217 | from after the throw statement. This feature allows
|
---|
218 | each exception type to define its own action, such as printing an informative
|
---|
219 | error message, when an exception is not handled in the program.
|
---|
220 | However the default handler for all exception types triggers a cancellation
|
---|
221 | using the exception.
|
---|
222 |
|
---|
223 | \subsection{Resumption}
|
---|
224 | \label{s:Resumption}
|
---|
225 |
|
---|
226 | Resumption raise, called ``resume'', is as old as termination
|
---|
227 | raise~\cite{Goodenough75} but is less popular. In many ways, resumption is
|
---|
228 | simpler and easier to understand, as it is simply a dynamic call.
|
---|
229 | The semantics of resumption is: search the stack for a matching handler,
|
---|
230 | execute the handler, and continue execution after the resume. Notice, the stack
|
---|
231 | cannot be unwound because execution returns to the raise point. Resumption is
|
---|
232 | used used when execution \emph{can} return to the resume. To continue
|
---|
233 | execution, the program must \emph{correct} in the handler for the failed
|
---|
234 | execution at the raise so execution can safely continue after the resume.
|
---|
235 |
|
---|
236 | A resumption raise is started with the @throwResume@ statement:
|
---|
237 | \begin{cfa}
|
---|
238 | throwResume EXPRESSION;
|
---|
239 | \end{cfa}
|
---|
240 | The semantics of the @throwResume@ statement are like the @throw@, but the
|
---|
241 | expression has return a reference a type that satifies the trait
|
---|
242 | @is_resumption_exception@. Like with termination the exception system can
|
---|
243 | use these assertions while (throwing/raising/handling) the exception.
|
---|
244 |
|
---|
245 | At runtime, no copies are made. As the stack is not unwound the exception and
|
---|
246 | any values on the stack will remain in scope while the resumption is handled.
|
---|
247 |
|
---|
248 | Then the exception system searches the stack starting from the resume and
|
---|
249 | proceeding to the base of the stack, from callee to caller. At each stack
|
---|
250 | frame, a check is made for resumption handlers defined by the @catchResume@
|
---|
251 | clauses of a @try@ statement.
|
---|
252 | \begin{cfa}
|
---|
253 | try {
|
---|
254 | GUARDED_BLOCK
|
---|
255 | } catchResume (EXCEPTION_TYPE$\(_1\)$ * NAME$\(_1\)$) {
|
---|
256 | HANDLER_BLOCK$\(_1\)$
|
---|
257 | } catchResume (EXCEPTION_TYPE$\(_2\)$ * NAME$\(_2\)$) {
|
---|
258 | HANDLER_BLOCK$\(_2\)$
|
---|
259 | }
|
---|
260 | \end{cfa}
|
---|
261 | The statements in the @GUARDED_BLOCK@ are executed. If those statements, or any
|
---|
262 | functions invoked from those statements, resumes an exception, and the
|
---|
263 | exception is not handled by a try statement further up the stack, the
|
---|
264 | resumption handlers are searched for a matching exception type from top to
|
---|
265 | bottom. (Note, termination and resumption handlers may be intermixed in a @try@
|
---|
266 | statement but the kind of raise (throw/resume) only matches with the
|
---|
267 | corresponding kind of handler clause.)
|
---|
268 |
|
---|
269 | The exception search and matching for resumption is the same as for
|
---|
270 | termination, including exception inheritance. The difference is when control
|
---|
271 | reaches the end of the handler: the resumption handler returns after the resume
|
---|
272 | rather than after the try statement. The resume point assumes the handler has
|
---|
273 | corrected the problem so execution can safely continue.
|
---|
274 |
|
---|
275 | Like termination, if no resumption handler is found, the default handler
|
---|
276 | visible at the resume statement is called, and the system default action is
|
---|
277 | executed.
|
---|
278 |
|
---|
279 | For resumption, the exception system uses stack marking to partition the
|
---|
280 | resumption search. If another resumption exception is raised in a resumption
|
---|
281 | handler, the second exception search does not start at the point of the
|
---|
282 | original raise. (Remember the stack is not unwound and the current handler is
|
---|
283 | at the top of the stack.) The search for the second resumption starts at the
|
---|
284 | current point on the stack because new try statements may have been pushed by
|
---|
285 | the handler or functions called from the handler. If there is no match back to
|
---|
286 | the point of the current handler, the search skips\label{p:searchskip} the
|
---|
287 | stack frames already searched by the first resume and continues after
|
---|
288 | the try statement. The default handler always continues from default
|
---|
289 | handler associated with the point where the exception is created.
|
---|
290 |
|
---|
291 | % This might need a diagram. But it is an important part of the justification
|
---|
292 | % of the design of the traversal order.
|
---|
293 | \begin{verbatim}
|
---|
294 | throwResume2 ----------.
|
---|
295 | | |
|
---|
296 | generated from handler |
|
---|
297 | | |
|
---|
298 | handler |
|
---|
299 | | |
|
---|
300 | throwResume1 -----. :
|
---|
301 | | | :
|
---|
302 | try | : search skip
|
---|
303 | | | :
|
---|
304 | catchResume <----' :
|
---|
305 | | |
|
---|
306 | \end{verbatim}
|
---|
307 |
|
---|
308 | This resumption search pattern reflects the one for termination, and so
|
---|
309 | should come naturally to most programmers.
|
---|
310 | However, it avoids the \emph{recursive resumption} problem.
|
---|
311 | If parts of the stack are searched multiple times, loops
|
---|
312 | can easily form resulting in infinite recursion.
|
---|
313 |
|
---|
314 | Consider the trivial case:
|
---|
315 | \begin{cfa}
|
---|
316 | try {
|
---|
317 | throwResume (E &){}; // first
|
---|
318 | } catchResume(E *) {
|
---|
319 | throwResume (E &){}; // second
|
---|
320 | }
|
---|
321 | \end{cfa}
|
---|
322 | If this handler is ever used it will be placed on top of the stack above the
|
---|
323 | try statement. If the stack was not masked than the @throwResume@ in the
|
---|
324 | handler would always be caught by the handler, leading to an infinite loop.
|
---|
325 | Masking avoids this problem and other more complex versions of it involving
|
---|
326 | multiple handlers and exception types.
|
---|
327 |
|
---|
328 | Other masking stratagies could be used; such as masking the handlers that
|
---|
329 | have caught an exception. This one was choosen because it creates a symmetry
|
---|
330 | with termination (masked sections of the stack would be unwound with
|
---|
331 | termination) and having only one pattern to learn is easier.
|
---|
332 |
|
---|
333 | \section{Conditional Catch}
|
---|
334 | Both termination and resumption handler clauses can be given an additional
|
---|
335 | condition to further control which exceptions they handle:
|
---|
336 | \begin{cfa}
|
---|
337 | catch (EXCEPTION_TYPE * NAME ; @CONDITION@)
|
---|
338 | \end{cfa}
|
---|
339 | First, the same semantics is used to match the exception type. Second, if the
|
---|
340 | exception matches, @CONDITION@ is executed. The condition expression may
|
---|
341 | reference all names in scope at the beginning of the try block and @NAME@
|
---|
342 | introduced in the handler clause. If the condition is true, then the handler
|
---|
343 | matches. Otherwise, the exception search continues at the next appropriate kind
|
---|
344 | of handler clause in the try block.
|
---|
345 | \begin{cfa}
|
---|
346 | try {
|
---|
347 | f1 = open( ... );
|
---|
348 | f2 = open( ... );
|
---|
349 | ...
|
---|
350 | } catch( IOFailure * f ; fd( f ) == f1 ) {
|
---|
351 | // only handle IO failure for f1
|
---|
352 | }
|
---|
353 | \end{cfa}
|
---|
354 | Note, catching @IOFailure@, checking for @f1@ in the handler, and reraising the
|
---|
355 | exception if not @f1@ is different because the reraise does not examine any of
|
---|
356 | remaining handlers in the current try statement.
|
---|
357 |
|
---|
358 | \section{Reraise}
|
---|
359 | \color{red}{From Andrew: I recomend we talk about why the language doesn't
|
---|
360 | have rethrows/reraises instead.}
|
---|
361 |
|
---|
362 | \label{s:Reraise}
|
---|
363 | Within the handler block or functions called from the handler block, it is
|
---|
364 | possible to reraise the most recently caught exception with @throw@ or
|
---|
365 | @throwResume@, respective.
|
---|
366 | \begin{cfa}
|
---|
367 | try {
|
---|
368 | ...
|
---|
369 | } catch( ... ) {
|
---|
370 | ... throw; // rethrow
|
---|
371 | } catchResume( ... ) {
|
---|
372 | ... throwResume; // reresume
|
---|
373 | }
|
---|
374 | \end{cfa}
|
---|
375 | The only difference between a raise and a reraise is that reraise does not
|
---|
376 | create a new exception; instead it continues using the current exception, \ie
|
---|
377 | no allocation and copy. However the default handler is still set to the one
|
---|
378 | visible at the raise point, and hence, for termination could refer to data that
|
---|
379 | is part of an unwound stack frame. To prevent this problem, a new default
|
---|
380 | handler is generated that does a program-level abort.
|
---|
381 |
|
---|
382 | \section{Finally Clauses}
|
---|
383 | A @finally@ clause may be placed at the end of a @try@ statement.
|
---|
384 | \begin{cfa}
|
---|
385 | try {
|
---|
386 | GUARDED_BLOCK
|
---|
387 | } ... // any number or kind of handler clauses
|
---|
388 | ... finally {
|
---|
389 | FINALLY_BLOCK
|
---|
390 | }
|
---|
391 | \end{cfa}
|
---|
392 | The @FINALLY_BLOCK@ is executed when the try statement is removed from the
|
---|
393 | stack, including when the @GUARDED_BLOCK@ or any handler clause finishes or
|
---|
394 | during an unwind.
|
---|
395 | The only time the block is not executed is if the program is exited before
|
---|
396 | that happens.
|
---|
397 |
|
---|
398 | Execution of the finally block should always finish, meaning control runs off
|
---|
399 | the end of the block. This requirement ensures always continues as if the
|
---|
400 | finally clause is not present, \ie finally is for cleanup not changing control
|
---|
401 | flow. Because of this requirement, local control flow out of the finally block
|
---|
402 | is forbidden. The compiler precludes any @break@, @continue@, @fallthru@ or
|
---|
403 | @return@ that causes control to leave the finally block. Other ways to leave
|
---|
404 | the finally block, such as a long jump or termination are much harder to check,
|
---|
405 | and at best requiring additional run-time overhead, and so are discouraged.
|
---|
406 |
|
---|
407 | \section{Cancellation}
|
---|
408 | Cancellation is a stack-level abort, which can be thought of as as an
|
---|
409 | uncatchable termination. It unwinds the entirety of the current stack, and if
|
---|
410 | possible forwards the cancellation exception to a different stack.
|
---|
411 |
|
---|
412 | Cancellation is not an exception operation like termination or resumption.
|
---|
413 | There is no special statement for starting a cancellation; instead the standard
|
---|
414 | library function @cancel_stack@ is called passing an exception. Unlike a
|
---|
415 | raise, this exception is not used in matching only to pass information about
|
---|
416 | the cause of the cancellation.
|
---|
417 |
|
---|
418 | Handling of a cancellation depends on which stack is being cancelled.
|
---|
419 | \begin{description}
|
---|
420 | \item[Main Stack:]
|
---|
421 | The main stack is the one used by the program main at the start of execution,
|
---|
422 | and is the only stack in a sequential program. Even in a concurrent program
|
---|
423 | the main stack is only dependent on the environment that started the program.
|
---|
424 | Hence, when the main stack is cancelled there is nowhere else in the program
|
---|
425 | to notify. After the stack is unwound, there is a program-level abort.
|
---|
426 |
|
---|
427 | \item[Thread Stack:]
|
---|
428 | A thread stack is created for a @thread@ object or object that satisfies the
|
---|
429 | @is_thread@ trait. A thread only has two points of communication that must
|
---|
430 | happen: start and join. As the thread must be running to perform a
|
---|
431 | cancellation, it must occur after start and before join, so join is used
|
---|
432 | for communication here.
|
---|
433 | After the stack is unwound, the thread halts and waits for
|
---|
434 | another thread to join with it. The joining thread checks for a cancellation,
|
---|
435 | and if present, resumes exception @ThreadCancelled@.
|
---|
436 |
|
---|
437 | There is a subtle difference between the explicit join (@join@ function) and
|
---|
438 | implicit join (from a destructor call). The explicit join takes the default
|
---|
439 | handler (@defaultResumptionHandler@) from its calling context, which is used if
|
---|
440 | the exception is not caught. The implicit join does a program abort instead.
|
---|
441 |
|
---|
442 | This semantics is for safety. If an unwind is triggered while another unwind
|
---|
443 | is underway only one of them can proceed as they both want to ``consume'' the
|
---|
444 | stack. Letting both try to proceed leads to very undefined behaviour.
|
---|
445 | Both termination and cancellation involve unwinding and, since the default
|
---|
446 | @defaultResumptionHandler@ preforms a termination that could more easily
|
---|
447 | happen in an implicate join inside a destructor. So there is an error message
|
---|
448 | and an abort instead.
|
---|
449 |
|
---|
450 | The recommended way to avoid the abort is to handle the intial resumption
|
---|
451 | from the implicate join. If required you may put an explicate join inside a
|
---|
452 | finally clause to disable the check and use the local
|
---|
453 | @defaultResumptionHandler@ instead.
|
---|
454 |
|
---|
455 | \item[Coroutine Stack:] A coroutine stack is created for a @coroutine@ object
|
---|
456 | or object that satisfies the @is_coroutine@ trait. A coroutine only knows of
|
---|
457 | two other coroutines, its starter and its last resumer. The last resumer has
|
---|
458 | the tightest coupling to the coroutine it activated. Hence, cancellation of
|
---|
459 | the active coroutine is forwarded to the last resumer after the stack is
|
---|
460 | unwound, as the last resumer has the most precise knowledge about the current
|
---|
461 | execution. When the resumer restarts, it resumes exception
|
---|
462 | @CoroutineCancelled@, which is polymorphic over the coroutine type and has a
|
---|
463 | pointer to the cancelled coroutine.
|
---|
464 |
|
---|
465 | The resume function also has an assertion that the @defaultResumptionHandler@
|
---|
466 | for the exception. So it will use the default handler like a regular throw.
|
---|
467 | \end{description}
|
---|