# HG changeset patch # User Torsten # Date 1451818159 -3600 # Node ID 4a7d9c335402d0cf818c80b642cd02fd8a5853c0 # Parent 8000dacaea986441151f0f022ca4911d757a3192 GUI: move the command queue into a separate class * main-window.cc (main_window): remove initializations of queue synchronization; (execute_command_in_terminal,run_file_callback,debug_continue,debug_step_into, debug_step_over,debug_step_out,debug_quit,closeEvent): adapt queuing commands; (queue_command): remove function for queuing a command; (execute_command_callback): move function from here into octave-cmd; * main-window.h: remove class variables related to the command queue_command * module.mk: add octave-cmd to the moc-list * octave-cmd.cc: new class octave_command_queue; (add_cmd): method for adding a command to the queue; (execute_command_callback): method for executing commands from the queue * octave-cmd.h: new class octave_command_queue diff -r 8000dacaea98 -r 4a7d9c335402 libgui/src/main-window.cc --- a/libgui/src/main-window.cc Sun Jan 03 11:49:16 2016 +0100 +++ b/libgui/src/main-window.cc Sun Jan 03 11:49:19 2016 +0100 @@ -89,9 +89,6 @@ community_news_window (0), _octave_qt_link (0), _clipboard (QApplication::clipboard ()), - _cmd_queue (QList ()), // no command pending - _cmd_processing (1), - _cmd_queue_mutex (), _prevent_readline_conflicts (true), _suppress_dbg_location (true), _start_gui (start_gui) @@ -325,7 +322,7 @@ main_window::execute_command_in_terminal (const QString& command) { octave_cmd_exec *cmd = new octave_cmd_exec (command); - queue_command (cmd); + _cmd_queue.add_cmd (cmd); if (focus_console_after_command ()) focus_command_window (); } @@ -342,18 +339,7 @@ main_window::run_file_callback (const QFileInfo& info) { octave_cmd_eval *cmd = new octave_cmd_eval (info); - queue_command (cmd); -} - -void -main_window::queue_command (octave_cmd* cmd) -{ - _cmd_queue_mutex.lock (); - _cmd_queue.append (cmd); // queue command and type - _cmd_queue_mutex.unlock (); - - if (_cmd_processing.tryAcquire ()) // if callback not processing, post event - octave_link::post_event (this, &main_window::execute_command_callback); + _cmd_queue.add_cmd (cmd); } void @@ -956,35 +942,35 @@ main_window::debug_continue (void) { octave_cmd_debug *cmd = new octave_cmd_debug ("cont", _suppress_dbg_location); - queue_command (cmd); + _cmd_queue.add_cmd (cmd); } void main_window::debug_step_into (void) { octave_cmd_debug *cmd = new octave_cmd_debug ("in", _suppress_dbg_location); - queue_command (cmd); + _cmd_queue.add_cmd (cmd); } void main_window::debug_step_over (void) { octave_cmd_debug *cmd = new octave_cmd_debug ("step", _suppress_dbg_location); - queue_command (cmd); + _cmd_queue.add_cmd (cmd); } void main_window::debug_step_out (void) { octave_cmd_debug *cmd = new octave_cmd_debug ("out", _suppress_dbg_location); - queue_command (cmd); + _cmd_queue.add_cmd (cmd); } void main_window::debug_quit (void) { octave_cmd_debug *cmd = new octave_cmd_debug ("quit", _suppress_dbg_location); - queue_command (cmd); + _cmd_queue.add_cmd (cmd); } void @@ -1039,7 +1025,7 @@ { e->ignore (); octave_cmd_exec *cmd = new octave_cmd_exec ("exit"); - queue_command (cmd); + _cmd_queue.add_cmd (cmd); } void @@ -2128,33 +2114,6 @@ } void -main_window::execute_command_callback () -{ - bool repost = false; // flag for reposting event for this callback - - if (! _cmd_queue.isEmpty ()) // list can not be empty here, just to make sure - { - _cmd_queue_mutex.lock (); // critical path - - octave_cmd *cmd = _cmd_queue.takeFirst (); - - if (_cmd_queue.isEmpty ()) - _cmd_processing.release (); // cmd queue empty, processing will stop - else - repost = true; // not empty, repost at end - _cmd_queue_mutex.unlock (); - - cmd->execute (); - - delete cmd; - } - - if (repost) // queue not empty, so repost event for further processing - octave_link::post_event (this, &main_window::execute_command_callback); - -} - -void main_window::new_figure_callback (void) { Fbuiltin (ovl ("figure")); diff -r 8000dacaea98 -r 4a7d9c335402 libgui/src/main-window.h --- a/libgui/src/main-window.h Sun Jan 03 11:49:16 2016 +0100 +++ b/libgui/src/main-window.h Sun Jan 03 11:49:19 2016 +0100 @@ -36,7 +36,6 @@ #include #include #include -#include #include // Editor includes @@ -395,11 +394,9 @@ // and related callback // the queue for the command structures - QList _cmd_queue; - // semaphores used for handling the queue - QSemaphore _cmd_processing; - QMutex _cmd_queue_mutex; + octave_command_queue _cmd_queue; + // some class global flags bool _prevent_readline_conflicts; bool _suppress_dbg_location; bool _start_gui; diff -r 8000dacaea98 -r 4a7d9c335402 libgui/src/module.mk --- a/libgui/src/module.mk Sun Jan 03 11:49:16 2016 +0100 +++ b/libgui/src/module.mk Sun Jan 03 11:49:19 2016 +0100 @@ -87,6 +87,7 @@ libgui/src/moc-files-dock-widget.cc \ libgui/src/moc-history-dock-widget.cc \ libgui/src/moc-main-window.cc \ + libgui/src/moc-octave-cmd.cc \ libgui/src/moc-octave-interpreter.cc \ libgui/src/moc-octave-qt-link.cc \ libgui/src/moc-settings-dialog.cc \ diff -r 8000dacaea98 -r 4a7d9c335402 libgui/src/octave-cmd.cc --- a/libgui/src/octave-cmd.cc Sun Jan 03 11:49:16 2016 +0100 +++ b/libgui/src/octave-cmd.cc Sun Jan 03 11:49:19 2016 +0100 @@ -108,3 +108,49 @@ command_editor::interrupt (true); } + + +// --------------------------------------------------------------------- +// class octave_command_queue: queue of octave commands + +// add_cmd: add a command to the queue +void +octave_command_queue::add_cmd (octave_cmd* cmd) +{ + _queue_mutex.lock (); + _queue.append (cmd); + _queue_mutex.unlock (); + + if (_processing.tryAcquire ()) // if callback not processing, post event + octave_link::post_event (this, + &octave_command_queue::execute_command_callback); +} + +// callback for executing the command by the worker thread +void +octave_command_queue::execute_command_callback () +{ + bool repost = false; // flag for reposting event for this callback + + if (! _queue.isEmpty ()) // list can not be empty here, just to make sure + { + _queue_mutex.lock (); // critical path + + octave_cmd *cmd = _queue.takeFirst (); + + if (_queue.isEmpty ()) + _processing.release (); // cmd queue empty, processing will stop + else + repost = true; // not empty, repost at end + _queue_mutex.unlock (); + + cmd->execute (); + + delete cmd; + } + + if (repost) // queue not empty, so repost event for further processing + octave_link::post_event (this, + &octave_command_queue::execute_command_callback); + +} diff -r 8000dacaea98 -r 4a7d9c335402 libgui/src/octave-cmd.h --- a/libgui/src/octave-cmd.h Sun Jan 03 11:49:16 2016 +0100 +++ b/libgui/src/octave-cmd.h Sun Jan 03 11:49:19 2016 +0100 @@ -25,9 +25,13 @@ #if ! defined (octave_cmd_h) #define octave_cmd_h 1 +#include +#include #include #include +#include "octave-qt-link.h" + class octave_cmd { public: @@ -90,4 +94,43 @@ bool _suppress_dbg_location; }; + + +/** + * @class octave_command_queue + * + * Queuing commands from the GUI for the worker thread + */ +// --------------------------------------------------------------------- +// class octave_command_queue: queue of octave commands + +class octave_command_queue : QObject +{ + Q_OBJECT; + +public: + + octave_command_queue (void) : QObject (), + _queue (QList ()), + _processing (1), + _queue_mutex () { }; + ~octave_command_queue (void) { }; + + /** + * Adds a new octave command to the command queue. + * @param cmd The octave command to be queued + */ + void add_cmd (octave_cmd *cmd); + /** + * Callback routine for executing the command by the worker thread + */ + void execute_command_callback (void); + +private: + + QList _queue; + QSemaphore _processing; + QMutex _queue_mutex; +}; + #endif