Changeset 4aba055 for doc/theses
- Timestamp:
- Jun 15, 2021, 12:27:20 PM (4 years ago)
- Branches:
- ADT, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- 2f19e03
- Parents:
- 471ff17
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified doc/theses/andrew_beach_MMath/features.tex ¶
r471ff17 r4aba055 2 2 \label{c:features} 3 3 4 This chapter covers the design and user interface of the \CFA 5 exception-handling mechanism (EHM). % or exception system. 6 7 We will begin with an overview of EHMs in general. It is not a strict 8 definition of all EHMs nor an exaustive list of all possible features. 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. 9 7 However it does cover the most common structure and features found in them. 10 8 9 \section{Overview of EHMs} 11 10 % We should cover what is an exception handling mechanism and what is an 12 11 % exception before this. Probably in the introduction. Some of this could 13 12 % move there. 14 \ paragraph{Raise / Handle}13 \subsection{Raise / Handle} 15 14 An exception operation has two main parts: raise and handle. 16 15 These terms are sometimes also known as throw and catch but this work uses 17 16 throw/catch as a particular kind of raise/handle. 18 These are the two parts that the user w ill write themselves and may17 These are the two parts that the user writes and may 19 18 be the only two pieces of the EHM that have any syntax in the language. 20 19 21 \ subparagraph{Raise}20 \paragraph{Raise} 22 21 The raise is the starting point for exception handling. It marks the beginning 23 of exception handling by raising an excep ion, which passes it to22 of exception handling by raising an exception, which passes it to 24 23 the EHM. 25 24 … … 29 28 purposes of this overview that can be ignored. 30 29 31 \ subparagraph{Handle}30 \paragraph{Handle} 32 31 The purpose of most exception operations is to run some user code to handle 33 32 that exception. This code is given, with some other information, in a handler. 34 33 35 34 A handler has three common features: the previously mentioned user code, a 36 region of code they coverand an exception label/condition that matches35 region of code they guard and an exception label/condition that matches 37 36 certain exceptions. 38 Only raises inside the covered region and raising exceptions that match the37 Only raises inside the guarded region and raising exceptions that match the 39 38 label can be handled by a given handler. 40 Different EHMs will have different rules to pick a handler41 if multip e handlers could be used such as ``best match" or ``first found".39 Different EHMs use different rules to pick a handler, 40 if multiple handlers could be used such as ``best match" or ``first found". 42 41 43 42 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 covered43 also show another common feature of handlers, they are grouped by the guarded 45 44 region. 46 45 47 \ paragraph{Propagation}46 \subsection{Propagation} 48 47 After an exception is raised comes what is usually the biggest step for the 49 EHM: finding and setting up the handler. The prop ogation from raise to48 EHM: finding and setting up the handler. The propagation from raise to 50 49 handler can be broken up into three different tasks: searching for a handler, 51 50 matching against the handler and installing the handler. 52 51 53 \ subparagraph{Searching}52 \paragraph{Searching} 54 53 The EHM begins by searching for handlers that might be used to handle 55 54 the exception. Searching is usually independent of the exception that was 56 thrown as it looks for handlers that have the raise site in their covered55 thrown as it looks for handlers that have the raise site in their guarded 57 56 region. 58 Th is includes handlers in the current function, as well as any in callers59 on the stack that have the function call in their covered region.60 61 \ subparagraph{Matching}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. 59 60 \paragraph{Matching} 62 61 Each handler found has to be matched with the raised exception. The exception 63 label defines a condition that be useused with exception and decides if62 label defines a condition that is used with exception and decides if 64 63 there is a match or not. 65 64 66 In languages where the first match is used this step is intertwined with67 searching ,a match check is preformed immediately after the search finds65 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 68 67 a possible handler. 69 68 70 \ subparagraph{Installing}69 \paragraph{Installing} 71 70 After a handler is chosen it must be made ready to run. 72 71 The implementation can vary widely to fit with the rest of the … … 75 74 case when stack unwinding is involved. 76 75 77 If a matching handler is not guarantied to be found the EHM will need a 78 different course of action here in the cases where no handler matches. 79 This is only required with unchecked exceptions as checked exceptions 80 (such as in Java) can make than guaranty. 81 This different action can also be installing a handler but it is usually an 82 implicat and much more general one. 83 84 \subparagraph{Hierarchy} 76 If a matching handler is not guarantied to be found, the EHM needs a 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. 81 82 \paragraph{Hierarchy} 85 83 A common way to organize exceptions is in a hierarchical structure. 86 This is especially true inobject-orientated languages where the84 This pattern comes from object-orientated languages where the 87 85 exception hierarchy is a natural extension of the object hierarchy. 88 86 … … 92 90 \end{center} 93 91 94 A handler label led with any given exception can handle exceptions of that92 A handler labeled with any given exception can handle exceptions of that 95 93 type or any child type of that exception. The root of the exception hierarchy 96 94 (here \code{C}{exception}) acts as a catch-all, leaf types catch single types … … 106 104 % Could I cite the rational for the Python IO exception rework? 107 105 108 \ paragraph{Completion}106 \subsection{Completion} 109 107 After the handler has finished the entire exception operation has to complete 110 108 and continue executing somewhere else. This step is usually simple, … … 113 111 114 112 The EHM can return control to many different places, 115 the most common are after the handler definition and after the raise. 116 117 \paragraph{Communication} 113 the most common are after the handler definition (termination) 114 and after the raise (resumption). 115 116 \subsection{Communication} 118 117 For effective exception handling, additional information is often passed 119 from the raise to the handler .118 from the raise to the handler and back again. 120 119 So far only communication of the exceptions' identity has been covered. 121 A common method is putting fields into the exception instance and giving the 122 handler access to them. 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. 123 124 124 125 \section{Virtuals} 125 126 Virtual types and casts are not part of \CFA's EHM nor are they required for 126 127 any EHM. 127 However the \CFA uses a hierarchy built with the virtual system as the basis128 for exceptions and exception matching.129 130 The virtual system would have ideallybeen part of \CFA before the work128 However, it is one of the best ways to support an exception hierachy 129 is via a virtual hierarchy and dispatch system. 130 131 Ideally, the virtual system would have been part of \CFA before the work 131 132 on exception handling began, but unfortunately it was not. 132 Because of thisonly the features and framework needed for the EHM were133 Hence, only the features and framework needed for the EHM were 133 134 designed and implemented. Other features were considered to ensure that 134 the structure could accom idate other desirable features but they were not135 implemented.136 The rest of this section will only discuss the finalized portionof the137 virtual system .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. 138 139 139 140 The virtual system supports multiple ``trees" of types. Each tree is … … 145 146 % A type's ancestors are its parent and its parent's ancestors. 146 147 % The root type has no ancestors. 147 % A type's de cendents are its children and its children's decendents.148 % A type's descendants are its children and its children's descendants. 148 149 149 150 Every virtual type also has a list of virtual members. Children inherit … … 151 152 It is important to note that these are virtual members, not virtual methods 152 153 of object-orientated programming, and can be of any type. 154 153 155 \CFA still supports virtual methods as a special case of virtual members. 154 Function pointers that take a pointer to the virtual type will be modified156 Function pointers that take a pointer to the virtual type are modified 155 157 with each level of inheritance so that refers to the new type. 156 158 This means an object can always be passed to a function in its virtual table 157 159 as if it were a method. 160 \todo{Clarify (with an example) virtual methods.} 158 161 159 162 Each virtual type has a unique id. 160 This uniqueid and all the virtual members are combined163 This id and all the virtual members are combined 161 164 into a virtual table type. Each virtual type has a pointer to a virtual table 162 165 as a hidden field. 166 \todo{Might need a diagram for virtual structure.} 163 167 164 168 Up until this point the virtual system is similar to ones found in 165 169 object-orientated languages but this where \CFA diverges. Objects encapsulate a 166 170 single set of behaviours in each type, universally across the entire program, 167 and indeed all programs that use that type definition. In this sense the171 and indeed all programs that use that type definition. In this sense, the 168 172 types are ``closed" and cannot be altered. 169 173 170 In \CFA types do not encapsulate any behaviour. Traits are local and171 types can begin to s tatify a trait, stop satifying a trait or satify the same174 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 172 176 trait in a different way at any lexical location in the program. 173 In this sense they are ``open" as they can change at any time. This means it174 is implossible to pick a single set of functions that repersent the type's175 implementation across the program.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. 176 180 177 181 \CFA side-steps this issue by not having a single virtual table for each 178 type. A user can define virtual tables whichare filled in at their179 declaration and given a name. Anywhere that name is visible, even if it was180 defined locally inside a function (although that means it willnot have a182 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 181 185 static lifetime), it can be used. 182 Specifically, a virtual type is ``bound" to a virtual table which186 Specifically, a virtual type is ``bound" to a virtual table that 183 187 sets the virtual members for that object. The virtual members can be accessed 184 188 through the object. … … 212 216 \end{cfa} 213 217 The trait is defined over two types, the exception type and the virtual table 214 type. Each exception type should have buta single virtual table type.215 Now there are no actual assertions in this trait because the trait system216 actually can't express them(adding such assertions would be part of218 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 217 221 completing the virtual system). The imaginary assertions would probably come 218 222 from a trait defined by the virtual system, and state that the exception type 219 is a virtual type, is a de cendent of @exception_t@ (the base exception type)223 is a virtual type, is a descendant of @exception_t@ (the base exception type) 220 224 and note its virtual table type. 221 225 … … 236 240 }; 237 241 \end{cfa} 238 Both traits ensure a pair of types are an exception type and its virtual table 242 Both traits ensure a pair of types are an exception type, its virtual table 243 type 239 244 and defines one of the two default handlers. The default handlers are used 240 245 as fallbacks and are discussed in detail in \vref{s:ExceptionHandling}. … … 264 269 \section{Exception Handling} 265 270 \label{s:ExceptionHandling} 271 As stated, 266 272 \CFA provides two kinds of exception handling: termination and resumption. 267 273 These twin operations are the core of \CFA's exception handling mechanism. … … 271 277 Both operations follow the same set of steps. 272 278 Both start with the user preforming a raise on an exception. 273 Then the exception prop ogates up the stack.279 Then the exception propagates up the stack. 274 280 If a handler is found the exception is caught and the handler is run. 275 After that control returns to normal execution.276 If the search fails a default handler is run and then control277 returns to normal executionafter the raise.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. 278 284 279 285 This general description covers what the two kinds have in common. 280 Differences include how prop ogation is preformed, where exception continues286 Differences include how propagation is preformed, where exception continues 281 287 after an exception is caught and handled and which default handler is run. 282 288 … … 285 291 Termination handling is the familiar kind and used in most programming 286 292 languages with exception handling. 287 It is dynamic, non-local goto. If the raised exception is matched and288 handled the stack is unwound and control will (usually) continuethe function293 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 289 295 on the call stack that defined the handler. 290 296 Termination is commonly used when an error has occurred and recovery is … … 301 307 termination exception is any type that satisfies the trait 302 308 @is_termination_exception@ at the call site. 303 Through \CFA's trait system the trait functions are implicity passed into the309 Through \CFA's trait system, the trait functions are implicitly passed into the 304 310 throw code and the EHM. 305 311 A new @defaultTerminationHandler@ can be defined in any scope to 306 change the throw's behavio r (see below).307 308 The throw will copythe provided exception into managed memory to ensure312 change the throw's behaviour (see below). 313 314 The throw copies the provided exception into managed memory to ensure 309 315 the exception is not destroyed if the stack is unwound. 310 316 It is the user's responsibility to ensure the original exception is cleaned 311 up whe ither the stack is unwound or not. Allocating it on the stack is317 up whether the stack is unwound or not. Allocating it on the stack is 312 318 usually sufficient. 313 319 314 Then propogation starts with the search. \CFA uses a ``first match" rule so 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 315 322 matching is preformed with the copied exception as the search continues. 316 It starts from the throwing function and proceeds to thebase of the stack,323 It starts from the throwing function and proceeds towards base of the stack, 317 324 from callee to caller. 318 325 At each stack frame, a check is made for resumption handlers defined by the … … 327 334 } 328 335 \end{cfa} 329 When viewed on its own, a try statement will simply execute the statements 330 in \snake{GUARDED_BLOCK} and when those are finished the try statement finishes. 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. 331 339 332 340 However, while the guarded statements are being executed, including any 333 invoked functions, all the handlers in the statement are now on the search 334 path. If a termination exception is thrown and not handled further up the 335 stack they will be matched against the exception. 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. 336 345 337 346 Exception matching checks the handler in each catch clause in the order 338 they appear, top to bottom. If the representation of the thrownexception type347 they appear, top to bottom. If the representation of the raised exception type 339 348 is the same or a descendant of @EXCEPTION_TYPE@$_i$ then @NAME@$_i$ 340 349 (if provided) is … … 344 353 345 354 If no termination handler is found during the search then the default handler 346 (\defaultTerminationHandler) is run.347 Through \CFA's trait system the best match at the throw sight will be used.348 This function is run and is passed the copied exception. After the default349 handler is run control continues after the throwstatement.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. 350 359 351 360 There is a global @defaultTerminationHandler@ that is polymorphic over all 352 exception types. Since it is so general a more specific handler can be 353 defined and will be used for those types, effectively overriding the handler 354 for particular exception type. 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. 355 365 The global default termination handler performs a cancellation 356 366 (see \vref{s:Cancellation}) on the current stack with the copied exception. … … 362 372 just as old~\cite{Goodenough75} and is simpler in many ways. 363 373 It is a dynamic, non-local function call. If the raised exception is 364 matched a closure will be taken from up the stack and executed, 365 after which the raising function will continue executing. 366 These are most often used when an error occurred and if the error is repaired 367 then the function can continue. 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. 368 381 369 382 A resumption raise is started with the @throwResume@ statement: … … 371 384 throwResume EXPRESSION; 372 385 \end{cfa} 386 \todo{Decide on a final set of keywords and use them everywhere.} 373 387 It works much the same way as the termination throw. 374 388 The expression must return a reference to a resumption exception, … … 379 393 380 394 At run-time, no exception copy is made. 381 As the stack is not unwound the exception and 382 any values on the stack will remain in scope while the resumption is handled. 383 384 The EHM then begins propogation. The search starts from the raise in the 385 resuming function and proceeds to the base of the stack, from callee to caller. 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. 397 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. 386 401 At each stack frame, a check is made for resumption handlers defined by the 387 402 @catchResume@ clauses of a @try@ statement. … … 398 413 Note that termination handlers and resumption handlers may be used together 399 414 in a single try statement, intermixing @catch@ and @catchResume@ freely. 400 Each type of handler will only interactwith exceptions from the matching401 typeof raise.402 When a try statement is executed it simply executes the statements in the415 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 403 418 @GUARDED_BLOCK@ and then finishes. 404 419 405 420 However, while the guarded statements are being executed, including any 406 invoked functions, all the handlers in the statement are now on the search 407 path. If a resumption exception is reported and not handled further up the 408 stack they will be matched against the exception. 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. 409 425 410 426 Exception matching checks the handler in each catch clause in the order 411 they appear, top to bottom. If the representation of the thrownexception type427 they appear, top to bottom. If the representation of the raised exception type 412 428 is the same or a descendant of @EXCEPTION_TYPE@$_i$ then @NAME@$_i$ 413 429 (if provided) is bound to a pointer to the exception and the statements in … … 416 432 the raise statement that raised the handled exception. 417 433 418 Like termination, if no resumption handler is found, the default handler 419 visible at the throw statement is called. It will use the best match at the 420 call sight according to \CFA's overloading rules. The default handler is 421 passed the exception given to the throw. When the default handler finishes 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 422 439 execution continues after the raise statement. 423 440 424 441 There is a global \defaultResumptionHandler{} is polymorphic over all 425 termination exceptions and preforms a termination throw on the exception. 426 The \defaultTerminationHandler{} for that raise is matched at the 427 original raise statement (the resumption @throw@\-@Resume@) and it can be 428 customized by introducing a new or better match as well. 442 resumption exceptions and preforms a termination throw on the exception. 443 The \defaultTerminationHandler{} can be overriden by providing a new 444 function that is a better match. 429 445 430 446 \subsubsection{Resumption Marking} … … 433 449 not unwind the stack. A side effect that is that when a handler is matched 434 450 and run it's try block (the guarded statements) and every try statement 435 searched before it are still on the stack. Th is can lead to the recursive436 resumption problem.451 searched before it are still on the stack. There presence can lead to 452 the recursive resumption problem. 437 453 438 454 The recursive resumption problem is any situation where a resumption handler … … 446 462 } 447 463 \end{cfa} 448 When this code is executed the guarded @throwResume@ will throw, starta449 search and match the handler in the @catchResume@ clause. This will be450 call and placed on the stack on top of the try-block. The second throwthen451 throws and will search the same try block and put callanother instance of the452 same handler leading to an infinite loop.453 454 This situation is trivial and easy to avoid, butmuch more complex cycles464 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. 469 470 While this situation is trivial and easy to avoid, much more complex cycles 455 471 can form with multiple handlers and different exception types. 456 472 457 To prevent all of these cases we mark try statements on the stack.458 A try statement is marked when a match check is preformed with it and an 459 exception. The statement will be unmarked when the handling of that exception 460 is completed or the search completes without finding a handler.461 While a try statement is marked its handlers are never matched, effectify473 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 462 478 skipping over it to the next try statement. 463 479 … … 466 482 \end{center} 467 483 468 These rules mirror what happens with termination. 469 When a termination throw happens in a handler the search will not look at 470 any handlers from the original throw to the original catch because that 471 part of the stack has been unwound. 472 A resumption raise in the same situation wants to search the entire stack, 473 but it will not try to match the exception with try statements in the section 474 that would have been unwound as they are marked. 475 476 The symmetry between resumption termination is why this pattern was picked. 477 Other patterns, such as marking just the handlers that caught, also work but 478 lack the symmetry means there are more rules to remember. 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. 488 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. 479 497 480 498 \section{Conditional Catch} … … 491 509 did not match. 492 510 493 The condition matching allows finer matching by allowing the match to check511 The condition matching allows finer matching by checking 494 512 more kinds of information than just the exception type. 495 513 \begin{cfa} … … 506 524 // Can't handle a failure relating to f2 here. 507 525 \end{cfa} 508 In this example the file that experi anced the IO error is used to decide526 In this example the file that experienced the IO error is used to decide 509 527 which handler should be run, if any at all. 510 528 … … 536 554 \subsection{Comparison with Reraising} 537 555 A more popular way to allow handlers to match in more detail is to reraise 538 the exception after it has been caught if it could not be handled here. 539 On the surface these two features seem interchangable. 540 541 If we used @throw;@ to start a termination reraise then these two statements 542 would have the same behaviour: 556 the exception after it has been caught, if it could not be handled here. 557 On the surface these two features seem interchangeable. 558 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: 543 562 \begin{cfa} 544 563 try { … … 560 579 } 561 580 \end{cfa} 562 If there are further handlers after this handler only the first version will 563 check them. If multiple handlers on a single try block that could handle the 564 same exception the translations get more complex but they are equivilantly 565 powerful. 566 567 Until stack unwinding comes into the picture. In termination handling, a 568 conditional catch happens before the stack is unwound, but a reraise happens 569 afterwards. Normally this might only cause you to loose some debug 570 information you could get from a stack trace (and that can be side stepped 571 entirely by collecting information during the unwind). But for \CFA there is 572 another issue, if the exception isn't handled the default handler should be 573 run at the site of the original raise. 574 575 There are two problems with this: the site of the original raise doesn't 576 exist anymore and the default handler might not exist anymore. The site will 577 always be removed as part of the unwinding, often with the entirety of the 578 function it was in. The default handler could be a stack allocated nested 579 function removed during the unwind. 580 581 This means actually trying to pretend the catch didn't happening, continuing 582 the original raise instead of starting a new one, is infeasible. 583 That is the expected behaviour for most languages and we can't replicate 584 that behaviour. 581 That is, they will have the same behaviour in isolation. 582 Two things can expose differences between these cases. 583 584 One is the existance of multiple handlers on a single try statement. 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, 590 both can be used two mimick the behaviour of the other, 591 as reraise can pack arbitrary code in the handler and conditional catches 592 can put arbitrary code in the predicate. 593 % I was struggling with a long explination about some simple solutions, 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/ 585 639 586 640 \section{Finally Clauses} … … 614 668 615 669 Not all languages with unwinding have finally clauses. Notably \Cpp does 616 without it as descructors serve a similar role. Although destructors and 617 finally clauses can be used in many of the same areas they have their own 618 use cases like top-level functions and lambda functions with closures. 619 Destructors take a bit more work to set up but are much easier to reuse while 620 finally clauses are good for one-off uses and 621 can easily include local information. 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. 622 681 623 682 \section{Cancellation} … … 635 694 636 695 After @cancel_stack@ is called the exception is copied into the EHM's memory 637 and the current stack is 638 unwound. After that it depends one which stack isbeing cancelled.696 and the current stack is unwound. 697 The behaviour after that depends on the kind of stack being cancelled. 639 698 640 699 \paragraph{Main Stack} … … 643 702 After the main stack is unwound there is a program-level abort. 644 703 645 There are two reasons for this. The first is that it obviously had to do this 704 There are two reasons for these semantics. 705 The first is that it had to do this abort. 646 706 in a sequential program as there is nothing else to notify and the simplicity 647 707 of keeping the same behaviour in sequential and concurrent programs is good. 648 Also, even in concurrent programs there is no stack that an innate connection649 to, so it would have be explicitly managed.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. 650 710 651 711 \paragraph{Thread Stack} 652 712 A thread stack is created for a \CFA @thread@ object or object that satisfies 653 713 the @is_thread@ trait. 654 After a thread stack is unwound there exception is stored until another714 After a thread stack is unwound, the exception is stored until another 655 715 thread attempts to join with it. Then the exception @ThreadCancelled@, 656 716 which stores a reference to the thread and to the exception passed to the 657 cancellation, is reported from the join .717 cancellation, is reported from the join to the joining thread. 658 718 There is one difference between an explicit join (with the @join@ function) 659 719 and an implicit join (from a destructor call). The explicit join takes the 660 720 default handler (@defaultResumptionHandler@) from its calling context while 661 the implicit join provides its own which does a program abort if the721 the implicit join provides its own; which does a program abort if the 662 722 @ThreadCancelled@ exception cannot be handled. 663 723 664 Communication is done at join because a thread only has to have to points of 665 communication with other threads: start and join. 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. 666 727 Since a thread must be running to perform a cancellation (and cannot be 667 728 cancelled from another stack), the cancellation must be after start and 668 before the join . So join is the one that we will use.729 before the join, so join is used. 669 730 670 731 % TODO: Find somewhere to discuss unwind collisions. … … 678 739 A coroutine stack is created for a @coroutine@ object or object that 679 740 satisfies the @is_coroutine@ trait. 680 After a coroutine stack is unwound control returns to the resumefunction681 that most recently resumed it. The resume statementreports a741 After a coroutine stack is unwound, control returns to the @resume@ function 742 that most recently resumed it. @resume@ reports a 682 743 @CoroutineCancelled@ exception, which contains a references to the cancelled 683 744 coroutine and the exception used to cancel it. 684 The resumefunction also takes the \defaultResumptionHandler{} from the745 The @resume@ function also takes the \defaultResumptionHandler{} from the 685 746 caller's context and passes it to the internal report. 686 747 687 748 A coroutine knows of two other coroutines, its starter and its last resumer. 688 The starter has a much more distant connection while the last resumer just749 The starter has a much more distant connection, while the last resumer just 689 750 (in terms of coroutine state) called resume on this coroutine, so the message 690 751 is passed to the latter.
Note: See TracChangeset
for help on using the changeset viewer.