| 1 | \chapter{Exception Features}
|
|---|
| 2 | \label{c:features}
|
|---|
| 3 |
|
|---|
| 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.
|
|---|
| 7 | However, it does cover the most common structure and features found in them.
|
|---|
| 8 |
|
|---|
| 9 | \section{Overview of EHMs}
|
|---|
| 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.
|
|---|
| 13 | \subsection{Raise / Handle}
|
|---|
| 14 | An exception operation has two main parts: raise and handle.
|
|---|
| 15 | These terms are sometimes known as throw and catch but this work uses
|
|---|
| 16 | throw/catch as a particular kind of raise/handle.
|
|---|
| 17 | These are the two parts that the user writes and may
|
|---|
| 18 | be the only two pieces of the EHM that have any syntax in a language.
|
|---|
| 19 |
|
|---|
| 20 | \paragraph{Raise}
|
|---|
| 21 | The raise is the starting point for exception handling,
|
|---|
| 22 | by raising an exception, which passes it to
|
|---|
| 23 | the EHM.
|
|---|
| 24 |
|
|---|
| 25 | Some well known examples include the @throw@ statements of \Cpp and Java and
|
|---|
| 26 | the \code{Python}{raise} statement of Python. In real systems, a raise may
|
|---|
| 27 | perform some other work (such as memory management) but for the
|
|---|
| 28 | purposes of this overview that can be ignored.
|
|---|
| 29 |
|
|---|
| 30 | \paragraph{Handle}
|
|---|
| 31 | The primary purpose of an EHM is to run some user code to handle a raised
|
|---|
| 32 | exception. This code is given, along with some other information,
|
|---|
| 33 | in a handler.
|
|---|
| 34 |
|
|---|
| 35 | A handler has three common features: the previously mentioned user code, a
|
|---|
| 36 | region of code it guards and an exception label/condition that matches
|
|---|
| 37 | against the raised exception.
|
|---|
| 38 | Only raises inside the guarded region and raising exceptions that match the
|
|---|
| 39 | label can be handled by a given handler.
|
|---|
| 40 | If multiple handlers could can handle an exception,
|
|---|
| 41 | EHMs define a rule to pick one, such as ``best match" or ``first found".
|
|---|
| 42 |
|
|---|
| 43 | The @try@ statements of \Cpp, Java and Python are common examples. All three
|
|---|
| 44 | also show another common feature of handlers: they are grouped by the guarded
|
|---|
| 45 | region.
|
|---|
| 46 |
|
|---|
| 47 | \subsection{Propagation}
|
|---|
| 48 | After an exception is raised comes what is usually the biggest step for the
|
|---|
| 49 | EHM: finding and setting up the handler for execution.
|
|---|
| 50 | The propagation from raise to
|
|---|
| 51 | handler can be broken up into three different tasks: searching for a handler,
|
|---|
| 52 | matching against the handler and installing the handler.
|
|---|
| 53 |
|
|---|
| 54 | \paragraph{Searching}
|
|---|
| 55 | The EHM begins by searching for handlers that might be used to handle
|
|---|
| 56 | the exception.
|
|---|
| 57 | The search will find handlers that have the raise site in their guarded
|
|---|
| 58 | region.
|
|---|
| 59 | The search includes handlers in the current function, as well as any in
|
|---|
| 60 | callers on the stack that have the function call in their guarded region.
|
|---|
| 61 |
|
|---|
| 62 | \paragraph{Matching}
|
|---|
| 63 | Each handler found is with the raised exception. The exception
|
|---|
| 64 | label defines a condition that is used with the exception and decides if
|
|---|
| 65 | there is a match or not.
|
|---|
| 66 | %
|
|---|
| 67 | In languages where the first match is used, this step is intertwined with
|
|---|
| 68 | searching; a match check is performed immediately after the search finds
|
|---|
| 69 | a handler.
|
|---|
| 70 |
|
|---|
| 71 | \paragraph{Installing}
|
|---|
| 72 | After a handler is chosen, it must be made ready to run.
|
|---|
| 73 | The implementation can vary widely to fit with the rest of the
|
|---|
| 74 | design of the EHM. The installation step might be trivial or it could be
|
|---|
| 75 | the most expensive step in handling an exception. The latter tends to be the
|
|---|
| 76 | case when stack unwinding is involved.
|
|---|
| 77 |
|
|---|
| 78 | If a matching handler is not guaranteed to be found, the EHM needs a
|
|---|
| 79 | different course of action for this case.
|
|---|
| 80 | This situation only occurs with unchecked exceptions as checked exceptions
|
|---|
| 81 | (such as in Java) can make the guarantee.
|
|---|
| 82 | The unhandled action is usually very general, such as aborting the program.
|
|---|
| 83 |
|
|---|
| 84 | \paragraph{Hierarchy}
|
|---|
| 85 | A common way to organize exceptions is in a hierarchical structure.
|
|---|
| 86 | This pattern comes from object-orientated languages where the
|
|---|
| 87 | exception hierarchy is a natural extension of the object hierarchy.
|
|---|
| 88 |
|
|---|
| 89 | Consider the following exception hierarchy:
|
|---|
| 90 | \begin{center}
|
|---|
| 91 | \input{exception-hierarchy}
|
|---|
| 92 | \end{center}
|
|---|
| 93 | A handler labeled with any given exception can handle exceptions of that
|
|---|
| 94 | type or any child type of that exception. The root of the exception hierarchy
|
|---|
| 95 | (here \code{C}{exception}) acts as a catch-all, leaf types catch single types
|
|---|
| 96 | and the exceptions in the middle can be used to catch different groups of
|
|---|
| 97 | related exceptions.
|
|---|
| 98 |
|
|---|
| 99 | This system has some notable advantages, such as multiple levels of grouping,
|
|---|
| 100 | the ability for libraries to add new exception types and the isolation
|
|---|
| 101 | between different sub-hierarchies.
|
|---|
| 102 | This design is used in \CFA even though it is not a object-orientated
|
|---|
| 103 | language, so different tools are used to create the hierarchy.
|
|---|
| 104 |
|
|---|
| 105 | % Could I cite the rational for the Python IO exception rework?
|
|---|
| 106 |
|
|---|
| 107 | \subsection{Completion}
|
|---|
| 108 | After the handler has finished, the entire exception operation has to complete
|
|---|
| 109 | and continue executing somewhere else. This step is usually simple,
|
|---|
| 110 | both logically and in its implementation, as the installation of the handler
|
|---|
| 111 | is usually set up to do most of the work.
|
|---|
| 112 |
|
|---|
| 113 | The EHM can return control to many different places, where
|
|---|
| 114 | the most common are after the handler definition (termination)
|
|---|
| 115 | and after the raise (resumption).
|
|---|
| 116 |
|
|---|
| 117 | \subsection{Communication}
|
|---|
| 118 | For effective exception handling, additional information is often passed
|
|---|
| 119 | from the raise to the handler and back again.
|
|---|
| 120 | So far, only communication of the exception's identity is covered.
|
|---|
| 121 | A common communication method for adding information to an exception
|
|---|
| 122 | is putting fields into the exception instance
|
|---|
| 123 | and giving the handler access to them.
|
|---|
| 124 | % You can either have pointers/references in the exception, or have p/rs to
|
|---|
| 125 | % the exception when it doesn't have to be copied.
|
|---|
| 126 | Passing references or pointers allows data at the raise location to be
|
|---|
| 127 | updated, passing information in both directions.
|
|---|
| 128 |
|
|---|
| 129 | \section{Virtuals}
|
|---|
| 130 | \label{s:virtuals}
|
|---|
| 131 | %\todo{Maybe explain what "virtual" actually means.}
|
|---|
| 132 | Virtual types and casts are not part of \CFA's EHM nor are they required for
|
|---|
| 133 | an EHM.
|
|---|
| 134 | However, one of the best ways to support an exception hierarchy
|
|---|
| 135 | is via a virtual hierarchy and dispatch system.
|
|---|
| 136 | Ideally, the virtual system would have been part of \CFA before the work
|
|---|
| 137 | on exception handling began, but unfortunately it was not.
|
|---|
| 138 | Hence, only the features and framework needed for the EHM were
|
|---|
| 139 | designed and implemented for this thesis.
|
|---|
| 140 | Other features were considered to ensure that
|
|---|
| 141 | the structure could accommodate other desirable features in the future
|
|---|
| 142 | but are not implemented.
|
|---|
| 143 | The rest of this section only discusses the implemented subset of the
|
|---|
| 144 | virtual system design.
|
|---|
| 145 |
|
|---|
| 146 | The virtual system supports multiple ``trees" of types. Each tree is
|
|---|
| 147 | a simple hierarchy with a single root type. Each type in a tree has exactly
|
|---|
| 148 | one parent -- except for the root type which has zero parents -- and any
|
|---|
| 149 | number of children.
|
|---|
| 150 | Any type that belongs to any of these trees is called a virtual type.
|
|---|
| 151 | % A type's ancestors are its parent and its parent's ancestors.
|
|---|
| 152 | % The root type has no ancestors.
|
|---|
| 153 | % A type's descendants are its children and its children's descendants.
|
|---|
| 154 |
|
|---|
| 155 | For the purposes of illustration, a proposed, but unimplemented, syntax
|
|---|
| 156 | will be used. Each virtual type is represented by a trait with an annotation
|
|---|
| 157 | that makes it a virtual type. This annotation is empty for a root type, which
|
|---|
| 158 | creates a new tree:
|
|---|
| 159 | \begin{cfa}
|
|---|
| 160 | trait root_type(T) virtual() {}
|
|---|
| 161 | \end{cfa}
|
|---|
| 162 | The annotation may also refer to any existing virtual type to make this new
|
|---|
| 163 | type a child of that type and part of the same tree. The parent may itself
|
|---|
| 164 | be a child or a root type and may have any number of existing children.
|
|---|
| 165 |
|
|---|
| 166 | % OK, for some reason the b and t positioning options are reversed here.
|
|---|
| 167 | \begin{minipage}[b]{0.6\textwidth}
|
|---|
| 168 | \begin{cfa}
|
|---|
| 169 | trait child_a(T) virtual(root_type) {}
|
|---|
| 170 | trait grandchild(T) virtual(child_a) {}
|
|---|
| 171 | trait child_b(T) virtual(root_type) {}
|
|---|
| 172 | \end{cfa}
|
|---|
| 173 | \end{minipage}
|
|---|
| 174 | \begin{minipage}{0.4\textwidth}
|
|---|
| 175 | \begin{center}
|
|---|
| 176 | \input{virtual-tree}
|
|---|
| 177 | \end{center}
|
|---|
| 178 | \end{minipage}
|
|---|
| 179 |
|
|---|
| 180 | Every virtual type also has a list of virtual members and a unique id.
|
|---|
| 181 | Both are stored in a virtual table.
|
|---|
| 182 | Every instance of a virtual type also has a pointer to a virtual table stored
|
|---|
| 183 | in it, although there is no per-type virtual table as in many other languages.
|
|---|
| 184 |
|
|---|
| 185 | The list of virtual members is accumulated from the root type down the tree.
|
|---|
| 186 | Every virtual type
|
|---|
| 187 | inherits the list of virtual members from its parent and may add more
|
|---|
| 188 | virtual members to the end of the list which are passed on to its children.
|
|---|
| 189 | Again, using the unimplemented syntax this might look like:
|
|---|
| 190 | \begin{cfa}
|
|---|
| 191 | trait root_type(T) virtual() {
|
|---|
| 192 | const char * to_string(T const & this);
|
|---|
| 193 | unsigned int size;
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | trait child_type(T) virtual(root_type) {
|
|---|
| 197 | char * irrelevant_function(int, char);
|
|---|
| 198 | }
|
|---|
| 199 | \end{cfa}
|
|---|
| 200 | % Consider adding a diagram, but we might be good with the explanation.
|
|---|
| 201 |
|
|---|
| 202 | As @child_type@ is a child of @root_type@, it has the virtual members of
|
|---|
| 203 | @root_type@ (@to_string@ and @size@) as well as the one it declared
|
|---|
| 204 | (@irrelevant_function@).
|
|---|
| 205 |
|
|---|
| 206 | It is important to note that these are virtual members, and may contain
|
|---|
| 207 | arbitrary fields, functions or otherwise.
|
|---|
| 208 | The names ``size" and ``align" are reserved for the size and alignment of the
|
|---|
| 209 | virtual type, and are always automatically initialized as such.
|
|---|
| 210 | The other special case is uses of the trait's polymorphic argument
|
|---|
| 211 | (@T@ in the example), which are always updated to refer to the current
|
|---|
| 212 | virtual type. This allows functions that refer to the polymorphic argument
|
|---|
| 213 | to act as traditional virtual methods (@to_string@ in the example), as the
|
|---|
| 214 | object can always be passed to a virtual method in its virtual table.
|
|---|
| 215 |
|
|---|
| 216 | Up until this point, the virtual system is similar to ones found in
|
|---|
| 217 | object-oriented languages, but this is where \CFA diverges.
|
|---|
| 218 | Objects encapsulate a single set of methods in each type,
|
|---|
| 219 | universally across the entire program,
|
|---|
| 220 | and indeed all programs that use that type definition.
|
|---|
| 221 | The only way to change any method is to inherit and define a new type with
|
|---|
| 222 | its own universal implementation. In this sense,
|
|---|
| 223 | these object-oriented types are ``closed" and cannot be altered.
|
|---|
| 224 | % Because really they are class oriented.
|
|---|
| 225 |
|
|---|
| 226 | In \CFA, types do not encapsulate any code.
|
|---|
| 227 | Whether or not a type satisfies any given assertion, and hence any trait, is
|
|---|
| 228 | context sensitive. Types can begin to satisfy a trait, stop satisfying it or
|
|---|
| 229 | satisfy the same trait at any lexical location in the program.
|
|---|
| 230 | In this sense, a type's implementation in the set of functions and variables
|
|---|
| 231 | that allow it to satisfy a trait is ``open" and can change
|
|---|
| 232 | throughout the program.
|
|---|
| 233 | This capability means it is impossible to pick a single set of functions
|
|---|
| 234 | that represent a type's implementation across a program.
|
|---|
| 235 |
|
|---|
| 236 | \CFA side-steps this issue by not having a single virtual table for each
|
|---|
| 237 | type. A user can define virtual tables that are filled in at their
|
|---|
| 238 | declaration and given a name. Anywhere that name is visible, even if it is
|
|---|
| 239 | defined locally inside a function (although in this case the user must ensure
|
|---|
| 240 | it outlives any objects that use it), it can be used.
|
|---|
| 241 | Specifically, a virtual type is ``bound" to a virtual table that
|
|---|
| 242 | sets the virtual members for that object. The virtual members can be accessed
|
|---|
| 243 | through the object.
|
|---|
| 244 |
|
|---|
| 245 | This means virtual tables are declared and named in \CFA.
|
|---|
| 246 | They are declared as variables, using the type
|
|---|
| 247 | @vtable(VIRTUAL_TYPE)@ and any valid name. For example:
|
|---|
| 248 | \begin{cfa}
|
|---|
| 249 | vtable(virtual_type_name) table_name;
|
|---|
| 250 | \end{cfa}
|
|---|
| 251 |
|
|---|
| 252 | Like any variable, they may be forward declared with the @extern@ keyword.
|
|---|
| 253 | Forward declaring virtual tables is relatively common.
|
|---|
| 254 | Many virtual types have an ``obvious" implementation that works in most
|
|---|
| 255 | cases.
|
|---|
| 256 | A pattern that has appeared in the early work using virtuals is to
|
|---|
| 257 | implement a virtual table with the the obvious definition and place a forward
|
|---|
| 258 | declaration of it in the header beside the definition of the virtual type.
|
|---|
| 259 |
|
|---|
| 260 | Even on the full declaration, no initializer should be used.
|
|---|
| 261 | Initialization is automatic.
|
|---|
| 262 | The type id and special virtual members ``size" and ``align" only depend on
|
|---|
| 263 | the virtual type, which is fixed given the type of the virtual table, and
|
|---|
| 264 | so the compiler fills in a fixed value.
|
|---|
| 265 | The other virtual members are resolved using the best match to the member's
|
|---|
| 266 | name and type, in the same context as the virtual table is declared using
|
|---|
| 267 | \CFA's normal resolution rules.
|
|---|
| 268 |
|
|---|
| 269 | While much of the virtual infrastructure has been created,
|
|---|
| 270 | it is currently only used
|
|---|
| 271 | internally for exception handling. The only user-level feature is the virtual
|
|---|
| 272 | cast, which is the same as the \Cpp \code{C++}{dynamic_cast}.
|
|---|
| 273 | \label{p:VirtualCast}
|
|---|
| 274 | \begin{cfa}
|
|---|
| 275 | (virtual TYPE)EXPRESSION
|
|---|
| 276 | \end{cfa}
|
|---|
| 277 | Note, the syntax and semantics matches a C-cast, rather than the function-like
|
|---|
| 278 | \Cpp syntax for special casts. Both the type of @EXPRESSION@ and @TYPE@ must be
|
|---|
| 279 | pointers to virtual types.
|
|---|
| 280 | The cast dynamically checks if the @EXPRESSION@ type is the same or a sub-type
|
|---|
| 281 | of @TYPE@, and if true, returns a pointer to the
|
|---|
| 282 | @EXPRESSION@ object, otherwise it returns @0p@ (null pointer).
|
|---|
| 283 | This allows the expression to be used as both a cast and a type check.
|
|---|
| 284 |
|
|---|
| 285 | \section{Exceptions}
|
|---|
| 286 |
|
|---|
| 287 | The syntax for declaring an exception is the same as declaring a structure
|
|---|
| 288 | except the keyword:
|
|---|
| 289 | \begin{cfa}
|
|---|
| 290 | exception TYPE_NAME {
|
|---|
| 291 | FIELDS
|
|---|
| 292 | };
|
|---|
| 293 | \end{cfa}
|
|---|
| 294 |
|
|---|
| 295 | Fields are filled in the same way as a structure as well. However, an extra
|
|---|
| 296 | field is added that contains the pointer to the virtual table.
|
|---|
| 297 | It must be explicitly initialized by the user when the exception is
|
|---|
| 298 | constructed.
|
|---|
| 299 |
|
|---|
| 300 | Here is an example of declaring an exception type along with a virtual table,
|
|---|
| 301 | assuming the exception has an ``obvious" implementation and a default
|
|---|
| 302 | virtual table makes sense.
|
|---|
| 303 |
|
|---|
| 304 | \begin{minipage}[t]{0.4\textwidth}
|
|---|
| 305 | Header (.hfa):
|
|---|
| 306 | \begin{cfa}
|
|---|
| 307 | exception Example {
|
|---|
| 308 | int data;
|
|---|
| 309 | };
|
|---|
| 310 |
|
|---|
| 311 | extern vtable(Example)
|
|---|
| 312 | example_base_vtable;
|
|---|
| 313 | \end{cfa}
|
|---|
| 314 | \end{minipage}
|
|---|
| 315 | \begin{minipage}[t]{0.6\textwidth}
|
|---|
| 316 | Implementation (.cfa):
|
|---|
| 317 | \begin{cfa}
|
|---|
| 318 | vtable(Example) example_base_vtable
|
|---|
| 319 | \end{cfa}
|
|---|
| 320 | \vfil
|
|---|
| 321 | \end{minipage}
|
|---|
| 322 |
|
|---|
| 323 | %\subsection{Exception Details}
|
|---|
| 324 | This is the only interface needed when raising and handling exceptions.
|
|---|
| 325 | However, it is actually a shorthand for a more complex
|
|---|
| 326 | trait-based interface.
|
|---|
| 327 |
|
|---|
| 328 | The language views exceptions through a series of traits.
|
|---|
| 329 | If a type satisfies them, then it can be used as an exception. The following
|
|---|
| 330 | is the base trait all exceptions need to match.
|
|---|
| 331 | \begin{cfa}
|
|---|
| 332 | trait is_exception(exceptT &, virtualT &) {
|
|---|
| 333 | // Numerous imaginary assertions.
|
|---|
| 334 | };
|
|---|
| 335 | \end{cfa}
|
|---|
| 336 | The trait is defined over two types: the exception type and the virtual table
|
|---|
| 337 | type. Each exception type should have a single virtual table type.
|
|---|
| 338 | There are no actual assertions in this trait because the trait system
|
|---|
| 339 | cannot express them yet (adding such assertions would be part of
|
|---|
| 340 | completing the virtual system). The imaginary assertions would probably come
|
|---|
| 341 | from a trait defined by the virtual system, and state that the exception type
|
|---|
| 342 | is a virtual type,
|
|---|
| 343 | that that the type is a descendant of @exception_t@ (the base exception type)
|
|---|
| 344 | and allow the user to find the virtual table type.
|
|---|
| 345 |
|
|---|
| 346 | % I did have a note about how it is the programmer's responsibility to make
|
|---|
| 347 | % sure the function is implemented correctly. But this is true of every
|
|---|
| 348 | % similar system I know of (except Agda's I guess) so I took it out.
|
|---|
| 349 |
|
|---|
| 350 | There are two more traits for exceptions defined as follows:
|
|---|
| 351 | \begin{cfa}
|
|---|
| 352 | trait is_termination_exception(
|
|---|
| 353 | exceptT &, virtualT & | is_exception(exceptT, virtualT)) {
|
|---|
| 354 | void defaultTerminationHandler(exceptT &);
|
|---|
| 355 | };
|
|---|
| 356 |
|
|---|
| 357 | trait is_resumption_exception(
|
|---|
| 358 | exceptT &, virtualT & | is_exception(exceptT, virtualT)) {
|
|---|
| 359 | void defaultResumptionHandler(exceptT &);
|
|---|
| 360 | };
|
|---|
| 361 | \end{cfa}
|
|---|
| 362 | Both traits ensure a pair of types is an exception type and
|
|---|
| 363 | its virtual table type,
|
|---|
| 364 | and defines one of the two default handlers. The default handlers are used
|
|---|
| 365 | as fallbacks and are discussed in detail in \autoref{s:ExceptionHandling}.
|
|---|
| 366 |
|
|---|
| 367 | However, all three of these traits can be tricky to use directly.
|
|---|
| 368 | While there is a bit of repetition required,
|
|---|
| 369 | the largest issue is that the virtual table type is mangled and not in a user
|
|---|
| 370 | facing way. So, these three macros are provided to wrap these traits to
|
|---|
| 371 | simplify referring to the names:
|
|---|
| 372 | @IS_EXCEPTION@, @IS_TERMINATION_EXCEPTION@ and @IS_RESUMPTION_EXCEPTION@.
|
|---|
| 373 |
|
|---|
| 374 | All three take one or two arguments. The first argument is the name of the
|
|---|
| 375 | exception type. The macro passes its unmangled and mangled form to the trait.
|
|---|
| 376 | The second (optional) argument is a parenthesized list of polymorphic
|
|---|
| 377 | arguments. This argument is only used with polymorphic exceptions and the
|
|---|
| 378 | list is passed to both types.
|
|---|
| 379 | In the current set-up, the two types always have the same polymorphic
|
|---|
| 380 | arguments, so these macros can be used without losing flexibility.
|
|---|
| 381 |
|
|---|
| 382 | For example, consider a function that is polymorphic over types that have a
|
|---|
| 383 | defined arithmetic exception:
|
|---|
| 384 | \begin{cfa}
|
|---|
| 385 | forall(Num | IS_EXCEPTION(Arithmetic, (Num)))
|
|---|
| 386 | void some_math_function(Num & left, Num & right);
|
|---|
| 387 | \end{cfa}
|
|---|
| 388 |
|
|---|
| 389 | \section{Exception Handling}
|
|---|
| 390 | \label{s:ExceptionHandling}
|
|---|
| 391 | As stated,
|
|---|
| 392 | \CFA provides two kinds of exception handling: termination and resumption.
|
|---|
| 393 | These twin operations are the core of \CFA's exception handling mechanism.
|
|---|
| 394 | This section covers the general patterns shared by the two operations and
|
|---|
| 395 | then goes on to cover the details of each individual operation.
|
|---|
| 396 |
|
|---|
| 397 | Both operations follow the same set of steps.
|
|---|
| 398 | First, a user raises an exception.
|
|---|
| 399 | Second, the exception propagates up the stack, searching for a handler.
|
|---|
| 400 | Third, if a handler is found, the exception is caught and the handler is run.
|
|---|
| 401 | After that control continues at a raise-dependent location.
|
|---|
| 402 | As an alternate to the third step,
|
|---|
| 403 | if a handler is not found, a default handler is run and, if it returns,
|
|---|
| 404 | then control
|
|---|
| 405 | continues after the raise.
|
|---|
| 406 |
|
|---|
| 407 | The differences between the two operations include how propagation is
|
|---|
| 408 | performed, where execution continues after an exception is handled
|
|---|
| 409 | and which default handler is run.
|
|---|
| 410 |
|
|---|
| 411 | \subsection{Termination}
|
|---|
| 412 | \label{s:Termination}
|
|---|
| 413 | Termination handling is the familiar kind of handling
|
|---|
| 414 | used in most programming
|
|---|
| 415 | languages with exception handling.
|
|---|
| 416 | It is a dynamic, non-local goto. If the raised exception is matched and
|
|---|
| 417 | handled, the stack is unwound and control (usually) continues in the function
|
|---|
| 418 | on the call stack that defined the handler.
|
|---|
| 419 | Termination is commonly used when an error has occurred and recovery is
|
|---|
| 420 | impossible locally.
|
|---|
| 421 |
|
|---|
| 422 | % (usually) Control can continue in the current function but then a different
|
|---|
| 423 | % control flow construct should be used.
|
|---|
| 424 |
|
|---|
| 425 | A termination raise is started with the @throw@ statement:
|
|---|
| 426 | \begin{cfa}
|
|---|
| 427 | throw EXPRESSION;
|
|---|
| 428 | \end{cfa}
|
|---|
| 429 | The expression must return a reference to a termination exception, where the
|
|---|
| 430 | termination exception is any type that satisfies the trait
|
|---|
| 431 | @is_termination_exception@ at the call site.
|
|---|
| 432 | Through \CFA's trait system, the trait functions are implicitly passed into the
|
|---|
| 433 | throw code for use by the EHM.
|
|---|
| 434 | A new @defaultTerminationHandler@ can be defined in any scope to
|
|---|
| 435 | change the throw's behaviour when a handler is not found (see below).
|
|---|
| 436 |
|
|---|
| 437 | The throw copies the provided exception into managed memory to ensure
|
|---|
| 438 | the exception is not destroyed if the stack is unwound.
|
|---|
| 439 | It is the user's responsibility to ensure the original exception is cleaned
|
|---|
| 440 | up whether the stack is unwound or not. Allocating it on the stack is
|
|---|
| 441 | usually sufficient.
|
|---|
| 442 |
|
|---|
| 443 | % How to say propagation starts, its first sub-step is the search.
|
|---|
| 444 | Then propagation starts with the search. \CFA uses a ``first match" rule so
|
|---|
| 445 | matching is performed with the copied exception as the search key.
|
|---|
| 446 | It starts from the raise site and proceeds towards base of the stack,
|
|---|
| 447 | from callee to caller.
|
|---|
| 448 | At each stack frame, a check is made for termination handlers defined by the
|
|---|
| 449 | @catch@ clauses of a @try@ statement.
|
|---|
| 450 | \begin{cfa}
|
|---|
| 451 | try {
|
|---|
| 452 | GUARDED_BLOCK
|
|---|
| 453 | } catch (EXCEPTION_TYPE$\(_1\)$ * [NAME$\(_1\)$]) {
|
|---|
| 454 | HANDLER_BLOCK$\(_1\)$
|
|---|
| 455 | } catch (EXCEPTION_TYPE$\(_2\)$ * [NAME$\(_2\)$]) {
|
|---|
| 456 | HANDLER_BLOCK$\(_2\)$
|
|---|
| 457 | }
|
|---|
| 458 | \end{cfa}
|
|---|
| 459 | When viewed on its own, a try statement simply executes the statements
|
|---|
| 460 | in the \snake{GUARDED_BLOCK} and when those are finished,
|
|---|
| 461 | the try statement finishes.
|
|---|
| 462 |
|
|---|
| 463 | However, while the guarded statements are being executed, including any
|
|---|
| 464 | invoked functions, all the handlers in these statements are included in the
|
|---|
| 465 | search path.
|
|---|
| 466 | Hence, if a termination exception is raised, these handlers may be matched
|
|---|
| 467 | against the exception and may handle it.
|
|---|
| 468 |
|
|---|
| 469 | Exception matching checks the handler in each catch clause in the order
|
|---|
| 470 | they appear, top to bottom. If the representation of the raised exception type
|
|---|
| 471 | is the same or a descendant of @EXCEPTION_TYPE@$_i$, then @NAME@$_i$
|
|---|
| 472 | (if provided) is
|
|---|
| 473 | bound to a pointer to the exception and the statements in @HANDLER_BLOCK@$_i$
|
|---|
| 474 | are executed. If control reaches the end of the handler, the exception is
|
|---|
| 475 | freed and control continues after the try statement.
|
|---|
| 476 |
|
|---|
| 477 | If no termination handler is found during the search, then the default handler
|
|---|
| 478 | (\defaultTerminationHandler) visible at the raise statement is called.
|
|---|
| 479 | Through \CFA's trait system the best match at the raise statement is used.
|
|---|
| 480 | This function is run and is passed the copied exception.
|
|---|
| 481 | If the default handler finishes, control continues after the raise statement.
|
|---|
| 482 |
|
|---|
| 483 | There is a global @defaultTerminationHandler@ that is polymorphic over all
|
|---|
| 484 | termination exception types.
|
|---|
| 485 | The global default termination handler performs a cancellation
|
|---|
| 486 | (as described in \vref{s:Cancellation})
|
|---|
| 487 | on the current stack with the copied exception.
|
|---|
| 488 | Since it is so general, a more specific handler can be defined,
|
|---|
| 489 | overriding the default behaviour for the specific exception types.
|
|---|
| 490 | %\todo{Examples?}
|
|---|
| 491 |
|
|---|
| 492 | \subsection{Resumption}
|
|---|
| 493 | \label{s:Resumption}
|
|---|
| 494 |
|
|---|
| 495 | Resumption exception handling is less familar form of exception handling,
|
|---|
| 496 | but is
|
|---|
| 497 | just as old~\cite{Goodenough75} and is simpler in many ways.
|
|---|
| 498 | It is a dynamic, non-local function call. If the raised exception is
|
|---|
| 499 | matched, a closure is taken from up the stack and executed,
|
|---|
| 500 | after which the raising function continues executing.
|
|---|
| 501 | The common uses for resumption exceptions include
|
|---|
| 502 | potentially repairable errors, where execution can continue in the same
|
|---|
| 503 | function once the error is corrected, and
|
|---|
| 504 | ignorable events, such as logging where nothing needs to happen and control
|
|---|
| 505 | should always continue from the raise site.
|
|---|
| 506 |
|
|---|
| 507 | Except for the changes to fit into that pattern, resumption exception
|
|---|
| 508 | handling is symmetric with termination exception handling, by design
|
|---|
| 509 | (see \autoref{s:Termination}).
|
|---|
| 510 |
|
|---|
| 511 | A resumption raise is started with the @throwResume@ statement:
|
|---|
| 512 | \begin{cfa}
|
|---|
| 513 | throwResume EXPRESSION;
|
|---|
| 514 | \end{cfa}
|
|---|
| 515 | % The new keywords are currently ``experimental" and not used in this work.
|
|---|
| 516 | It works much the same way as the termination raise, except the
|
|---|
| 517 | type must satisfy the \snake{is_resumption_exception} that uses the
|
|---|
| 518 | default handler: \defaultResumptionHandler.
|
|---|
| 519 | This can be specialized for particular exception types.
|
|---|
| 520 |
|
|---|
| 521 | At run-time, no exception copy is made. Since
|
|---|
| 522 | resumption does not unwind the stack nor otherwise remove values from the
|
|---|
| 523 | current scope, there is no need to manage memory to keep the exception
|
|---|
| 524 | allocated.
|
|---|
| 525 |
|
|---|
| 526 | Then propagation starts with the search,
|
|---|
| 527 | following the same search path as termination,
|
|---|
| 528 | from the raise site to the base of stack and top of try statement to bottom.
|
|---|
| 529 | However, the handlers on try statements are defined by @catchResume@ clauses.
|
|---|
| 530 | \begin{cfa}
|
|---|
| 531 | try {
|
|---|
| 532 | GUARDED_BLOCK
|
|---|
| 533 | } catchResume (EXCEPTION_TYPE$\(_1\)$ * [NAME$\(_1\)$]) {
|
|---|
| 534 | HANDLER_BLOCK$\(_1\)$
|
|---|
| 535 | } catchResume (EXCEPTION_TYPE$\(_2\)$ * [NAME$\(_2\)$]) {
|
|---|
| 536 | HANDLER_BLOCK$\(_2\)$
|
|---|
| 537 | }
|
|---|
| 538 | \end{cfa}
|
|---|
| 539 | Note that termination handlers and resumption handlers may be used together
|
|---|
| 540 | in a single try statement, intermixing @catch@ and @catchResume@ freely.
|
|---|
| 541 | Each type of handler only interacts with exceptions from the matching
|
|---|
| 542 | kind of raise.
|
|---|
| 543 | Like @catch@ clauses, @catchResume@ clauses have no effect if an exception
|
|---|
| 544 | is not raised.
|
|---|
| 545 |
|
|---|
| 546 | The matching rules are exactly the same as well.
|
|---|
| 547 | The first major difference here is that after
|
|---|
| 548 | @EXCEPTION_TYPE@$_i$ is matched and @NAME@$_i$ is bound to the exception,
|
|---|
| 549 | @HANDLER_BLOCK@$_i$ is executed right away without first unwinding the stack.
|
|---|
| 550 | After the block has finished running, control jumps to the raise site, where
|
|---|
| 551 | the just handled exception came from, and continues executing after it,
|
|---|
| 552 | not after the try statement.
|
|---|
| 553 | %\todo{Examples?}
|
|---|
| 554 |
|
|---|
| 555 | \subsubsection{Resumption Marking}
|
|---|
| 556 | \label{s:ResumptionMarking}
|
|---|
| 557 | A key difference between resumption and termination is that resumption does
|
|---|
| 558 | not unwind the stack. A side effect is that, when a handler is matched
|
|---|
| 559 | and run, its try block (the guarded statements) and every try statement
|
|---|
| 560 | searched before it are still on the stack. Their presence can lead to
|
|---|
| 561 | the recursive resumption problem.\cite{Buhr00a}
|
|---|
| 562 | % Other possible citation is MacLaren77, but the form is different.
|
|---|
| 563 |
|
|---|
| 564 | The recursive resumption problem is any situation where a resumption handler
|
|---|
| 565 | ends up being called while it is running.
|
|---|
| 566 | Consider a trivial case:
|
|---|
| 567 | \begin{cfa}
|
|---|
| 568 | try {
|
|---|
| 569 | throwResume (E &){};
|
|---|
| 570 | } catchResume(E *) {
|
|---|
| 571 | throwResume (E &){};
|
|---|
| 572 | }
|
|---|
| 573 | \end{cfa}
|
|---|
| 574 | When this code is executed, the guarded @throwResume@ starts a
|
|---|
| 575 | search and matches the handler in the @catchResume@ clause. This
|
|---|
| 576 | call is placed on the stack above the try-block.
|
|---|
| 577 | Now the second raise in the handler searches the same try block,
|
|---|
| 578 | matches again and then puts another instance of the
|
|---|
| 579 | same handler on the stack leading to infinite recursion.
|
|---|
| 580 |
|
|---|
| 581 | While this situation is trivial and easy to avoid, much more complex cycles
|
|---|
| 582 | can form with multiple handlers and different exception types.
|
|---|
| 583 | To prevent all of these cases, each try statement is ``marked" from the
|
|---|
| 584 | time the exception search reaches it to either when a handler completes
|
|---|
| 585 | handling that exception or when the search reaches the base
|
|---|
| 586 | of the stack.
|
|---|
| 587 | While a try statement is marked, its handlers are never matched, effectively
|
|---|
| 588 | skipping over it to the next try statement.
|
|---|
| 589 |
|
|---|
| 590 | \begin{center}
|
|---|
| 591 | \input{stack-marking}
|
|---|
| 592 | \end{center}
|
|---|
| 593 |
|
|---|
| 594 | There are other sets of marking rules that could be used.
|
|---|
| 595 | For instance, marking just the handlers that caught the exception
|
|---|
| 596 | would also prevent recursive resumption.
|
|---|
| 597 | However, the rules selected mirror what happens with termination,
|
|---|
| 598 | so this reduces the amount of rules and patterns a programmer has to know.
|
|---|
| 599 |
|
|---|
| 600 | The marked try statements are the ones that would be removed from
|
|---|
| 601 | the stack for a termination exception, \ie those on the stack
|
|---|
| 602 | between the handler and the raise statement.
|
|---|
| 603 | This symmetry applies to the default handler as well, as both kinds of
|
|---|
| 604 | default handlers are run at the raise statement, rather than (physically
|
|---|
| 605 | or logically) at the bottom of the stack.
|
|---|
| 606 | % In early development having the default handler happen after
|
|---|
| 607 | % unmarking was just more useful. We assume that will continue.
|
|---|
| 608 |
|
|---|
| 609 | \section{Conditional Catch}
|
|---|
| 610 | Both termination and resumption handler clauses can be given an additional
|
|---|
| 611 | condition to further control which exceptions they handle:
|
|---|
| 612 | \begin{cfa}
|
|---|
| 613 | catch (EXCEPTION_TYPE * [NAME] ; CONDITION)
|
|---|
| 614 | \end{cfa}
|
|---|
| 615 | First, the same semantics is used to match the exception type. Second, if the
|
|---|
| 616 | exception matches, @CONDITION@ is executed. The condition expression may
|
|---|
| 617 | reference all names in scope at the beginning of the try block and @NAME@
|
|---|
| 618 | introduced in the handler clause. If the condition is true, then the handler
|
|---|
| 619 | matches. Otherwise, the exception search continues as if the exception type
|
|---|
| 620 | did not match.
|
|---|
| 621 |
|
|---|
| 622 | The condition matching allows finer matching by checking
|
|---|
| 623 | more kinds of information than just the exception type.
|
|---|
| 624 | \begin{cfa}
|
|---|
| 625 | try {
|
|---|
| 626 | handle1 = open( f1, ... );
|
|---|
| 627 | handle2 = open( f2, ... );
|
|---|
| 628 | handle3 = open( f3, ... );
|
|---|
| 629 | ...
|
|---|
| 630 | } catch( IOFailure * f ; fd( f ) == f1 ) {
|
|---|
| 631 | // Only handle IO failure for f1.
|
|---|
| 632 | } catch( IOFailure * f ; fd( f ) == f3 ) {
|
|---|
| 633 | // Only handle IO failure for f3.
|
|---|
| 634 | }
|
|---|
| 635 | // Handle a failure relating to f2 further down the stack.
|
|---|
| 636 | \end{cfa}
|
|---|
| 637 | In this example, the file that experienced the IO error is used to decide
|
|---|
| 638 | which handler should be run, if any at all.
|
|---|
| 639 |
|
|---|
| 640 | \begin{comment}
|
|---|
| 641 | % I know I actually haven't got rid of them yet, but I'm going to try
|
|---|
| 642 | % to write it as if I had and see if that makes sense:
|
|---|
| 643 | \section{Reraising}
|
|---|
| 644 | \label{s:Reraising}
|
|---|
| 645 | Within the handler block or functions called from the handler block, it is
|
|---|
| 646 | possible to reraise the most recently caught exception with @throw@ or
|
|---|
| 647 | @throwResume@, respectively.
|
|---|
| 648 | \begin{cfa}
|
|---|
| 649 | try {
|
|---|
| 650 | ...
|
|---|
| 651 | } catch( ... ) {
|
|---|
| 652 | ... throw;
|
|---|
| 653 | } catchResume( ... ) {
|
|---|
| 654 | ... throwResume;
|
|---|
| 655 | }
|
|---|
| 656 | \end{cfa}
|
|---|
| 657 | The only difference between a raise and a reraise is that reraise does not
|
|---|
| 658 | create a new exception; instead it continues using the current exception, \ie
|
|---|
| 659 | no allocation and copy. However the default handler is still set to the one
|
|---|
| 660 | visible at the raise point, and hence, for termination could refer to data that
|
|---|
| 661 | is part of an unwound stack frame. To prevent this problem, a new default
|
|---|
| 662 | handler is generated that does a program-level abort.
|
|---|
| 663 | \end{comment}
|
|---|
| 664 |
|
|---|
| 665 | \subsection{Comparison with Reraising}
|
|---|
| 666 | In languages without conditional catch -- that is, no ability to match an
|
|---|
| 667 | exception based on something other than its type -- it can be mimicked
|
|---|
| 668 | by matching all exceptions of the right type, checking any additional
|
|---|
| 669 | conditions inside the handler and re-raising the exception if it does not
|
|---|
| 670 | match those.
|
|---|
| 671 |
|
|---|
| 672 | Here is a minimal example comparing both patterns, using @throw;@
|
|---|
| 673 | (no operand) to start a re-raise.
|
|---|
| 674 | \begin{center}
|
|---|
| 675 | \begin{tabular}{l r}
|
|---|
| 676 | \begin{cfa}
|
|---|
| 677 | try {
|
|---|
| 678 | do_work_may_throw();
|
|---|
| 679 | } catch(exception_t * exc ;
|
|---|
| 680 | can_handle(exc)) {
|
|---|
| 681 | handle(exc);
|
|---|
| 682 | }
|
|---|
| 683 |
|
|---|
| 684 |
|
|---|
| 685 |
|
|---|
| 686 | \end{cfa}
|
|---|
| 687 | &
|
|---|
| 688 | \begin{cfa}
|
|---|
| 689 | try {
|
|---|
| 690 | do_work_may_throw();
|
|---|
| 691 | } catch(exception_t * exc) {
|
|---|
| 692 | if (can_handle(exc)) {
|
|---|
| 693 | handle(exc);
|
|---|
| 694 | } else {
|
|---|
| 695 | throw;
|
|---|
| 696 | }
|
|---|
| 697 | }
|
|---|
| 698 | \end{cfa}
|
|---|
| 699 | \end{tabular}
|
|---|
| 700 | \end{center}
|
|---|
| 701 | At first glance, catch-and-reraise may appear to just be a quality-of-life
|
|---|
| 702 | feature, but there are some significant differences between the two
|
|---|
| 703 | strategies.
|
|---|
| 704 |
|
|---|
| 705 | A simple difference that is more important for \CFA than many other languages
|
|---|
| 706 | is that the raise site changes with a re-raise, but does not with a
|
|---|
| 707 | conditional catch.
|
|---|
| 708 | This is important in \CFA because control returns to the raise site to run
|
|---|
| 709 | the per-site default handler. Because of this, only a conditional catch can
|
|---|
| 710 | allow the original raise to continue.
|
|---|
| 711 |
|
|---|
| 712 | The more complex issue comes from the difference in how conditional
|
|---|
| 713 | catches and re-raises handle multiple handlers attached to a single try
|
|---|
| 714 | statement. A conditional catch will continue checking later handlers while
|
|---|
| 715 | a re-raise will skip them.
|
|---|
| 716 | If the different handlers could handle some of the same exceptions,
|
|---|
| 717 | translating a try statement that uses one to use the other can quickly
|
|---|
| 718 | become non-trivial:
|
|---|
| 719 |
|
|---|
| 720 | \noindent
|
|---|
| 721 | Original, with conditional catch:
|
|---|
| 722 | \begin{cfa}
|
|---|
| 723 | ...
|
|---|
| 724 | } catch (an_exception * e ; check_a(e)) {
|
|---|
| 725 | handle_a(e);
|
|---|
| 726 | } catch (exception_t * e ; check_b(e)) {
|
|---|
| 727 | handle_b(e);
|
|---|
| 728 | }
|
|---|
| 729 | \end{cfa}
|
|---|
| 730 | Translated, with re-raise:
|
|---|
| 731 | \begin{cfa}
|
|---|
| 732 | ...
|
|---|
| 733 | } catch (exception_t * e) {
|
|---|
| 734 | an_exception * an_e = (virtual an_exception *)e;
|
|---|
| 735 | if (an_e && check_a(an_e)) {
|
|---|
| 736 | handle_a(an_e);
|
|---|
| 737 | } else if (check_b(e)) {
|
|---|
| 738 | handle_b(e);
|
|---|
| 739 | } else {
|
|---|
| 740 | throw;
|
|---|
| 741 | }
|
|---|
| 742 | }
|
|---|
| 743 | \end{cfa}
|
|---|
| 744 | (There is a simpler solution if @handle_a@ never raises exceptions,
|
|---|
| 745 | using nested try statements.)
|
|---|
| 746 |
|
|---|
| 747 | % } catch (an_exception * e ; check_a(e)) {
|
|---|
| 748 | % handle_a(e);
|
|---|
| 749 | % } catch (exception_t * e ; !(virtual an_exception *)e && check_b(e)) {
|
|---|
| 750 | % handle_b(e);
|
|---|
| 751 | % }
|
|---|
| 752 | %
|
|---|
| 753 | % } catch (an_exception * e)
|
|---|
| 754 | % if (check_a(e)) {
|
|---|
| 755 | % handle_a(e);
|
|---|
| 756 | % } else throw;
|
|---|
| 757 | % } catch (exception_t * e)
|
|---|
| 758 | % if (check_b(e)) {
|
|---|
| 759 | % handle_b(e);
|
|---|
| 760 | % } else throw;
|
|---|
| 761 | % }
|
|---|
| 762 | In similar simple examples, translating from re-raise to conditional catch
|
|---|
| 763 | takes less code but it does not have a general, trivial solution either.
|
|---|
| 764 |
|
|---|
| 765 | So, given that the two patterns do not trivially translate into each other,
|
|---|
| 766 | it becomes a matter of which on should be encouraged and made the default.
|
|---|
| 767 | From the premise that if a handler could handle an exception then it
|
|---|
| 768 | should, it follows that checking as many handlers as possible is preferred.
|
|---|
| 769 | So, conditional catch and checking later handlers is a good default.
|
|---|
| 770 |
|
|---|
| 771 | \section{Finally Clauses}
|
|---|
| 772 | \label{s:FinallyClauses}
|
|---|
| 773 | Finally clauses are used to perform unconditional cleanup when leaving a
|
|---|
| 774 | scope and are placed at the end of a try statement after any handler clauses:
|
|---|
| 775 | \begin{cfa}
|
|---|
| 776 | try {
|
|---|
| 777 | GUARDED_BLOCK
|
|---|
| 778 | } ... // any number or kind of handler clauses
|
|---|
| 779 | ... finally {
|
|---|
| 780 | FINALLY_BLOCK
|
|---|
| 781 | }
|
|---|
| 782 | \end{cfa}
|
|---|
| 783 | The @FINALLY_BLOCK@ is executed when the try statement is removed from the
|
|---|
| 784 | stack, including when the @GUARDED_BLOCK@ finishes, any termination handler
|
|---|
| 785 | finishes or during an unwind.
|
|---|
| 786 | The only time the block is not executed is if the program is exited before
|
|---|
| 787 | the stack is unwound.
|
|---|
| 788 |
|
|---|
| 789 | Execution of the finally block should always finish, meaning control runs off
|
|---|
| 790 | the end of the block. This requirement ensures control always continues as if
|
|---|
| 791 | the finally clause is not present, \ie finally is for cleanup, not changing
|
|---|
| 792 | control flow.
|
|---|
| 793 | Because of this requirement, local control flow out of the finally block
|
|---|
| 794 | is forbidden. The compiler precludes any @break@, @continue@, @fallthru@ or
|
|---|
| 795 | @return@ that causes control to leave the finally block. Other ways to leave
|
|---|
| 796 | the finally block, such as a @longjmp@ or termination are much harder to check,
|
|---|
| 797 | and at best require additional run-time overhead, and so are only
|
|---|
| 798 | discouraged.
|
|---|
| 799 |
|
|---|
| 800 | Not all languages with unwinding have finally clauses. Notably, \Cpp does
|
|---|
| 801 | without it as destructors, and the RAII design pattern, serve a similar role.
|
|---|
| 802 | Although destructors and finally clauses can be used for the same cases,
|
|---|
| 803 | they have their own strengths, similar to top-level function and lambda
|
|---|
| 804 | functions with closures.
|
|---|
| 805 | Destructors take more work to create, but if there is clean-up code
|
|---|
| 806 | that needs to be run every time a type is used, they are much easier
|
|---|
| 807 | to set up for each use. % It's automatic.
|
|---|
| 808 | On the other hand, finally clauses capture the local context, so are easy to
|
|---|
| 809 | use when the cleanup is not dependent on the type of a variable or requires
|
|---|
| 810 | information from multiple variables.
|
|---|
| 811 |
|
|---|
| 812 | \section{Cancellation}
|
|---|
| 813 | \label{s:Cancellation}
|
|---|
| 814 | Cancellation is a stack-level abort, which can be thought of as as an
|
|---|
| 815 | uncatchable termination. It unwinds the entire current stack, and if
|
|---|
| 816 | possible, forwards the cancellation exception to a different stack.
|
|---|
| 817 |
|
|---|
| 818 | Cancellation is not an exception operation like termination or resumption.
|
|---|
| 819 | There is no special statement for starting a cancellation; instead the standard
|
|---|
| 820 | library function @cancel_stack@ is called, passing an exception. Unlike a
|
|---|
| 821 | raise, this exception is not used in matching, only to pass information about
|
|---|
| 822 | the cause of the cancellation.
|
|---|
| 823 | Finally, as no handler is provided, there is no default handler.
|
|---|
| 824 |
|
|---|
| 825 | After @cancel_stack@ is called, the exception is copied into the EHM's memory
|
|---|
| 826 | and the current stack is unwound.
|
|---|
| 827 | The behaviour after that depends on the kind of stack being cancelled.
|
|---|
| 828 |
|
|---|
| 829 | \paragraph{Main Stack}
|
|---|
| 830 | The main stack is the one used by
|
|---|
| 831 | the program's main function at the start of execution,
|
|---|
| 832 | and is the only stack in a sequential program.
|
|---|
| 833 | After the main stack is unwound, there is a program-level abort.
|
|---|
| 834 |
|
|---|
| 835 | The first reason for this behaviour is for sequential programs where there
|
|---|
| 836 | is only one stack, and hence no stack to pass information to.
|
|---|
| 837 | Second, even in concurrent programs, the main stack has no dependency
|
|---|
| 838 | on another stack and no reliable way to find another living stack.
|
|---|
| 839 | Finally, keeping the same behaviour in both sequential and concurrent
|
|---|
| 840 | programs is simple and easy to understand.
|
|---|
| 841 |
|
|---|
| 842 | \paragraph{Thread Stack}
|
|---|
| 843 | A thread stack is created for a \CFA @thread@ object or object that satisfies
|
|---|
| 844 | the @is_thread@ trait.
|
|---|
| 845 | After a thread stack is unwound, the exception is stored until another
|
|---|
| 846 | thread attempts to join with it. Then the exception @ThreadCancelled@,
|
|---|
| 847 | which stores a reference to the thread and to the exception passed to the
|
|---|
| 848 | cancellation, is reported from the join to the joining thread.
|
|---|
| 849 | There is one difference between an explicit join (with the @join@ function)
|
|---|
| 850 | and an implicit join (from a destructor call). The explicit join takes the
|
|---|
| 851 | default handler (@defaultResumptionHandler@) from its calling context while
|
|---|
| 852 | the implicit join provides its own, which does a program abort if the
|
|---|
| 853 | @ThreadCancelled@ exception cannot be handled.
|
|---|
| 854 |
|
|---|
| 855 | The communication and synchronization are done here because threads only have
|
|---|
| 856 | two structural points (not dependent on user-code) where
|
|---|
| 857 | communication/synchronization happens: start and join.
|
|---|
| 858 | Since a thread must be running to perform a cancellation (and cannot be
|
|---|
| 859 | cancelled from another stack), the cancellation must be after start and
|
|---|
| 860 | before the join, so join is used.
|
|---|
| 861 |
|
|---|
| 862 | % TODO: Find somewhere to discuss unwind collisions.
|
|---|
| 863 | The difference between the explicit and implicit join is for safety and
|
|---|
| 864 | debugging. It helps prevent unwinding collisions by avoiding throwing from
|
|---|
| 865 | a destructor and prevents cascading the error across multiple threads if
|
|---|
| 866 | the user is not equipped to deal with it.
|
|---|
| 867 | It is always possible to add an explicit join if that is the desired behaviour.
|
|---|
| 868 |
|
|---|
| 869 | With explicit join and a default handler that triggers a cancellation, it is
|
|---|
| 870 | possible to cascade an error across any number of threads,
|
|---|
| 871 | alternating between the resumption (possibly termination) and cancellation,
|
|---|
| 872 | cleaning up each
|
|---|
| 873 | in turn, until the error is handled or the main thread is reached.
|
|---|
| 874 |
|
|---|
| 875 | \paragraph{Coroutine Stack}
|
|---|
| 876 | A coroutine stack is created for a @coroutine@ object or object that
|
|---|
| 877 | satisfies the @is_coroutine@ trait.
|
|---|
| 878 | After a coroutine stack is unwound, control returns to the @resume@ function
|
|---|
| 879 | that most recently resumed it. @resume@ reports a
|
|---|
| 880 | @CoroutineCancelled@ exception, which contains a references to the cancelled
|
|---|
| 881 | coroutine and the exception used to cancel it.
|
|---|
| 882 | The @resume@ function also takes the \defaultResumptionHandler{} from the
|
|---|
| 883 | caller's context and passes it to the internal report.
|
|---|
| 884 |
|
|---|
| 885 | A coroutine only knows of two other coroutines,
|
|---|
| 886 | its starter and its last resumer.
|
|---|
| 887 | The starter has a much more distant connection, while the last resumer just
|
|---|
| 888 | (in terms of coroutine state) called resume on this coroutine, so the message
|
|---|
| 889 | is passed to the latter.
|
|---|
| 890 |
|
|---|
| 891 | With a default handler that triggers a cancellation, it is possible to
|
|---|
| 892 | cascade an error across any number of coroutines,
|
|---|
| 893 | alternating between the resumption (possibly termination) and cancellation,
|
|---|
| 894 | cleaning up each in turn,
|
|---|
| 895 | until the error is handled or a thread stack is reached.
|
|---|