# HG changeset patch # User John W. Eaton # Date 1559082438 14400 # Node ID 2ddf3fe6fa33f768ac8dae04b5cf0d5cd707a258 # Parent bfa9aaa2d6080ef36366987ca7ae86b77b5e8ad2 initial attempt at using separate thread for interpreter diff -r bfa9aaa2d608 -r 2ddf3fe6fa33 calc.pro --- a/calc.pro Fri May 24 09:22:35 2019 -0400 +++ b/calc.pro Tue May 28 18:27:18 2019 -0400 @@ -28,3 +28,7 @@ QMAKE_EXTRA_TARGETS += extraclean extraclean.commands = rm -f parse.cpp + +## Use the following to experiment with running the interpreter +## in a separate thread. +## QMAKE_CXXFLAGS += -DCALC_USE_INTERPRETER_THREAD=1 diff -r bfa9aaa2d608 -r 2ddf3fe6fa33 command-window.cpp --- a/command-window.cpp Fri May 24 09:22:35 2019 -0400 +++ b/command-window.cpp Tue May 28 18:27:18 2019 -0400 @@ -116,7 +116,11 @@ command_window *command_window::the_command_window = nullptr; command_window::command_window (QWidget *p) - : QTextEdit (p), m_buffer (new QTextDocument ()), + : QTextEdit (p), +#if defined (CALC_USE_INTERPRETER_THREAD) + m_interpreter_thread (), +#endif + m_buffer (new QTextDocument ()), m_interpreter (new calc::qt_interpreter ()), beg_mark (), prompt_mark () { @@ -134,6 +138,13 @@ setDocument (m_buffer); +#if defined (CALC_USE_INTERPRETER_THREAD) + m_interpreter->moveToThread (&m_interpreter_thread); + + connect (&m_interpreter_thread, &QThread::finished, + m_interpreter, &QObject::deleteLater); +#endif + connect (m_interpreter, SIGNAL (result_ready (double)), this, SLOT (handle_result (double))); @@ -163,6 +174,10 @@ readline_init (); +#if defined (CALC_USE_INTERPRETER_THREAD) + m_interpreter_thread.start (); +#endif + // Defer initializing and executing the interpreter until after the main // window and QApplication are running to prevent race conditions QTimer::singleShot (0, m_interpreter, SLOT (execute (void))); @@ -171,6 +186,11 @@ command_window::~command_window (void) { readline_fini (); + +#if defined (CALC_USE_INTERPRETER_THREAD) + m_interpreter_thread.quit (); + m_interpreter_thread.wait (); +#endif } // Accept an input line, parse and possibly execute it. @@ -185,6 +205,10 @@ using_history (); emit accept_line_signal (QString::fromStdString (line)); + + // FIXME: how to wait until interpreter is finished and + // results are printed before returning to readline callback + // that will print the next prompt? } } diff -r bfa9aaa2d608 -r 2ddf3fe6fa33 command-window.h --- a/command-window.h Fri May 24 09:22:35 2019 -0400 +++ b/command-window.h Tue May 28 18:27:18 2019 -0400 @@ -5,6 +5,9 @@ #include #include +#if defined (CALC_USE_INTERPRETER_THREAD) +# include +#endif #include "qt-interpreter.h" @@ -81,6 +84,10 @@ void scroll_to_bottom (void); +#if defined (CALC_USE_INTERPRETER_THREAD) + QThread m_interpreter_thread; +#endif + // Child widgets: QTextDocument *m_buffer;