changeset 17:2ddf3fe6fa33

initial attempt at using separate thread for interpreter
author John W. Eaton <jwe@octave.org>
date Tue, 28 May 2019 18:27:18 -0400
parents bfa9aaa2d608
children 7e93b74bd6c2
files calc.pro command-window.cpp command-window.h
diffstat 3 files changed, 36 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- 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
--- 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?
       }
   }
 
--- 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 <QTextDocument>
 #include <QTextEdit>
+#if defined (CALC_USE_INTERPRETER_THREAD)
+#  include <QThread>
+#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;