annotate libgui/src/interpreter-qobject.h @ 28347:00a9a49c7670 stable

improve interpreter shutdown process (bug #56952) This change is a further attempt to avoid segfaults when shutting down the interpreter and exiting the GUI event loop. The latest approach is to have the interpreter signal that it is finished with "normal" command execution (REPL, command line script, or --eval option code), then let the GUI thread process any remaining functions in its event loop(s) then signal back to the interpreter that it is OK to shutdown. Once the shutdown has happened (which may involve further calls to the GUI thread while executing atexit functions or finish.m or other shutdown code, the interpreter signals back to the GUI that shutdown is complete. At that point, the GUI can delete the interpreter object and exit. * ObjectProxy.h, ObjectProxy.cc (ObjectProxy::sendFinalize): New signal. (ObjectProxy::ObjectProxy): Connect/disconnect sendFinalize signal. (ObjectProxy::update, ObjectProxy::finalize): Use normal signal/slot connection. * interpreter-qobject.h, interpreter-qobject.cc (interpreter_qobject::ready): Rename from octave_ready_signal. Change all uses. (interpreter_qobject::execution_finished): Rename from octave_finished_singal. Change all uses. (interpreter_qobject::shutdown_finished): New signal. (interpreter_qobject::shutdown): New slot. (interpreter_qobject::execute): After interpreter finishes with normal execution, simply signal that execution has finished. Don't attempt to disable connecton to GUI or cleanup/delete interpreter. * octave-qobject.h, octave-qobject.cc (base_qobject::handle_interpreter_execution_finished): Rename from handle_octave_finished): Simply emit a signal requesting an orderly shutdown of the interpreter. Change all uses. (base_qobject::handle_interpreter_shutdown_finished): New slot. (base_qobject::request_interpreter_shutdown): New signal. (base_qobject::base_qobject): Connect request_interpreter_shutdown to interpreter_qobject::shutdown slot. * interpreter.h, interpreter.cc (interpreter::shutdown): Rename from cleanup and make public. (interpreter::~interpreter): Don't call cleanup here.
author John W. Eaton <jwe@octave.org>
date Fri, 22 May 2020 12:54:13 -0400
parents bd51beb6205e
children eb1e10abe9d5 0a5b15007766
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
3 // Copyright (C) 2011-2020 The Octave Project Developers
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 ////////////////////////////////////////////////////////////////////////
27196
d993642352d0 rename octave_interpreter object in GUI
John W. Eaton <jwe@octave.org>
parents:
diff changeset
25
d993642352d0 rename octave_interpreter object in GUI
John W. Eaton <jwe@octave.org>
parents:
diff changeset
26 #if ! defined (octave_interpreter_qobject_h)
d993642352d0 rename octave_interpreter object in GUI
John W. Eaton <jwe@octave.org>
parents:
diff changeset
27 #define octave_interpreter_qobject_h 1
d993642352d0 rename octave_interpreter object in GUI
John W. Eaton <jwe@octave.org>
parents:
diff changeset
28
d993642352d0 rename octave_interpreter object in GUI
John W. Eaton <jwe@octave.org>
parents:
diff changeset
29 #include <QObject>
d993642352d0 rename octave_interpreter object in GUI
John W. Eaton <jwe@octave.org>
parents:
diff changeset
30
27302
5f170ea12fa1 use Qt signals to pass interpreter callbacks to octave-qobject
John W. Eaton <jwe@octave.org>
parents: 27263
diff changeset
31 #include "qt-interpreter-events.h"
5f170ea12fa1 use Qt signals to pass interpreter callbacks to octave-qobject
John W. Eaton <jwe@octave.org>
parents: 27263
diff changeset
32
27196
d993642352d0 rename octave_interpreter object in GUI
John W. Eaton <jwe@octave.org>
parents:
diff changeset
33 namespace octave
d993642352d0 rename octave_interpreter object in GUI
John W. Eaton <jwe@octave.org>
parents:
diff changeset
34 {
27302
5f170ea12fa1 use Qt signals to pass interpreter callbacks to octave-qobject
John W. Eaton <jwe@octave.org>
parents: 27263
diff changeset
35 class interpreter;
5f170ea12fa1 use Qt signals to pass interpreter callbacks to octave-qobject
John W. Eaton <jwe@octave.org>
parents: 27263
diff changeset
36
27218
a044202208af more refactoring of GUI classes that manage the interpreter
John W. Eaton <jwe@octave.org>
parents: 27197
diff changeset
37 class base_qobject;
27196
d993642352d0 rename octave_interpreter object in GUI
John W. Eaton <jwe@octave.org>
parents:
diff changeset
38
d993642352d0 rename octave_interpreter object in GUI
John W. Eaton <jwe@octave.org>
parents:
diff changeset
39 class interpreter_qobject : public QObject
d993642352d0 rename octave_interpreter object in GUI
John W. Eaton <jwe@octave.org>
parents:
diff changeset
40 {
d993642352d0 rename octave_interpreter object in GUI
John W. Eaton <jwe@octave.org>
parents:
diff changeset
41 Q_OBJECT
d993642352d0 rename octave_interpreter object in GUI
John W. Eaton <jwe@octave.org>
parents:
diff changeset
42
d993642352d0 rename octave_interpreter object in GUI
John W. Eaton <jwe@octave.org>
parents:
diff changeset
43 public:
d993642352d0 rename octave_interpreter object in GUI
John W. Eaton <jwe@octave.org>
parents:
diff changeset
44
27588
f0e3f3e28a8e move qt_interpreter_events object from interpreter_qobject to octave_qobject
John W. Eaton <jwe@octave.org>
parents: 27302
diff changeset
45 interpreter_qobject (base_qobject& oct_qobj);
27196
d993642352d0 rename octave_interpreter object in GUI
John W. Eaton <jwe@octave.org>
parents:
diff changeset
46
27697
5e8891dceec7 backout changesets cdb681adc85a and b3401292e101 (bug #56952)
John W. Eaton <jwe@octave.org>
parents: 27695
diff changeset
47 ~interpreter_qobject (void) = default;
27695
cdb681adc85a improve exit behavior with qt graphics toolkit (bug #56952)
John W. Eaton <jwe@octave.org>
parents: 27694
diff changeset
48
27588
f0e3f3e28a8e move qt_interpreter_events object from interpreter_qobject to octave_qobject
John W. Eaton <jwe@octave.org>
parents: 27302
diff changeset
49 qt_interpreter_events * qt_link (void);
27218
a044202208af more refactoring of GUI classes that manage the interpreter
John W. Eaton <jwe@octave.org>
parents: 27197
diff changeset
50
27302
5f170ea12fa1 use Qt signals to pass interpreter callbacks to octave-qobject
John W. Eaton <jwe@octave.org>
parents: 27263
diff changeset
51 void interpreter_event (const fcn_callback& fcn);
5f170ea12fa1 use Qt signals to pass interpreter callbacks to octave-qobject
John W. Eaton <jwe@octave.org>
parents: 27263
diff changeset
52
5f170ea12fa1 use Qt signals to pass interpreter callbacks to octave-qobject
John W. Eaton <jwe@octave.org>
parents: 27263
diff changeset
53 void interpreter_event (const meth_callback& meth);
5f170ea12fa1 use Qt signals to pass interpreter callbacks to octave-qobject
John W. Eaton <jwe@octave.org>
parents: 27263
diff changeset
54
27196
d993642352d0 rename octave_interpreter object in GUI
John W. Eaton <jwe@octave.org>
parents:
diff changeset
55 signals:
d993642352d0 rename octave_interpreter object in GUI
John W. Eaton <jwe@octave.org>
parents:
diff changeset
56
28347
00a9a49c7670 improve interpreter shutdown process (bug #56952)
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
57 void ready (void);
27302
5f170ea12fa1 use Qt signals to pass interpreter callbacks to octave-qobject
John W. Eaton <jwe@octave.org>
parents: 27263
diff changeset
58
28347
00a9a49c7670 improve interpreter shutdown process (bug #56952)
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
59 void execution_finished (int);
00a9a49c7670 improve interpreter shutdown process (bug #56952)
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
60
00a9a49c7670 improve interpreter shutdown process (bug #56952)
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
61 void shutdown_finished (int);
27196
d993642352d0 rename octave_interpreter object in GUI
John W. Eaton <jwe@octave.org>
parents:
diff changeset
62
d993642352d0 rename octave_interpreter object in GUI
John W. Eaton <jwe@octave.org>
parents:
diff changeset
63 public slots:
d993642352d0 rename octave_interpreter object in GUI
John W. Eaton <jwe@octave.org>
parents:
diff changeset
64
27302
5f170ea12fa1 use Qt signals to pass interpreter callbacks to octave-qobject
John W. Eaton <jwe@octave.org>
parents: 27263
diff changeset
65 // Programming Note: With the current design of the interpreter,
5f170ea12fa1 use Qt signals to pass interpreter callbacks to octave-qobject
John W. Eaton <jwe@octave.org>
parents: 27263
diff changeset
66 // additional signals will not be noticed because the execute
5f170ea12fa1 use Qt signals to pass interpreter callbacks to octave-qobject
John W. Eaton <jwe@octave.org>
parents: 27263
diff changeset
67 // function starts the Octave interpreter and it doesn't return
5f170ea12fa1 use Qt signals to pass interpreter callbacks to octave-qobject
John W. Eaton <jwe@octave.org>
parents: 27263
diff changeset
68 // until the interpreter exits. So the Qt event loop running in the
5f170ea12fa1 use Qt signals to pass interpreter callbacks to octave-qobject
John W. Eaton <jwe@octave.org>
parents: 27263
diff changeset
69 // thread where the interpreter_qobject object lives never has a
5f170ea12fa1 use Qt signals to pass interpreter callbacks to octave-qobject
John W. Eaton <jwe@octave.org>
parents: 27263
diff changeset
70 // chance to see another signal. Changing the design of the
5f170ea12fa1 use Qt signals to pass interpreter callbacks to octave-qobject
John W. Eaton <jwe@octave.org>
parents: 27263
diff changeset
71 // terminal widget as proposed and discussed here:
5f170ea12fa1 use Qt signals to pass interpreter callbacks to octave-qobject
John W. Eaton <jwe@octave.org>
parents: 27263
diff changeset
72 //
5f170ea12fa1 use Qt signals to pass interpreter callbacks to octave-qobject
John W. Eaton <jwe@octave.org>
parents: 27263
diff changeset
73 // https://lists.gnu.org/archive/html/octave-maintainers/2019-05/msg00115.html
5f170ea12fa1 use Qt signals to pass interpreter callbacks to octave-qobject
John W. Eaton <jwe@octave.org>
parents: 27263
diff changeset
74 // https://lists.gnu.org/archive/html/octave-maintainers/2019-06/msg00009.html
5f170ea12fa1 use Qt signals to pass interpreter callbacks to octave-qobject
John W. Eaton <jwe@octave.org>
parents: 27263
diff changeset
75 //
5f170ea12fa1 use Qt signals to pass interpreter callbacks to octave-qobject
John W. Eaton <jwe@octave.org>
parents: 27263
diff changeset
76 // coulld solve that problem. Briefly, instead of having the Octave
5f170ea12fa1 use Qt signals to pass interpreter callbacks to octave-qobject
John W. Eaton <jwe@octave.org>
parents: 27263
diff changeset
77 // interpreter block and wait for keyboard input, the terminal
5f170ea12fa1 use Qt signals to pass interpreter callbacks to octave-qobject
John W. Eaton <jwe@octave.org>
parents: 27263
diff changeset
78 // widget would be in charge of accepting keyboard events and use
5f170ea12fa1 use Qt signals to pass interpreter callbacks to octave-qobject
John W. Eaton <jwe@octave.org>
parents: 27263
diff changeset
79 // readline in callback mode. Then the terminal widget and the
5f170ea12fa1 use Qt signals to pass interpreter callbacks to octave-qobject
John W. Eaton <jwe@octave.org>
parents: 27263
diff changeset
80 // interpreter will not block except when executing code. Then we
5f170ea12fa1 use Qt signals to pass interpreter callbacks to octave-qobject
John W. Eaton <jwe@octave.org>
parents: 27263
diff changeset
81 // could have the interpreter qobject directly accept signals.
5f170ea12fa1 use Qt signals to pass interpreter callbacks to octave-qobject
John W. Eaton <jwe@octave.org>
parents: 27263
diff changeset
82
27196
d993642352d0 rename octave_interpreter object in GUI
John W. Eaton <jwe@octave.org>
parents:
diff changeset
83 //! Initialize and execute the octave interpreter.
d993642352d0 rename octave_interpreter object in GUI
John W. Eaton <jwe@octave.org>
parents:
diff changeset
84
d993642352d0 rename octave_interpreter object in GUI
John W. Eaton <jwe@octave.org>
parents:
diff changeset
85 void execute (void);
d993642352d0 rename octave_interpreter object in GUI
John W. Eaton <jwe@octave.org>
parents:
diff changeset
86
28347
00a9a49c7670 improve interpreter shutdown process (bug #56952)
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
87 void shutdown (int);
00a9a49c7670 improve interpreter shutdown process (bug #56952)
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
88
27196
d993642352d0 rename octave_interpreter object in GUI
John W. Eaton <jwe@octave.org>
parents:
diff changeset
89 private:
d993642352d0 rename octave_interpreter object in GUI
John W. Eaton <jwe@octave.org>
parents:
diff changeset
90
27588
f0e3f3e28a8e move qt_interpreter_events object from interpreter_qobject to octave_qobject
John W. Eaton <jwe@octave.org>
parents: 27302
diff changeset
91 base_qobject& m_octave_qobj;
27218
a044202208af more refactoring of GUI classes that manage the interpreter
John W. Eaton <jwe@octave.org>
parents: 27197
diff changeset
92
27302
5f170ea12fa1 use Qt signals to pass interpreter callbacks to octave-qobject
John W. Eaton <jwe@octave.org>
parents: 27263
diff changeset
93 interpreter *m_interpreter;
27196
d993642352d0 rename octave_interpreter object in GUI
John W. Eaton <jwe@octave.org>
parents:
diff changeset
94 };
d993642352d0 rename octave_interpreter object in GUI
John W. Eaton <jwe@octave.org>
parents:
diff changeset
95 }
d993642352d0 rename octave_interpreter object in GUI
John W. Eaton <jwe@octave.org>
parents:
diff changeset
96
d993642352d0 rename octave_interpreter object in GUI
John W. Eaton <jwe@octave.org>
parents:
diff changeset
97 #endif