2016-10-07 Steven G. Kargl <kargl@gcc.gnu.org>
[official-gcc.git] / libgo / runtime / go-defer.c
blobf3e14bd0b966e4695ee77b4aa9fab2e85dc615ef
1 /* go-defer.c -- manage the defer stack.
3 Copyright 2009 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. */
7 #include <stddef.h>
9 #include "runtime.h"
10 #include "go-alloc.h"
11 #include "go-panic.h"
13 /* This function is called each time we need to defer a call. */
15 void
16 __go_defer (_Bool *frame, void (*pfn) (void *), void *arg)
18 G *g;
19 Defer *n;
21 g = runtime_g ();
22 n = runtime_newdefer ();
23 n->next = g->_defer;
24 n->frame = frame;
25 n->_panic = g->_panic;
26 n->pfn = (uintptr) pfn;
27 n->arg = arg;
28 n->retaddr = 0;
29 n->makefunccanrecover = 0;
30 n->special = 0;
31 g->_defer = n;
34 /* This function is called when we want to undefer the stack. */
36 void
37 __go_undefer (_Bool *frame)
39 G *g;
41 g = runtime_g ();
42 while (g->_defer != NULL && g->_defer->frame == frame)
44 Defer *d;
45 void (*pfn) (void *);
47 d = g->_defer;
48 pfn = (void (*) (void *)) d->pfn;
49 d->pfn = 0;
51 if (pfn != NULL)
52 (*pfn) (d->arg);
54 g->_defer = d->next;
56 /* This may be called by a cgo callback routine to defer the
57 call to syscall.CgocallBackDone, in which case we will not
58 have a memory context. Don't try to free anything in that
59 case--the GC will release it later. */
60 if (runtime_m () != NULL)
61 runtime_freedefer (d);
63 /* Since we are executing a defer function here, we know we are
64 returning from the calling function. If the calling
65 function, or one of its callees, paniced, then the defer
66 functions would be executed by __go_panic. */
67 *frame = 1;
71 /* This function is called to record the address to which the deferred
72 function returns. This may in turn be checked by __go_can_recover.
73 The frontend relies on this function returning false. */
75 _Bool
76 __go_set_defer_retaddr (void *retaddr)
78 G *g;
80 g = runtime_g ();
81 if (g->_defer != NULL)
82 g->_defer->retaddr = (uintptr) __builtin_extract_return_addr (retaddr);
83 return 0;