[4706098c] | 1 | \chapter{Exception Features} |
---|
[553f8abe] | 2 | \label{c:features} |
---|
[4706098c] | 3 | |
---|
[4aba055] | 4 | This chapter covers the design and user interface of the \CFA EHM |
---|
| 5 | and begins with a general overview of EHMs. It is not a strict |
---|
| 6 | definition of all EHMs nor an exhaustive list of all possible features. |
---|
[21f2e92] | 7 | However it does cover the most common structure and features found in them. |
---|
[f6106a6] | 8 | |
---|
[4aba055] | 9 | \section{Overview of EHMs} |
---|
[4260566] | 10 | % We should cover what is an exception handling mechanism and what is an |
---|
| 11 | % exception before this. Probably in the introduction. Some of this could |
---|
| 12 | % move there. |
---|
[4aba055] | 13 | \subsection{Raise / Handle} |
---|
[4260566] | 14 | An exception operation has two main parts: raise and handle. |
---|
[6071efc] | 15 | These terms are sometimes known as throw and catch but this work uses |
---|
[4260566] | 16 | throw/catch as a particular kind of raise/handle. |
---|
[4aba055] | 17 | These are the two parts that the user writes and may |
---|
[f6106a6] | 18 | be the only two pieces of the EHM that have any syntax in the language. |
---|
[4260566] | 19 | |
---|
[4aba055] | 20 | \paragraph{Raise} |
---|
[f6106a6] | 21 | The raise is the starting point for exception handling. It marks the beginning |
---|
[4aba055] | 22 | of exception handling by raising an exception, which passes it to |
---|
[f6106a6] | 23 | the EHM. |
---|
[4260566] | 24 | |
---|
[f6106a6] | 25 | Some well known examples include the @throw@ statements of \Cpp and Java and |
---|
[21f2e92] | 26 | the \code{Python}{raise} statement from Python. In real systems a raise may |
---|
| 27 | preform some other work (such as memory management) but for the |
---|
[299b8b2] | 28 | purposes of this overview that can be ignored. |
---|
[4260566] | 29 | |
---|
[4aba055] | 30 | \paragraph{Handle} |
---|
[f6106a6] | 31 | The purpose of most exception operations is to run some user code to handle |
---|
| 32 | that exception. This code is given, with some other information, in a handler. |
---|
| 33 | |
---|
| 34 | A handler has three common features: the previously mentioned user code, a |
---|
[4aba055] | 35 | region of code they guard and an exception label/condition that matches |
---|
[f6106a6] | 36 | certain exceptions. |
---|
[4aba055] | 37 | Only raises inside the guarded region and raising exceptions that match the |
---|
[f6106a6] | 38 | label can be handled by a given handler. |
---|
[6071efc] | 39 | If multiple handlers could can handle an exception, |
---|
| 40 | EHMs will define a rule to pick one, such as ``best match" or ``first found". |
---|
[4260566] | 41 | |
---|
[f6106a6] | 42 | The @try@ statements of \Cpp, Java and Python are common examples. All three |
---|
[4aba055] | 43 | also show another common feature of handlers, they are grouped by the guarded |
---|
[f6106a6] | 44 | region. |
---|
| 45 | |
---|
[4aba055] | 46 | \subsection{Propagation} |
---|
[de47a9d] | 47 | After an exception is raised comes what is usually the biggest step for the |
---|
[4aba055] | 48 | EHM: finding and setting up the handler. The propagation from raise to |
---|
[f6106a6] | 49 | handler can be broken up into three different tasks: searching for a handler, |
---|
[21f2e92] | 50 | matching against the handler and installing the handler. |
---|
[de47a9d] | 51 | |
---|
[4aba055] | 52 | \paragraph{Searching} |
---|
[f6106a6] | 53 | The EHM begins by searching for handlers that might be used to handle |
---|
[de47a9d] | 54 | the exception. Searching is usually independent of the exception that was |
---|
[4aba055] | 55 | thrown as it looks for handlers that have the raise site in their guarded |
---|
[f6106a6] | 56 | region. |
---|
[4aba055] | 57 | The search includes handlers in the current function, as well as any in |
---|
| 58 | callers on the stack that have the function call in their guarded region. |
---|
[f6106a6] | 59 | |
---|
[4aba055] | 60 | \paragraph{Matching} |
---|
[f6106a6] | 61 | Each handler found has to be matched with the raised exception. The exception |
---|
[4aba055] | 62 | label defines a condition that is used with exception and decides if |
---|
[f6106a6] | 63 | there is a match or not. |
---|
| 64 | |
---|
[4aba055] | 65 | In languages where the first match is used, this step is intertwined with |
---|
| 66 | searching; a match check is preformed immediately after the search finds |
---|
[4260566] | 67 | a possible handler. |
---|
| 68 | |
---|
[4aba055] | 69 | \paragraph{Installing} |
---|
[f6106a6] | 70 | After a handler is chosen it must be made ready to run. |
---|
| 71 | The implementation can vary widely to fit with the rest of the |
---|
[de47a9d] | 72 | design of the EHM. The installation step might be trivial or it could be |
---|
[4260566] | 73 | the most expensive step in handling an exception. The latter tends to be the |
---|
| 74 | case when stack unwinding is involved. |
---|
[de47a9d] | 75 | |
---|
[6071efc] | 76 | If a matching handler is not guaranteed to be found, the EHM needs a |
---|
[4aba055] | 77 | different course of action for the case where no handler matches. |
---|
| 78 | This situation only occurs with unchecked exceptions as checked exceptions |
---|
| 79 | (such as in Java) can make the guarantee. |
---|
| 80 | This unhandled action is usually very general, such as aborting the program. |
---|
[4260566] | 81 | |
---|
[4aba055] | 82 | \paragraph{Hierarchy} |
---|
[f6106a6] | 83 | A common way to organize exceptions is in a hierarchical structure. |
---|
[4aba055] | 84 | This pattern comes from object-orientated languages where the |
---|
[4260566] | 85 | exception hierarchy is a natural extension of the object hierarchy. |
---|
| 86 | |
---|
| 87 | Consider the following hierarchy of exceptions: |
---|
[4706098c] | 88 | \begin{center} |
---|
[6a8208cb] | 89 | \input{exception-hierarchy} |
---|
[4706098c] | 90 | \end{center} |
---|
[de47a9d] | 91 | |
---|
[4aba055] | 92 | A handler labeled with any given exception can handle exceptions of that |
---|
[4260566] | 93 | type or any child type of that exception. The root of the exception hierarchy |
---|
[299b8b2] | 94 | (here \code{C}{exception}) acts as a catch-all, leaf types catch single types |
---|
[4260566] | 95 | and the exceptions in the middle can be used to catch different groups of |
---|
| 96 | related exceptions. |
---|
| 97 | |
---|
| 98 | This system has some notable advantages, such as multiple levels of grouping, |
---|
[de47a9d] | 99 | the ability for libraries to add new exception types and the isolation |
---|
[f6106a6] | 100 | between different sub-hierarchies. |
---|
| 101 | This design is used in \CFA even though it is not a object-orientated |
---|
[a6c45c6] | 102 | language; so different tools are used to create the hierarchy. |
---|
[4260566] | 103 | |
---|
| 104 | % Could I cite the rational for the Python IO exception rework? |
---|
| 105 | |
---|
[4aba055] | 106 | \subsection{Completion} |
---|
[6071efc] | 107 | After the handler has finished, the entire exception operation has to complete |
---|
[f6106a6] | 108 | and continue executing somewhere else. This step is usually simple, |
---|
| 109 | both logically and in its implementation, as the installation of the handler |
---|
| 110 | is usually set up to do most of the work. |
---|
[de47a9d] | 111 | |
---|
[f6106a6] | 112 | The EHM can return control to many different places, |
---|
[4aba055] | 113 | the most common are after the handler definition (termination) |
---|
| 114 | and after the raise (resumption). |
---|
[4260566] | 115 | |
---|
[4aba055] | 116 | \subsection{Communication} |
---|
[887fc79] | 117 | For effective exception handling, additional information is often passed |
---|
[4aba055] | 118 | from the raise to the handler and back again. |
---|
[f6106a6] | 119 | So far only communication of the exceptions' identity has been covered. |
---|
[4aba055] | 120 | A common communication method is putting fields into the exception instance |
---|
| 121 | and giving the handler access to them. |
---|
| 122 | Passing the exception by reference instead of by value can allow data to be |
---|
| 123 | passed in both directions. |
---|
[4260566] | 124 | |
---|
| 125 | \section{Virtuals} |
---|
[f6106a6] | 126 | Virtual types and casts are not part of \CFA's EHM nor are they required for |
---|
[a6c45c6] | 127 | any EHM. |
---|
[6071efc] | 128 | However, it is one of the best ways to support an exception hierarchy |
---|
[4aba055] | 129 | is via a virtual hierarchy and dispatch system. |
---|
[a6c45c6] | 130 | |
---|
[4aba055] | 131 | Ideally, the virtual system would have been part of \CFA before the work |
---|
[a6c45c6] | 132 | on exception handling began, but unfortunately it was not. |
---|
[4aba055] | 133 | Hence, only the features and framework needed for the EHM were |
---|
[a6c45c6] | 134 | designed and implemented. Other features were considered to ensure that |
---|
[4aba055] | 135 | the structure could accommodate other desirable features in the future |
---|
| 136 | but they were not implemented. |
---|
| 137 | The rest of this section will only discuss the implemented subset of the |
---|
| 138 | virtual system design. |
---|
[4260566] | 139 | |
---|
| 140 | The virtual system supports multiple ``trees" of types. Each tree is |
---|
| 141 | a simple hierarchy with a single root type. Each type in a tree has exactly |
---|
[f6106a6] | 142 | one parent -- except for the root type which has zero parents -- and any |
---|
[4260566] | 143 | number of children. |
---|
| 144 | Any type that belongs to any of these trees is called a virtual type. |
---|
| 145 | |
---|
| 146 | % A type's ancestors are its parent and its parent's ancestors. |
---|
| 147 | % The root type has no ancestors. |
---|
[4aba055] | 148 | % A type's descendants are its children and its children's descendants. |
---|
[4260566] | 149 | |
---|
[de47a9d] | 150 | Every virtual type also has a list of virtual members. Children inherit |
---|
| 151 | their parent's list of virtual members but may add new members to it. |
---|
[f6106a6] | 152 | It is important to note that these are virtual members, not virtual methods |
---|
| 153 | of object-orientated programming, and can be of any type. |
---|
[4aba055] | 154 | |
---|
[c21f5a9] | 155 | \CFA still supports virtual methods as a special case of virtual members. |
---|
[4aba055] | 156 | Function pointers that take a pointer to the virtual type are modified |
---|
[c21f5a9] | 157 | with each level of inheritance so that refers to the new type. |
---|
| 158 | This means an object can always be passed to a function in its virtual table |
---|
[21f2e92] | 159 | as if it were a method. |
---|
[4aba055] | 160 | \todo{Clarify (with an example) virtual methods.} |
---|
[4260566] | 161 | |
---|
[f6106a6] | 162 | Each virtual type has a unique id. |
---|
[4aba055] | 163 | This id and all the virtual members are combined |
---|
[de47a9d] | 164 | into a virtual table type. Each virtual type has a pointer to a virtual table |
---|
[4260566] | 165 | as a hidden field. |
---|
[4aba055] | 166 | \todo{Might need a diagram for virtual structure.} |
---|
[4260566] | 167 | |
---|
[f6106a6] | 168 | Up until this point the virtual system is similar to ones found in |
---|
| 169 | object-orientated languages but this where \CFA diverges. Objects encapsulate a |
---|
[08e75215] | 170 | single set of behaviours in each type, universally across the entire program, |
---|
[4aba055] | 171 | and indeed all programs that use that type definition. In this sense, the |
---|
[08e75215] | 172 | types are ``closed" and cannot be altered. |
---|
[de47a9d] | 173 | |
---|
[4aba055] | 174 | In \CFA, types do not encapsulate any behaviour. Traits are local and |
---|
| 175 | types can begin to satisfy a trait, stop satisfying a trait or satisfy the same |
---|
[f6106a6] | 176 | trait in a different way at any lexical location in the program. |
---|
[4aba055] | 177 | In this sense, they are ``open" as they can change at any time. |
---|
| 178 | This capability means it is impossible to pick a single set of functions |
---|
| 179 | that represent the type's implementation across the program. |
---|
[f6106a6] | 180 | |
---|
| 181 | \CFA side-steps this issue by not having a single virtual table for each |
---|
[4aba055] | 182 | type. A user can define virtual tables that are filled in at their |
---|
| 183 | declaration and given a name. Anywhere that name is visible, even if it is |
---|
| 184 | defined locally inside a function (although that means it does not have a |
---|
[f6106a6] | 185 | static lifetime), it can be used. |
---|
[4aba055] | 186 | Specifically, a virtual type is ``bound" to a virtual table that |
---|
[08e75215] | 187 | sets the virtual members for that object. The virtual members can be accessed |
---|
| 188 | through the object. |
---|
[4706098c] | 189 | |
---|
| 190 | While much of the virtual infrastructure is created, it is currently only used |
---|
| 191 | internally for exception handling. The only user-level feature is the virtual |
---|
[21f2e92] | 192 | cast, which is the same as the \Cpp \code{C++}{dynamic_cast}. |
---|
[7eb6eb5] | 193 | \label{p:VirtualCast} |
---|
[4706098c] | 194 | \begin{cfa} |
---|
[4a36b344] | 195 | (virtual TYPE)EXPRESSION |
---|
[4706098c] | 196 | \end{cfa} |
---|
[29c9b23] | 197 | Note, the syntax and semantics matches a C-cast, rather than the function-like |
---|
| 198 | \Cpp syntax for special casts. Both the type of @EXPRESSION@ and @TYPE@ must be |
---|
| 199 | a pointer to a virtual type. |
---|
[de47a9d] | 200 | The cast dynamically checks if the @EXPRESSION@ type is the same or a sub-type |
---|
[29c9b23] | 201 | of @TYPE@, and if true, returns a pointer to the |
---|
[4706098c] | 202 | @EXPRESSION@ object, otherwise it returns @0p@ (null pointer). |
---|
| 203 | |
---|
| 204 | \section{Exception} |
---|
[4a36b344] | 205 | % Leaving until later, hopefully it can talk about actual syntax instead |
---|
| 206 | % of my many strange macros. Syntax aside I will also have to talk about the |
---|
| 207 | % features all exceptions support. |
---|
| 208 | |
---|
[4706098c] | 209 | Exceptions are defined by the trait system; there are a series of traits, and |
---|
[1c1c180] | 210 | if a type satisfies them, then it can be used as an exception. The following |
---|
[4706098c] | 211 | is the base trait all exceptions need to match. |
---|
| 212 | \begin{cfa} |
---|
| 213 | trait is_exception(exceptT &, virtualT &) { |
---|
[a6c45c6] | 214 | // Numerous imaginary assertions. |
---|
[02b73ea] | 215 | }; |
---|
[4706098c] | 216 | \end{cfa} |
---|
[29c9b23] | 217 | The trait is defined over two types, the exception type and the virtual table |
---|
[4aba055] | 218 | type. Each exception type should have a single virtual table type. |
---|
| 219 | There are no actual assertions in this trait because the trait system |
---|
| 220 | cannot express them yet (adding such assertions would be part of |
---|
[a6c45c6] | 221 | completing the virtual system). The imaginary assertions would probably come |
---|
| 222 | from a trait defined by the virtual system, and state that the exception type |
---|
[4aba055] | 223 | is a virtual type, is a descendant of @exception_t@ (the base exception type) |
---|
[a6c45c6] | 224 | and note its virtual table type. |
---|
[29c9b23] | 225 | |
---|
| 226 | % I did have a note about how it is the programmer's responsibility to make |
---|
| 227 | % sure the function is implemented correctly. But this is true of every |
---|
[de47a9d] | 228 | % similar system I know of (except Agda's I guess) so I took it out. |
---|
| 229 | |
---|
[f6106a6] | 230 | There are two more traits for exceptions defined as follows: |
---|
[4706098c] | 231 | \begin{cfa} |
---|
[02b73ea] | 232 | trait is_termination_exception( |
---|
[4706098c] | 233 | exceptT &, virtualT & | is_exception(exceptT, virtualT)) { |
---|
[29c9b23] | 234 | void defaultTerminationHandler(exceptT &); |
---|
[02b73ea] | 235 | }; |
---|
| 236 | |
---|
| 237 | trait is_resumption_exception( |
---|
[4706098c] | 238 | exceptT &, virtualT & | is_exception(exceptT, virtualT)) { |
---|
[29c9b23] | 239 | void defaultResumptionHandler(exceptT &); |
---|
[02b73ea] | 240 | }; |
---|
[4706098c] | 241 | \end{cfa} |
---|
[4aba055] | 242 | Both traits ensure a pair of types are an exception type, its virtual table |
---|
| 243 | type |
---|
[f6106a6] | 244 | and defines one of the two default handlers. The default handlers are used |
---|
[df24d37] | 245 | as fallbacks and are discussed in detail in \vref{s:ExceptionHandling}. |
---|
[de47a9d] | 246 | |
---|
[f6106a6] | 247 | However, all three of these traits can be tricky to use directly. |
---|
| 248 | While there is a bit of repetition required, |
---|
[de47a9d] | 249 | the largest issue is that the virtual table type is mangled and not in a user |
---|
[f6106a6] | 250 | facing way. So these three macros are provided to wrap these traits to |
---|
| 251 | simplify referring to the names: |
---|
[29c9b23] | 252 | @IS_EXCEPTION@, @IS_TERMINATION_EXCEPTION@ and @IS_RESUMPTION_EXCEPTION@. |
---|
[1830a86] | 253 | |
---|
[f6106a6] | 254 | All three take one or two arguments. The first argument is the name of the |
---|
| 255 | exception type. The macro passes its unmangled and mangled form to the trait. |
---|
[1830a86] | 256 | The second (optional) argument is a parenthesized list of polymorphic |
---|
[f6106a6] | 257 | arguments. This argument is only used with polymorphic exceptions and the |
---|
| 258 | list is be passed to both types. |
---|
| 259 | In the current set-up, the two types always have the same polymorphic |
---|
| 260 | arguments so these macros can be used without losing flexibility. |
---|
[29c9b23] | 261 | |
---|
| 262 | For example consider a function that is polymorphic over types that have a |
---|
| 263 | defined arithmetic exception: |
---|
| 264 | \begin{cfa} |
---|
[de47a9d] | 265 | forall(Num | IS_EXCEPTION(Arithmetic, (Num))) |
---|
[29c9b23] | 266 | void some_math_function(Num & left, Num & right); |
---|
| 267 | \end{cfa} |
---|
[4706098c] | 268 | |
---|
[1830a86] | 269 | \section{Exception Handling} |
---|
[f6106a6] | 270 | \label{s:ExceptionHandling} |
---|
[4aba055] | 271 | As stated, |
---|
[21f2e92] | 272 | \CFA provides two kinds of exception handling: termination and resumption. |
---|
[f6106a6] | 273 | These twin operations are the core of \CFA's exception handling mechanism. |
---|
[21f2e92] | 274 | This section will cover the general patterns shared by the two operations and |
---|
| 275 | then go on to cover the details each individual operation. |
---|
[de47a9d] | 276 | |
---|
[f6106a6] | 277 | Both operations follow the same set of steps. |
---|
[21f2e92] | 278 | Both start with the user preforming a raise on an exception. |
---|
[4aba055] | 279 | Then the exception propagates up the stack. |
---|
[f6106a6] | 280 | If a handler is found the exception is caught and the handler is run. |
---|
[4aba055] | 281 | After that control continues at a raise-dependent location. |
---|
| 282 | If the search fails a default handler is run and, if it returns, then control |
---|
| 283 | continues after the raise. |
---|
[f6106a6] | 284 | |
---|
| 285 | This general description covers what the two kinds have in common. |
---|
[4aba055] | 286 | Differences include how propagation is preformed, where exception continues |
---|
[f6106a6] | 287 | after an exception is caught and handled and which default handler is run. |
---|
[1830a86] | 288 | |
---|
[4706098c] | 289 | \subsection{Termination} |
---|
| 290 | \label{s:Termination} |
---|
[f6106a6] | 291 | Termination handling is the familiar kind and used in most programming |
---|
[1830a86] | 292 | languages with exception handling. |
---|
[4aba055] | 293 | It is a dynamic, non-local goto. If the raised exception is matched and |
---|
| 294 | handled, the stack is unwound and control (usually) continues in the function |
---|
[f6106a6] | 295 | on the call stack that defined the handler. |
---|
| 296 | Termination is commonly used when an error has occurred and recovery is |
---|
| 297 | impossible locally. |
---|
[1830a86] | 298 | |
---|
| 299 | % (usually) Control can continue in the current function but then a different |
---|
| 300 | % control flow construct should be used. |
---|
[4706098c] | 301 | |
---|
[f6106a6] | 302 | A termination raise is started with the @throw@ statement: |
---|
[4706098c] | 303 | \begin{cfa} |
---|
[4a36b344] | 304 | throw EXPRESSION; |
---|
[4706098c] | 305 | \end{cfa} |
---|
[29c9b23] | 306 | The expression must return a reference to a termination exception, where the |
---|
[f6106a6] | 307 | termination exception is any type that satisfies the trait |
---|
| 308 | @is_termination_exception@ at the call site. |
---|
[4aba055] | 309 | Through \CFA's trait system, the trait functions are implicitly passed into the |
---|
[f6106a6] | 310 | throw code and the EHM. |
---|
| 311 | A new @defaultTerminationHandler@ can be defined in any scope to |
---|
[4aba055] | 312 | change the throw's behaviour (see below). |
---|
[de47a9d] | 313 | |
---|
[4aba055] | 314 | The throw copies the provided exception into managed memory to ensure |
---|
[21f2e92] | 315 | the exception is not destroyed if the stack is unwound. |
---|
[f6106a6] | 316 | It is the user's responsibility to ensure the original exception is cleaned |
---|
[4aba055] | 317 | up whether the stack is unwound or not. Allocating it on the stack is |
---|
[f6106a6] | 318 | usually sufficient. |
---|
[de47a9d] | 319 | |
---|
[4aba055] | 320 | % How to say propagation starts, its first sub-step is the search. |
---|
| 321 | Then propagation starts with the search. \CFA uses a ``first match" rule so |
---|
[21f2e92] | 322 | matching is preformed with the copied exception as the search continues. |
---|
[4aba055] | 323 | It starts from the throwing function and proceeds towards base of the stack, |
---|
[1830a86] | 324 | from callee to caller. |
---|
[de47a9d] | 325 | At each stack frame, a check is made for resumption handlers defined by the |
---|
[1830a86] | 326 | @catch@ clauses of a @try@ statement. |
---|
[4706098c] | 327 | \begin{cfa} |
---|
[4a36b344] | 328 | try { |
---|
[4706098c] | 329 | GUARDED_BLOCK |
---|
[f6106a6] | 330 | } catch (EXCEPTION_TYPE$\(_1\)$ * [NAME$\(_1\)$]) { |
---|
[4706098c] | 331 | HANDLER_BLOCK$\(_1\)$ |
---|
[f6106a6] | 332 | } catch (EXCEPTION_TYPE$\(_2\)$ * [NAME$\(_2\)$]) { |
---|
[4706098c] | 333 | HANDLER_BLOCK$\(_2\)$ |
---|
[4a36b344] | 334 | } |
---|
[4706098c] | 335 | \end{cfa} |
---|
[4aba055] | 336 | When viewed on its own, a try statement simply executes the statements |
---|
| 337 | in \snake{GUARDED_BLOCK} and when those are finished, |
---|
| 338 | the try statement finishes. |
---|
[de47a9d] | 339 | |
---|
| 340 | However, while the guarded statements are being executed, including any |
---|
[4aba055] | 341 | invoked functions, all the handlers in these statements are included in the |
---|
| 342 | search path. |
---|
| 343 | Hence, if a termination exception is raised these handlers may be matched |
---|
| 344 | against the exception and may handle it. |
---|
[f6106a6] | 345 | |
---|
| 346 | Exception matching checks the handler in each catch clause in the order |
---|
[4aba055] | 347 | they appear, top to bottom. If the representation of the raised exception type |
---|
[f6106a6] | 348 | is the same or a descendant of @EXCEPTION_TYPE@$_i$ then @NAME@$_i$ |
---|
[21f2e92] | 349 | (if provided) is |
---|
| 350 | bound to a pointer to the exception and the statements in @HANDLER_BLOCK@$_i$ |
---|
| 351 | are executed. If control reaches the end of the handler, the exception is |
---|
[de47a9d] | 352 | freed and control continues after the try statement. |
---|
[4706098c] | 353 | |
---|
[21f2e92] | 354 | If no termination handler is found during the search then the default handler |
---|
[4aba055] | 355 | (\defaultTerminationHandler) visible at the raise statement is run. |
---|
| 356 | Through \CFA's trait system the best match at the raise statement will be used. |
---|
| 357 | This function is run and is passed the copied exception. |
---|
| 358 | If the default handler is run control continues after the raise statement. |
---|
[1830a86] | 359 | |
---|
[f6106a6] | 360 | There is a global @defaultTerminationHandler@ that is polymorphic over all |
---|
[4aba055] | 361 | termination exception types. |
---|
| 362 | Since it is so general a more specific handler can be |
---|
| 363 | defined and is used for those types, effectively overriding the handler |
---|
| 364 | for a particular exception type. |
---|
[f6106a6] | 365 | The global default termination handler performs a cancellation |
---|
[df24d37] | 366 | (see \vref{s:Cancellation}) on the current stack with the copied exception. |
---|
[4706098c] | 367 | |
---|
| 368 | \subsection{Resumption} |
---|
| 369 | \label{s:Resumption} |
---|
| 370 | |
---|
[f6106a6] | 371 | Resumption exception handling is less common than termination but is |
---|
| 372 | just as old~\cite{Goodenough75} and is simpler in many ways. |
---|
| 373 | It is a dynamic, non-local function call. If the raised exception is |
---|
[4aba055] | 374 | matched a closure is taken from up the stack and executed, |
---|
| 375 | after which the raising function continues executing. |
---|
| 376 | The common uses for resumption exceptions include |
---|
| 377 | potentially repairable errors, where execution can continue in the same |
---|
| 378 | function once the error is corrected, and |
---|
| 379 | ignorable events, such as logging where nothing needs to happen and control |
---|
| 380 | should always continue from the same place. |
---|
[8483c39a] | 381 | |
---|
[4706098c] | 382 | A resumption raise is started with the @throwResume@ statement: |
---|
| 383 | \begin{cfa} |
---|
[4a36b344] | 384 | throwResume EXPRESSION; |
---|
[4706098c] | 385 | \end{cfa} |
---|
[4aba055] | 386 | \todo{Decide on a final set of keywords and use them everywhere.} |
---|
[f6106a6] | 387 | It works much the same way as the termination throw. |
---|
| 388 | The expression must return a reference to a resumption exception, |
---|
| 389 | where the resumption exception is any type that satisfies the trait |
---|
| 390 | @is_resumption_exception@ at the call site. |
---|
| 391 | The assertions from this trait are available to |
---|
[21f2e92] | 392 | the exception system while handling the exception. |
---|
[29c9b23] | 393 | |
---|
[21f2e92] | 394 | At run-time, no exception copy is made. |
---|
[4aba055] | 395 | Resumption does not unwind the stack nor otherwise remove values from the |
---|
| 396 | current scope, so there is no need to manage memory to keep things in scope. |
---|
[4706098c] | 397 | |
---|
[4aba055] | 398 | The EHM then begins propagation. The search starts from the raise in the |
---|
| 399 | resuming function and proceeds towards the base of the stack, |
---|
| 400 | from callee to caller. |
---|
[1830a86] | 401 | At each stack frame, a check is made for resumption handlers defined by the |
---|
| 402 | @catchResume@ clauses of a @try@ statement. |
---|
[4706098c] | 403 | \begin{cfa} |
---|
[4a36b344] | 404 | try { |
---|
[4706098c] | 405 | GUARDED_BLOCK |
---|
[f6106a6] | 406 | } catchResume (EXCEPTION_TYPE$\(_1\)$ * [NAME$\(_1\)$]) { |
---|
[4706098c] | 407 | HANDLER_BLOCK$\(_1\)$ |
---|
[f6106a6] | 408 | } catchResume (EXCEPTION_TYPE$\(_2\)$ * [NAME$\(_2\)$]) { |
---|
[4706098c] | 409 | HANDLER_BLOCK$\(_2\)$ |
---|
[4a36b344] | 410 | } |
---|
[4706098c] | 411 | \end{cfa} |
---|
[f6106a6] | 412 | % I wonder if there would be some good central place for this. |
---|
| 413 | Note that termination handlers and resumption handlers may be used together |
---|
| 414 | in a single try statement, intermixing @catch@ and @catchResume@ freely. |
---|
[4aba055] | 415 | Each type of handler only interacts with exceptions from the matching |
---|
| 416 | kind of raise. |
---|
| 417 | When a try statement is executed, it simply executes the statements in the |
---|
[21f2e92] | 418 | @GUARDED_BLOCK@ and then finishes. |
---|
[f6106a6] | 419 | |
---|
| 420 | However, while the guarded statements are being executed, including any |
---|
[4aba055] | 421 | invoked functions, all the handlers in these statements are included in the |
---|
| 422 | search path. |
---|
| 423 | Hence, if a resumption exception is raised these handlers may be matched |
---|
| 424 | against the exception and may handle it. |
---|
[f6106a6] | 425 | |
---|
| 426 | Exception matching checks the handler in each catch clause in the order |
---|
[4aba055] | 427 | they appear, top to bottom. If the representation of the raised exception type |
---|
[f6106a6] | 428 | is the same or a descendant of @EXCEPTION_TYPE@$_i$ then @NAME@$_i$ |
---|
| 429 | (if provided) is bound to a pointer to the exception and the statements in |
---|
| 430 | @HANDLER_BLOCK@$_i$ are executed. |
---|
| 431 | If control reaches the end of the handler, execution continues after the |
---|
| 432 | the raise statement that raised the handled exception. |
---|
[de47a9d] | 433 | |
---|
[4aba055] | 434 | Like termination, if no resumption handler is found during the search, |
---|
| 435 | the default handler (\defaultResumptionHandler) visible at the raise |
---|
| 436 | statement is called. It will use the best match at the raise sight according |
---|
| 437 | to \CFA's overloading rules. The default handler is |
---|
| 438 | passed the exception given to the raise. When the default handler finishes |
---|
[f6106a6] | 439 | execution continues after the raise statement. |
---|
[de47a9d] | 440 | |
---|
[21f2e92] | 441 | There is a global \defaultResumptionHandler{} is polymorphic over all |
---|
[4aba055] | 442 | resumption exceptions and preforms a termination throw on the exception. |
---|
[6071efc] | 443 | The \defaultTerminationHandler{} can be overridden by providing a new |
---|
[4aba055] | 444 | function that is a better match. |
---|
[1830a86] | 445 | |
---|
[f6106a6] | 446 | \subsubsection{Resumption Marking} |
---|
[df24d37] | 447 | \label{s:ResumptionMarking} |
---|
[1830a86] | 448 | A key difference between resumption and termination is that resumption does |
---|
[de47a9d] | 449 | not unwind the stack. A side effect that is that when a handler is matched |
---|
[21f2e92] | 450 | and run it's try block (the guarded statements) and every try statement |
---|
[4aba055] | 451 | searched before it are still on the stack. There presence can lead to |
---|
| 452 | the recursive resumption problem. |
---|
[1830a86] | 453 | |
---|
| 454 | The recursive resumption problem is any situation where a resumption handler |
---|
| 455 | ends up being called while it is running. |
---|
| 456 | Consider a trivial case: |
---|
| 457 | \begin{cfa} |
---|
| 458 | try { |
---|
| 459 | throwResume (E &){}; |
---|
| 460 | } catchResume(E *) { |
---|
| 461 | throwResume (E &){}; |
---|
| 462 | } |
---|
| 463 | \end{cfa} |
---|
[4aba055] | 464 | When this code is executed, the guarded @throwResume@ starts a |
---|
| 465 | search and matches the handler in the @catchResume@ clause. This |
---|
| 466 | call is placed on the stack above the try-block. The second raise then |
---|
| 467 | searches the same try block and puts another instance of the |
---|
| 468 | same handler on the stack leading to infinite recursion. |
---|
[1830a86] | 469 | |
---|
[4aba055] | 470 | While this situation is trivial and easy to avoid, much more complex cycles |
---|
[1830a86] | 471 | can form with multiple handlers and different exception types. |
---|
| 472 | |
---|
[4aba055] | 473 | To prevent all of these cases, a each try statement is ``marked" from the |
---|
| 474 | time the exception search reaches it to either when the exception is being |
---|
| 475 | handled completes the matching handler or when the search reaches the base |
---|
| 476 | of the stack. |
---|
| 477 | While a try statement is marked, its handlers are never matched, effectively |
---|
[21f2e92] | 478 | skipping over it to the next try statement. |
---|
[4a36b344] | 479 | |
---|
[6a8208cb] | 480 | \begin{center} |
---|
| 481 | \input{stack-marking} |
---|
| 482 | \end{center} |
---|
[de47a9d] | 483 | |
---|
[4aba055] | 484 | There are other sets of marking rules that could be used, |
---|
| 485 | for instance, marking just the handlers that caught the exception, |
---|
| 486 | would also prevent recursive resumption. |
---|
| 487 | However, these rules mirror what happens with termination. |
---|
[4706098c] | 488 | |
---|
[4aba055] | 489 | The try statements that are marked are the ones that would be removed from |
---|
| 490 | the stack if this was a termination exception, that is those on the stack |
---|
| 491 | between the handler and the raise statement. |
---|
| 492 | This symmetry applies to the default handler as well, as both kinds of |
---|
| 493 | default handlers are run at the raise statement, rather than (physically |
---|
| 494 | or logically) at the bottom of the stack. |
---|
| 495 | % In early development having the default handler happen after |
---|
| 496 | % unmarking was just more useful. We assume that will continue. |
---|
[4706098c] | 497 | |
---|
| 498 | \section{Conditional Catch} |
---|
[de47a9d] | 499 | Both termination and resumption handler clauses can be given an additional |
---|
| 500 | condition to further control which exceptions they handle: |
---|
[4706098c] | 501 | \begin{cfa} |
---|
[f6106a6] | 502 | catch (EXCEPTION_TYPE * [NAME] ; CONDITION) |
---|
[4706098c] | 503 | \end{cfa} |
---|
| 504 | First, the same semantics is used to match the exception type. Second, if the |
---|
| 505 | exception matches, @CONDITION@ is executed. The condition expression may |
---|
[de47a9d] | 506 | reference all names in scope at the beginning of the try block and @NAME@ |
---|
[1c1c180] | 507 | introduced in the handler clause. If the condition is true, then the handler |
---|
[1830a86] | 508 | matches. Otherwise, the exception search continues as if the exception type |
---|
| 509 | did not match. |
---|
[f6106a6] | 510 | |
---|
[4aba055] | 511 | The condition matching allows finer matching by checking |
---|
[f6106a6] | 512 | more kinds of information than just the exception type. |
---|
[4706098c] | 513 | \begin{cfa} |
---|
| 514 | try { |
---|
[f6106a6] | 515 | handle1 = open( f1, ... ); |
---|
| 516 | handle2 = open( f2, ... ); |
---|
| 517 | handle3 = open( f3, ... ); |
---|
[4706098c] | 518 | ... |
---|
[de47a9d] | 519 | } catch( IOFailure * f ; fd( f ) == f1 ) { |
---|
[f6106a6] | 520 | // Only handle IO failure for f1. |
---|
| 521 | } catch( IOFailure * f ; fd( f ) == f3 ) { |
---|
| 522 | // Only handle IO failure for f3. |
---|
[4706098c] | 523 | } |
---|
[f6106a6] | 524 | // Can't handle a failure relating to f2 here. |
---|
[4706098c] | 525 | \end{cfa} |
---|
[4aba055] | 526 | In this example the file that experienced the IO error is used to decide |
---|
[f6106a6] | 527 | which handler should be run, if any at all. |
---|
| 528 | |
---|
| 529 | \begin{comment} |
---|
| 530 | % I know I actually haven't got rid of them yet, but I'm going to try |
---|
| 531 | % to write it as if I had and see if that makes sense: |
---|
| 532 | \section{Reraising} |
---|
| 533 | \label{s:Reraising} |
---|
[4706098c] | 534 | Within the handler block or functions called from the handler block, it is |
---|
| 535 | possible to reraise the most recently caught exception with @throw@ or |
---|
[1830a86] | 536 | @throwResume@, respectively. |
---|
[4706098c] | 537 | \begin{cfa} |
---|
[29c9b23] | 538 | try { |
---|
| 539 | ... |
---|
| 540 | } catch( ... ) { |
---|
[1830a86] | 541 | ... throw; |
---|
[4706098c] | 542 | } catchResume( ... ) { |
---|
[1830a86] | 543 | ... throwResume; |
---|
[4706098c] | 544 | } |
---|
| 545 | \end{cfa} |
---|
| 546 | The only difference between a raise and a reraise is that reraise does not |
---|
| 547 | create a new exception; instead it continues using the current exception, \ie |
---|
| 548 | no allocation and copy. However the default handler is still set to the one |
---|
| 549 | visible at the raise point, and hence, for termination could refer to data that |
---|
| 550 | is part of an unwound stack frame. To prevent this problem, a new default |
---|
| 551 | handler is generated that does a program-level abort. |
---|
[f6106a6] | 552 | \end{comment} |
---|
| 553 | |
---|
| 554 | \subsection{Comparison with Reraising} |
---|
| 555 | A more popular way to allow handlers to match in more detail is to reraise |
---|
[4aba055] | 556 | the exception after it has been caught, if it could not be handled here. |
---|
| 557 | On the surface these two features seem interchangeable. |
---|
[f6106a6] | 558 | |
---|
[4aba055] | 559 | If @throw;@ (no argument) starts a termination reraise, |
---|
| 560 | which is the same as a raise but reuses the last caught exception, |
---|
| 561 | then these two statements have the same behaviour: |
---|
[f6106a6] | 562 | \begin{cfa} |
---|
| 563 | try { |
---|
| 564 | do_work_may_throw(); |
---|
| 565 | } catch(exception_t * exc ; can_handle(exc)) { |
---|
| 566 | handle(exc); |
---|
| 567 | } |
---|
| 568 | \end{cfa} |
---|
| 569 | |
---|
| 570 | \begin{cfa} |
---|
| 571 | try { |
---|
| 572 | do_work_may_throw(); |
---|
| 573 | } catch(exception_t * exc) { |
---|
| 574 | if (can_handle(exc)) { |
---|
| 575 | handle(exc); |
---|
| 576 | } else { |
---|
| 577 | throw; |
---|
| 578 | } |
---|
| 579 | } |
---|
| 580 | \end{cfa} |
---|
[4aba055] | 581 | That is, they will have the same behaviour in isolation. |
---|
| 582 | Two things can expose differences between these cases. |
---|
| 583 | |
---|
[6071efc] | 584 | One is the existence of multiple handlers on a single try statement. |
---|
[4aba055] | 585 | A reraise skips all later handlers on this try statement but a conditional |
---|
| 586 | catch does not. |
---|
| 587 | Hence, if an earlier handler contains a reraise later handlers are |
---|
| 588 | implicitly skipped, with a conditional catch they are not. |
---|
| 589 | Still, they are equivalently powerful, |
---|
[6071efc] | 590 | both can be used two mimic the behaviour of the other, |
---|
[4aba055] | 591 | as reraise can pack arbitrary code in the handler and conditional catches |
---|
| 592 | can put arbitrary code in the predicate. |
---|
[6071efc] | 593 | % I was struggling with a long explanation about some simple solutions, |
---|
[4aba055] | 594 | % like repeating a condition on later handlers, and the general solution of |
---|
| 595 | % merging everything together. I don't think it is useful though unless its |
---|
| 596 | % for a proof. |
---|
| 597 | % https://en.cppreference.com/w/cpp/language/throw |
---|
| 598 | |
---|
| 599 | The question then becomes ``Which is a better default?" |
---|
| 600 | We believe that not skipping possibly useful handlers is a better default. |
---|
| 601 | If a handler can handle an exception it should and if the handler can not |
---|
| 602 | handle the exception then it is probably safer to have that explicitly |
---|
| 603 | described in the handler itself instead of implicitly described by its |
---|
| 604 | ordering with other handlers. |
---|
| 605 | % Or you could just alter the semantics of the throw statement. The handler |
---|
| 606 | % index is in the exception so you could use it to know where to start |
---|
| 607 | % searching from in the current try statement. |
---|
| 608 | % No place for the `goto else;` metaphor. |
---|
| 609 | |
---|
| 610 | The other issue is all of the discussion above assumes that the only |
---|
| 611 | way to tell apart two raises is the exception being raised and the remaining |
---|
| 612 | search path. |
---|
| 613 | This is not true generally, the current state of the stack can matter in |
---|
| 614 | a number of cases, even only for a stack trace after an program abort. |
---|
| 615 | But \CFA has a much more significant need of the rest of the stack, the |
---|
| 616 | default handlers for both termination and resumption. |
---|
| 617 | |
---|
| 618 | % For resumption it turns out it is possible continue a raise after the |
---|
| 619 | % exception has been caught, as if it hadn't been caught in the first place. |
---|
| 620 | This becomes a problem combined with the stack unwinding used in termination |
---|
| 621 | exception handling. |
---|
| 622 | The stack is unwound before the handler is installed, and hence before any |
---|
| 623 | reraises can run. So if a reraise happens the previous stack is gone, |
---|
| 624 | the place on the stack where the default handler was supposed to run is gone, |
---|
| 625 | if the default handler was a local function it may have been unwound too. |
---|
| 626 | There is no reasonable way to restore that information, so the reraise has |
---|
| 627 | to be considered as a new raise. |
---|
| 628 | This is the strongest advantage conditional catches have over reraising, |
---|
| 629 | they happen before stack unwinding and avoid this problem. |
---|
| 630 | |
---|
| 631 | % The one possible disadvantage of conditional catch is that it runs user |
---|
| 632 | % code during the exception search. While this is a new place that user code |
---|
| 633 | % can be run destructors and finally clauses are already run during the stack |
---|
| 634 | % unwinding. |
---|
| 635 | % |
---|
| 636 | % https://www.cplusplus.com/reference/exception/current_exception/ |
---|
| 637 | % `exception_ptr current_exception() noexcept;` |
---|
| 638 | % https://www.python.org/dev/peps/pep-0343/ |
---|
[4a36b344] | 639 | |
---|
| 640 | \section{Finally Clauses} |
---|
[f6106a6] | 641 | \label{s:FinallyClauses} |
---|
[de47a9d] | 642 | Finally clauses are used to preform unconditional clean-up when leaving a |
---|
[f6106a6] | 643 | scope and are placed at the end of a try statement after any handler clauses: |
---|
[4706098c] | 644 | \begin{cfa} |
---|
[4a36b344] | 645 | try { |
---|
[4706098c] | 646 | GUARDED_BLOCK |
---|
[29c9b23] | 647 | } ... // any number or kind of handler clauses |
---|
| 648 | ... finally { |
---|
[4706098c] | 649 | FINALLY_BLOCK |
---|
[4a36b344] | 650 | } |
---|
[4706098c] | 651 | \end{cfa} |
---|
[29c9b23] | 652 | The @FINALLY_BLOCK@ is executed when the try statement is removed from the |
---|
[1830a86] | 653 | stack, including when the @GUARDED_BLOCK@ finishes, any termination handler |
---|
[21f2e92] | 654 | finishes or during an unwind. |
---|
[29c9b23] | 655 | The only time the block is not executed is if the program is exited before |
---|
[1830a86] | 656 | the stack is unwound. |
---|
[4706098c] | 657 | |
---|
| 658 | Execution of the finally block should always finish, meaning control runs off |
---|
[f6106a6] | 659 | the end of the block. This requirement ensures control always continues as if |
---|
| 660 | the finally clause is not present, \ie finally is for cleanup not changing |
---|
| 661 | control flow. |
---|
| 662 | Because of this requirement, local control flow out of the finally block |
---|
[1c1c180] | 663 | is forbidden. The compiler precludes any @break@, @continue@, @fallthru@ or |
---|
[4706098c] | 664 | @return@ that causes control to leave the finally block. Other ways to leave |
---|
| 665 | the finally block, such as a long jump or termination are much harder to check, |
---|
[f6106a6] | 666 | and at best requiring additional run-time overhead, and so are only |
---|
[1830a86] | 667 | discouraged. |
---|
| 668 | |
---|
[f6106a6] | 669 | Not all languages with unwinding have finally clauses. Notably \Cpp does |
---|
[4aba055] | 670 | without it as descructors, and the RAII design pattern, serve a similar role. |
---|
| 671 | Although destructors and finally clauses can be used in the same cases, |
---|
| 672 | they have their own strengths, similar to top-level function and lambda |
---|
| 673 | functions with closures. |
---|
| 674 | Destructors take more work for their first use, but if there is clean-up code |
---|
| 675 | that needs to be run every time a type is used they soon become much easier |
---|
| 676 | to set-up. |
---|
| 677 | On the other hand finally clauses capture the local context, so is easy to |
---|
| 678 | use when the clean-up is not dependent on the type of a variable or requires |
---|
| 679 | information from multiple variables. |
---|
| 680 | % To Peter: I think these are the main points you were going for. |
---|
[4a36b344] | 681 | |
---|
| 682 | \section{Cancellation} |
---|
[f6106a6] | 683 | \label{s:Cancellation} |
---|
[de47a9d] | 684 | Cancellation is a stack-level abort, which can be thought of as as an |
---|
[f6106a6] | 685 | uncatchable termination. It unwinds the entire current stack, and if |
---|
[de47a9d] | 686 | possible forwards the cancellation exception to a different stack. |
---|
[4706098c] | 687 | |
---|
[29c9b23] | 688 | Cancellation is not an exception operation like termination or resumption. |
---|
[4706098c] | 689 | There is no special statement for starting a cancellation; instead the standard |
---|
[1c1c180] | 690 | library function @cancel_stack@ is called passing an exception. Unlike a |
---|
[f6106a6] | 691 | raise, this exception is not used in matching only to pass information about |
---|
[4706098c] | 692 | the cause of the cancellation. |
---|
[21f2e92] | 693 | (This also means matching cannot fail so there is no default handler.) |
---|
[4706098c] | 694 | |
---|
[f6106a6] | 695 | After @cancel_stack@ is called the exception is copied into the EHM's memory |
---|
[4aba055] | 696 | and the current stack is unwound. |
---|
| 697 | The behaviour after that depends on the kind of stack being cancelled. |
---|
[a6c45c6] | 698 | |
---|
| 699 | \paragraph{Main Stack} |
---|
[4706098c] | 700 | The main stack is the one used by the program main at the start of execution, |
---|
[f6106a6] | 701 | and is the only stack in a sequential program. |
---|
| 702 | After the main stack is unwound there is a program-level abort. |
---|
| 703 | |
---|
[4aba055] | 704 | There are two reasons for these semantics. |
---|
| 705 | The first is that it had to do this abort. |
---|
[f6106a6] | 706 | in a sequential program as there is nothing else to notify and the simplicity |
---|
| 707 | of keeping the same behaviour in sequential and concurrent programs is good. |
---|
[4aba055] | 708 | Also, even in concurrent programs there may not currently be any other stacks |
---|
| 709 | and even if other stacks do exist, main has no way to know where they are. |
---|
[4706098c] | 710 | |
---|
[a6c45c6] | 711 | \paragraph{Thread Stack} |
---|
[f6106a6] | 712 | A thread stack is created for a \CFA @thread@ object or object that satisfies |
---|
| 713 | the @is_thread@ trait. |
---|
[4aba055] | 714 | After a thread stack is unwound, the exception is stored until another |
---|
[f6106a6] | 715 | thread attempts to join with it. Then the exception @ThreadCancelled@, |
---|
| 716 | which stores a reference to the thread and to the exception passed to the |
---|
[4aba055] | 717 | cancellation, is reported from the join to the joining thread. |
---|
[f6106a6] | 718 | There is one difference between an explicit join (with the @join@ function) |
---|
| 719 | and an implicit join (from a destructor call). The explicit join takes the |
---|
| 720 | default handler (@defaultResumptionHandler@) from its calling context while |
---|
[4aba055] | 721 | the implicit join provides its own; which does a program abort if the |
---|
[f6106a6] | 722 | @ThreadCancelled@ exception cannot be handled. |
---|
| 723 | |
---|
[4aba055] | 724 | The communication and synchronization are done here because threads only have |
---|
| 725 | two structural points (not dependent on user-code) where |
---|
| 726 | communication/synchronization happens: start and join. |
---|
[f6106a6] | 727 | Since a thread must be running to perform a cancellation (and cannot be |
---|
| 728 | cancelled from another stack), the cancellation must be after start and |
---|
[4aba055] | 729 | before the join, so join is used. |
---|
[f6106a6] | 730 | |
---|
| 731 | % TODO: Find somewhere to discuss unwind collisions. |
---|
| 732 | The difference between the explicit and implicit join is for safety and |
---|
| 733 | debugging. It helps prevent unwinding collisions by avoiding throwing from |
---|
| 734 | a destructor and prevents cascading the error across multiple threads if |
---|
| 735 | the user is not equipped to deal with it. |
---|
[33e1c91] | 736 | It is always possible to add an explicit join if that is the desired behaviour. |
---|
| 737 | |
---|
| 738 | With explicit join and a default handler that triggers a cancellation, it is |
---|
| 739 | possible to cascade an error across any number of threads, cleaning up each |
---|
| 740 | in turn, until the error is handled or the main thread is reached. |
---|
[f6106a6] | 741 | |
---|
[a6c45c6] | 742 | \paragraph{Coroutine Stack} |
---|
[f6106a6] | 743 | A coroutine stack is created for a @coroutine@ object or object that |
---|
| 744 | satisfies the @is_coroutine@ trait. |
---|
[4aba055] | 745 | After a coroutine stack is unwound, control returns to the @resume@ function |
---|
| 746 | that most recently resumed it. @resume@ reports a |
---|
[21f2e92] | 747 | @CoroutineCancelled@ exception, which contains a references to the cancelled |
---|
[f6106a6] | 748 | coroutine and the exception used to cancel it. |
---|
[4aba055] | 749 | The @resume@ function also takes the \defaultResumptionHandler{} from the |
---|
[21f2e92] | 750 | caller's context and passes it to the internal report. |
---|
[f6106a6] | 751 | |
---|
| 752 | A coroutine knows of two other coroutines, its starter and its last resumer. |
---|
[4aba055] | 753 | The starter has a much more distant connection, while the last resumer just |
---|
[f6106a6] | 754 | (in terms of coroutine state) called resume on this coroutine, so the message |
---|
| 755 | is passed to the latter. |
---|
[33e1c91] | 756 | |
---|
| 757 | With a default handler that triggers a cancellation, it is possible to |
---|
| 758 | cascade an error across any number of coroutines, cleaning up each in turn, |
---|
| 759 | until the error is handled or a thread stack is reached. |
---|