1 /* go-deferred-recover.c -- support for a deferred recover function.
3 Copyright 2010 The Go Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style
5 license that can be found in the LICENSE file. */
12 /* This is called when a call to recover is deferred. That is,
16 We need to handle this specially. In 6g/8g, the recover function
17 looks up the stack frame. In particular, that means that a
18 deferred recover will not recover a panic thrown in the same
19 function that defers the recover. It will only recover a panic
20 thrown in a function that defers the deferred call to recover.
25 defer recover() // does not stop panic
31 defer recover() // stops panic(0)
38 defer recover() // does not stop panic
47 defer recover() // stops panic(0)
54 The interesting case here is f3. As can be seen from f2, the
55 deferred recover could pick up panic(1). However, this does not
56 happen because it is blocked by the panic(0).
58 When a function calls recover, then when we invoke it we pass a
59 hidden parameter indicating whether it should recover something.
60 This parameter is set based on whether the function is being
61 invoked directly from defer. The parameter winds up determining
62 whether __go_recover or __go_deferred_recover is called at all.
64 In the case of a deferred recover, the hidden parameter which
65 controls the call is actually the one set up for the function which
66 runs the defer recover() statement. That is the right thing in all
67 the cases above except for f3. In f3 the function is permitted to
68 call recover, but the deferred recover call is not. We address
69 that here by checking for that specific case before calling
70 recover. If this function was deferred when there is already a
71 panic on the panic stack, then we can only recover that panic, not
74 Note that we can get away with using a special function here
75 because you are not permitted to take the address of a predeclared
76 function like recover. */
78 struct __go_empty_interface
79 __go_deferred_recover ()
81 if (__go_panic_defer
== NULL
82 || __go_panic_defer
->__defer
== NULL
83 || __go_panic_defer
->__defer
->__panic
!= __go_panic_defer
->__panic
)
85 struct __go_empty_interface ret
;
87 ret
.__type_descriptor
= NULL
;
91 return __go_recover();