diff options
author | Janne Blomqvist <jb@gcc.gnu.org> | 2018-11-30 18:44:27 +0200 |
---|---|---|
committer | Janne Blomqvist <jb@gcc.gnu.org> | 2018-11-30 18:44:27 +0200 |
commit | 854cedfd40f14e974060dd3ce4463712ed18274b (patch) | |
tree | 40de8bda791fc96b25048a081dc59b2d314e2f8a /libgfortran/runtime/backtrace.c | |
parent | 5bd624fbde5f6307fb71041a38aa0914eab9f6f9 (diff) | |
download | gcc-854cedfd40f14e974060dd3ce4463712ed18274b.zip gcc-854cedfd40f14e974060dd3ce4463712ed18274b.tar.gz gcc-854cedfd40f14e974060dd3ce4463712ed18274b.tar.bz2 |
Initialize backtrace state once
From backtrace.h for backtrace_create_state:
Calling this function allocates resources that can not be freed.
There is no backtrace_free_state function. The state is used to
cache information that is expensive to recompute. Programs are
expected to call this function at most once and to save the return
value for all later calls to backtrace functions.
So instead of calling backtrace_create_state every time we wish to
show a backtrace, do it once and store the result in a static
variable. libbacktrace allows multiple threads to access the state,
so no need to use TLS.
Regtested on x86_64-pc-linux-gnu.
libgfortran/ChangeLog:
2018-11-30 Janne Blomqvist <jb@gcc.gnu.org>
PR libfortran/88137
* runtime/backtrace.c (show_backtrace): Make lbstate a static
variable, initialize once.
From-SVN: r266677
Diffstat (limited to 'libgfortran/runtime/backtrace.c')
-rw-r--r-- | libgfortran/runtime/backtrace.c | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/libgfortran/runtime/backtrace.c b/libgfortran/runtime/backtrace.c index e0c2770..3fc973a 100644 --- a/libgfortran/runtime/backtrace.c +++ b/libgfortran/runtime/backtrace.c @@ -146,11 +146,15 @@ full_callback (void *data, uintptr_t pc, const char *filename, void show_backtrace (bool in_signal_handler) { - struct backtrace_state *lbstate; + /* Note that libbacktrace allows the state to be accessed from + multiple threads, so we don't need to use a TLS variable for the + state here. */ + static struct backtrace_state *lbstate; struct mystate state = { 0, false, in_signal_handler }; - - lbstate = backtrace_create_state (NULL, __gthread_active_p (), - error_callback, NULL); + + if (!lbstate) + lbstate = backtrace_create_state (NULL, __gthread_active_p (), + error_callback, NULL); if (lbstate == NULL) return; |