* const-uniq-1.c: Expand regex to match AIX XCOFF labels.
[official-gcc.git] / libgo / runtime / panic.c
blob1af9639245648d18c85005c356ca20dcc1a383c6
1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 #include "runtime.h"
6 #include "go-defer.h"
7 #include "go-panic.h"
9 // Code related to defer, panic and recover.
11 uint32 runtime_panicking;
12 static Lock paniclk;
14 // Run all deferred functions for the current goroutine.
15 static void
16 rundefer(void)
18 G *g;
19 Defer *d;
21 g = runtime_g();
22 while((d = g->defer) != nil) {
23 void (*pfn)(void*);
25 g->defer = d->__next;
26 pfn = d->__pfn;
27 d->__pfn = nil;
28 if (pfn != nil)
29 (*pfn)(d->__arg);
30 runtime_free(d);
34 void
35 runtime_startpanic(void)
37 M *m;
39 m = runtime_m();
40 if(m->dying) {
41 runtime_printf("panic during panic\n");
42 runtime_exit(3);
44 m->dying = 1;
45 runtime_xadd(&runtime_panicking, 1);
46 runtime_lock(&paniclk);
49 void
50 runtime_dopanic(int32 unused __attribute__ ((unused)))
52 G *g;
53 static bool didothers;
55 g = runtime_g();
56 if(g->sig != 0)
57 runtime_printf("[signal %x code=%p addr=%p]\n",
58 g->sig, (void*)g->sigcode0, (void*)g->sigcode1);
60 if(runtime_gotraceback()){
61 if(g != runtime_m()->g0) {
62 runtime_printf("\n");
63 runtime_goroutineheader(g);
64 runtime_traceback();
65 runtime_goroutinetrailer(g);
67 if(!didothers) {
68 didothers = true;
69 runtime_tracebackothers(g);
72 runtime_unlock(&paniclk);
73 if(runtime_xadd(&runtime_panicking, -1) != 0) {
74 // Some other m is panicking too.
75 // Let it print what it needs to print.
76 // Wait forever without chewing up cpu.
77 // It will exit when it's done.
78 static Lock deadlock;
79 runtime_lock(&deadlock);
80 runtime_lock(&deadlock);
83 runtime_exit(2);
86 void
87 runtime_throw(const char *s)
89 runtime_startpanic();
90 runtime_printf("throw: %s\n", s);
91 runtime_dopanic(0);
92 *(int32*)0 = 0; // not reached
93 runtime_exit(1); // even more not reached
96 void
97 runtime_panicstring(const char *s)
99 Eface err;
101 if(runtime_m()->gcing) {
102 runtime_printf("panic: %s\n", s);
103 runtime_throw("panic during gc");
105 runtime_newErrorString(runtime_gostringnocopy((const byte*)s), &err);
106 runtime_panic(err);
109 void runtime_Goexit (void) asm ("runtime.Goexit");
111 void
112 runtime_Goexit(void)
114 rundefer();
115 runtime_goexit();