GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
quit.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2002-2024 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include <atomic>
31 #include <cstring>
32 
33 #include <ostream>
34 #include <sstream>
35 #include <new>
36 
37 #include "quit.h"
38 
39 std::atomic<sig_atomic_t> octave_interrupt_state{0};
40 
41 volatile std::atomic<bool> octave_signal_caught{false};
42 
43 void (*octave_signal_hook) () = nullptr;
44 void (*octave_interrupt_hook) () = nullptr;
45 
47 
48 std::string execution_exception::stack_trace () const
49 {
50  std::size_t nframes = m_stack_info.size ();
51 
52  if (nframes == 0)
53  return std::string ();
54 
55  std::ostringstream buf;
56 
57  buf << "error: called from\n";
58 
59  for (const auto& frm : m_stack_info)
60  {
61  buf << " " << frm.fcn_name ();
62 
63  int line = frm.line ();
64 
65  if (line > 0)
66  {
67  buf << " at line " << line;
68 
69  int column = frm.column ();
70 
71  if (column > 0)
72  buf << " column " << column;
73  }
74 
75  buf << "\n";
76  }
77 
78  return buf.str ();
79 }
80 
81 void
82 execution_exception::display (std::ostream& os) const
83 {
84  if (! m_message.empty ())
85  {
86  os << m_err_type << ": " << m_message;
87 
88  if (m_message.back () != '\n')
89  {
90  os << "\n";
91 
92  std::string st = stack_trace ();
93 
94  if (! st.empty ())
95  os << st;
96  }
97  }
98 }
99 
100 OCTAVE_END_NAMESPACE(octave)
101 
102 extern "C" OCTAVE_API void
104 {
105  octave_quit ();
106 }
107 
108 void
110 {
111  if (octave_signal_hook)
113 
114  sig_atomic_t curr_interrupt_state = octave_interrupt_state.load ();
115 
116  while (curr_interrupt_state > 0 &&
117  ! octave_interrupt_state.compare_exchange_weak (curr_interrupt_state, -1))
118  ;
119 
120  if (curr_interrupt_state > 0)
121  throw octave::interrupt_exception ();
122 }
line(const graphics_handle &mh, const graphics_handle &p)
Definition: graphics.h:7672
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define OCTAVE_API
Definition: main.cc:55
std::atomic< sig_atomic_t > octave_interrupt_state
Definition: quit.cc:39
void(* octave_interrupt_hook)()
Definition: quit.cc:44
void(* octave_signal_hook)()
Definition: quit.cc:43
void octave_quit_c(void)
Definition: quit.cc:103
volatile std::atomic< bool > octave_signal_caught
Definition: quit.cc:41
void octave_handle_signal()
Definition: quit.cc:109