Mercurial > octave
annotate liboctave/util/quit.cc @ 31633:d9970470108a stable
Remove several race conditions with signal handler (bug #61370).
This patch removes several race conditions between the interpreter and the
Ctrl-C signal handler, as described in more detail in bug #61370.
* libinterp/corefcn/sighandlers.h: Make can_interrupt atomic.
* libinterp/corefcn/sighandlers.cc: Make can_interrupt, signals_caught atomic,
use atomic value read for signals_caught, octave_interrupt_state,
change 1 to true.
* liboctave/util/quit.h: Move octave_interrupt_state and octave_signal_caught
to C++ only code, rewrite octave_quit() function, rewrite OCTAVE_QUIT macro
to use octave_quit_c() function.
* liboctave/util/quit.cc: Make octave_interrupt_state and octave_signal_caught
atomic, add new wrapper function octave_quit_c().
* libinterp/corefcn/interpreter.h: Make octave_initialized atomic.
* libinterp/corefcn/interpreter.cc: Make octave_initialized atomic,
change 0 to false.
* liboctave/util/action-container.h: New namespace "util", make m_val atomic.
* liboctave/util/oct-atomic.c: Reorder preprocessor if-elif-else ladder.
* liboctave/util/cmd-edit.cc: Change 0 to false.
author | Reinhard Resch <r_resch@a1.net> |
---|---|
date | Sat, 03 Dec 2022 10:36:59 -0500 |
parents | aac27ad79be6 |
children | 7d3467f8d713 |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 //////////////////////////////////////////////////////////////////////// |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 // |
30564
796f54d4ddbf
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
29572
diff
changeset
|
3 // Copyright (C) 2002-2022 The Octave Project Developers |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
4 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 // See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 // distribution or <https://octave.org/copyright/>. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
7 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
8 // This file is part of Octave. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
9 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
10 // Octave is free software: you can redistribute it and/or modify it |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
11 // under the terms of the GNU General Public License as published by |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
12 // the Free Software Foundation, either version 3 of the License, or |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
13 // (at your option) any later version. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
14 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
15 // Octave is distributed in the hope that it will be useful, but |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
16 // WITHOUT ANY WARRANTY; without even the implied warranty of |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
18 // GNU General Public License for more details. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
19 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
20 // You should have received a copy of the GNU General Public License |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
21 // along with Octave; see the file COPYING. If not, see |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
22 // <https://www.gnu.org/licenses/>. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 //////////////////////////////////////////////////////////////////////// |
4153 | 25 |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
26 #if defined (HAVE_CONFIG_H) |
21301
40de9f8f23a6
Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents:
21202
diff
changeset
|
27 # include "config.h" |
4153 | 28 #endif |
29 | |
31633
d9970470108a
Remove several race conditions with signal handler (bug #61370).
Reinhard Resch <r_resch@a1.net>
parents:
31607
diff
changeset
|
30 #include <atomic> |
4154 | 31 #include <cstring> |
32 | |
27471 | 33 #include <ostream> |
34 #include <sstream> | |
4180 | 35 #include <new> |
36 | |
4153 | 37 #include "quit.h" |
38 | |
31633
d9970470108a
Remove several race conditions with signal handler (bug #61370).
Reinhard Resch <r_resch@a1.net>
parents:
31607
diff
changeset
|
39 std::atomic<sig_atomic_t> octave_interrupt_state{0}; |
27480
63b417917f5e
remove some obsolete signal handling functions
John W. Eaton <jwe@octave.org>
parents:
27471
diff
changeset
|
40 |
31633
d9970470108a
Remove several race conditions with signal handler (bug #61370).
Reinhard Resch <r_resch@a1.net>
parents:
31607
diff
changeset
|
41 volatile std::atomic<bool> octave_signal_caught{false}; |
27480
63b417917f5e
remove some obsolete signal handling functions
John W. Eaton <jwe@octave.org>
parents:
27471
diff
changeset
|
42 |
23795
980f39c3ab90
Use C++11 nullptr rather than 0 in code (bug #51565).
Rik <rik@octave.org>
parents:
23426
diff
changeset
|
43 void (*octave_signal_hook) (void) = nullptr; |
980f39c3ab90
Use C++11 nullptr rather than 0 in code (bug #51565).
Rik <rik@octave.org>
parents:
23426
diff
changeset
|
44 void (*octave_interrupt_hook) (void) = nullptr; |
4429 | 45 |
31605
e88a07dec498
maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents:
30565
diff
changeset
|
46 OCTAVE_BEGIN_NAMESPACE(octave) |
e88a07dec498
maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents:
30565
diff
changeset
|
47 |
31607
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
48 std::string execution_exception::stack_trace (void) const |
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
49 { |
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
50 std::size_t nframes = m_stack_info.size (); |
27471 | 51 |
31607
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
52 if (nframes == 0) |
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
53 return std::string (); |
27471 | 54 |
31607
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
55 std::ostringstream buf; |
27471 | 56 |
31607
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
57 buf << "error: called from\n"; |
27471 | 58 |
31607
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
59 for (const auto& frm : m_stack_info) |
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
60 { |
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
61 buf << " " << frm.fcn_name (); |
27471 | 62 |
31607
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
63 int line = frm.line (); |
27471 | 64 |
31607
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
65 if (line > 0) |
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
66 { |
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
67 buf << " at line " << line; |
27471 | 68 |
31607
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
69 int column = frm.column (); |
27471 | 70 |
31607
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
71 if (column > 0) |
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
72 buf << " column " << column; |
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
73 } |
27471 | 74 |
31607
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
75 buf << "\n"; |
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
76 } |
27471 | 77 |
31607
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
78 return buf.str (); |
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
79 } |
27471 | 80 |
31607
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
81 void execution_exception::display (std::ostream& os) const |
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
82 { |
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
83 if (! m_message.empty ()) |
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
84 { |
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
85 os << m_err_type << ": " << m_message; |
27471 | 86 |
31607
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
87 if (m_message.back () != '\n') |
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
88 { |
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
89 os << "\n"; |
27471 | 90 |
31607
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
91 std::string st = stack_trace (); |
27471 | 92 |
31607
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
93 if (! st.empty ()) |
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
94 os << st; |
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
95 } |
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
96 } |
aac27ad79be6
maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents:
31605
diff
changeset
|
97 } |
31605
e88a07dec498
maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents:
30565
diff
changeset
|
98 |
31633
d9970470108a
Remove several race conditions with signal handler (bug #61370).
Reinhard Resch <r_resch@a1.net>
parents:
31607
diff
changeset
|
99 void octave_quit_c (void) |
d9970470108a
Remove several race conditions with signal handler (bug #61370).
Reinhard Resch <r_resch@a1.net>
parents:
31607
diff
changeset
|
100 { |
d9970470108a
Remove several race conditions with signal handler (bug #61370).
Reinhard Resch <r_resch@a1.net>
parents:
31607
diff
changeset
|
101 octave_quit (); |
d9970470108a
Remove several race conditions with signal handler (bug #61370).
Reinhard Resch <r_resch@a1.net>
parents:
31607
diff
changeset
|
102 } |
d9970470108a
Remove several race conditions with signal handler (bug #61370).
Reinhard Resch <r_resch@a1.net>
parents:
31607
diff
changeset
|
103 |
31605
e88a07dec498
maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents:
30565
diff
changeset
|
104 OCTAVE_END_NAMESPACE(octave) |
27471 | 105 |
4153 | 106 void |
5142 | 107 octave_handle_signal (void) |
108 { | |
109 if (octave_signal_hook) | |
110 octave_signal_hook (); | |
111 | |
31633
d9970470108a
Remove several race conditions with signal handler (bug #61370).
Reinhard Resch <r_resch@a1.net>
parents:
31607
diff
changeset
|
112 sig_atomic_t curr_interrupt_state = octave_interrupt_state.load (); |
27480
63b417917f5e
remove some obsolete signal handling functions
John W. Eaton <jwe@octave.org>
parents:
27471
diff
changeset
|
113 |
31633
d9970470108a
Remove several race conditions with signal handler (bug #61370).
Reinhard Resch <r_resch@a1.net>
parents:
31607
diff
changeset
|
114 while (curr_interrupt_state > 0 && |
d9970470108a
Remove several race conditions with signal handler (bug #61370).
Reinhard Resch <r_resch@a1.net>
parents:
31607
diff
changeset
|
115 ! octave_interrupt_state.compare_exchange_weak (curr_interrupt_state, -1)) |
d9970470108a
Remove several race conditions with signal handler (bug #61370).
Reinhard Resch <r_resch@a1.net>
parents:
31607
diff
changeset
|
116 ; |
d9970470108a
Remove several race conditions with signal handler (bug #61370).
Reinhard Resch <r_resch@a1.net>
parents:
31607
diff
changeset
|
117 |
d9970470108a
Remove several race conditions with signal handler (bug #61370).
Reinhard Resch <r_resch@a1.net>
parents:
31607
diff
changeset
|
118 if (curr_interrupt_state > 0) |
d9970470108a
Remove several race conditions with signal handler (bug #61370).
Reinhard Resch <r_resch@a1.net>
parents:
31607
diff
changeset
|
119 throw octave::interrupt_exception (); |
27520
7a871724d4b0
restore some exception handling functions and variables and deprecate them
John W. Eaton <jwe@octave.org>
parents:
27480
diff
changeset
|
120 } |