Mercurial > jwe > octave
changeset 24016:e327adeae7a0
hide mutex and wait condition data members in GUI objects
* dialog.h, dialog.cc (class QUIWidgetCreator): Make mutex and
waitcondition data members private. Provide lock, unlock, wait, and
wake_all functions. Change all uses.
* octave-qt-link.h, octave-qt-link.cc (class octave_qt_link):
Likewise.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 06 Sep 2017 15:00:52 -0400 |
parents | 5ecefb7425e3 |
children | b0e696ea6487 |
files | libgui/src/dialog.cc libgui/src/dialog.h libgui/src/main-window.cc libgui/src/octave-qt-link.cc libgui/src/octave-qt-link.h |
diffstat | 5 files changed, 56 insertions(+), 47 deletions(-) [+] |
line wrap: on
line diff
--- a/libgui/src/dialog.cc Wed Sep 06 13:56:50 2017 -0400 +++ b/libgui/src/dialog.cc Wed Sep 06 15:00:52 2017 -0400 @@ -61,7 +61,7 @@ QUIWidgetCreator::dialog_button_clicked (QAbstractButton *button) { // Wait for link thread to go to sleep state. - mutex.lock (); + lock (); // Store the value so that builtin functions can retrieve. if (button) @@ -70,10 +70,10 @@ // The value should always be 1 for the Octave functions. m_dialog_result = 1; - mutex.unlock (); + unlock (); // Wake up Octave process so that it continues. - waitcondition.wakeAll (); + wake_all (); } void @@ -81,32 +81,32 @@ int button_pressed) { // Wait for link thread to go to sleep state. - mutex.lock (); + lock (); // Store the value so that builtin functions can retrieve. *m_list_index = selected; m_dialog_result = button_pressed; - mutex.unlock (); + unlock (); // Wake up Octave process so that it continues. - waitcondition.wakeAll (); + wake_all (); } void QUIWidgetCreator::input_finished (const QStringList& input, int button_pressed) { // Wait for link thread to go to sleep state. - mutex.lock (); + lock (); // Store the value so that builtin functions can retrieve. *m_string_list = input; m_dialog_result = button_pressed; - mutex.unlock (); + unlock (); // Wake up Octave process so that it continues. - waitcondition.wakeAll (); + wake_all (); } void @@ -114,17 +114,17 @@ const QString& path, int filterindex) { // Wait for link thread to go to sleep state. - mutex.lock (); + lock (); // Store the value so that builtin functions can retrieve. *m_string_list = files; m_dialog_result = filterindex; *m_path_name = path; - mutex.unlock (); + unlock (); // Wake up Octave process so that it continues. - waitcondition.wakeAll (); + wake_all (); } MessageDialog::MessageDialog (const QString& message,
--- a/libgui/src/dialog.h Wed Sep 06 13:56:50 2017 -0400 +++ b/libgui/src/dialog.h Wed Sep 06 15:00:52 2017 -0400 @@ -114,11 +114,10 @@ const QString * get_dialog_path (void) { return m_path_name; } - // GUI objects cannot be accessed in the non-GUI thread. However, - // signals can be sent to slots across threads with proper - // synchronization. Hence, the use of QWaitCondition. - QMutex mutex; - QWaitCondition waitcondition; + void lock (void) { m_mutex.lock (); } + void wait (void) { m_waitcondition.wait (&m_mutex); } + void unlock (void) { m_mutex.unlock (); } + void wake_all (void) { m_waitcondition.wakeAll (); } signals: @@ -158,6 +157,12 @@ QIntList *m_list_index; QString *m_path_name; + + // GUI objects cannot be accessed in the non-GUI thread. However, + // signals can be sent to slots across threads with proper + // synchronization. Hence, the use of QWaitCondition. + QMutex m_mutex; + QWaitCondition m_waitcondition; }; extern QUIWidgetCreator uiwidget_creator;
--- a/libgui/src/main-window.cc Wed Sep 06 13:56:50 2017 -0400 +++ b/libgui/src/main-window.cc Wed Sep 06 15:00:52 2017 -0400 @@ -937,15 +937,14 @@ } // Wait for link thread to go to sleep state. - _octave_qt_link->mutex.lock (); + _octave_qt_link->lock (); _octave_qt_link->shutdown_confirmation (closenow); - _octave_qt_link->mutex.unlock (); + _octave_qt_link->unlock (); // Awake the worker thread so that it continues shutting down (or not). - _octave_qt_link->waitcondition.wakeAll (); - + _octave_qt_link->wake_all (); } void
--- a/libgui/src/octave-qt-link.cc Wed Sep 06 13:56:50 2017 -0400 +++ b/libgui/src/octave-qt-link.cc Wed Sep 06 15:00:52 2017 -0400 @@ -58,16 +58,16 @@ octave_qt_link::do_confirm_shutdown (void) { // Lock the mutex before emitting signal. - mutex.lock (); + lock (); emit confirm_shutdown_signal (); // Wait while the GUI shuts down. - waitcondition.wait (&mutex); + wait (); // The GUI has sent a signal and the thread has been awakened. - mutex.unlock (); + unlock (); return m_shutdown_confirm_result; } @@ -103,7 +103,7 @@ btn << tr ("Create") << tr ("Cancel"); // Lock mutex before signaling. - uiwidget_creator.mutex.lock (); + uiwidget_creator.lock (); uiwidget_creator.signal_dialog ( tr ("File\n%1\ndoes not exist. Do you want to create it?"). @@ -112,13 +112,13 @@ tr ("Octave Editor"), "quest", btn, tr ("Create"), role); // Wait while the user is responding to message box. - uiwidget_creator.waitcondition.wait (&uiwidget_creator.mutex); + uiwidget_creator.wait (); // The GUI has sent a signal and the thread has been awakened. QString answer = uiwidget_creator.get_dialog_button (); - uiwidget_creator.mutex.unlock (); + uiwidget_creator.unlock (); return (answer == tr ("Create")); } @@ -129,7 +129,7 @@ const std::string& title) { // Lock mutex before signaling. - uiwidget_creator.mutex.lock (); + uiwidget_creator.lock (); uiwidget_creator.signal_dialog (QString::fromStdString (msg), QString::fromStdString (title), @@ -138,13 +138,13 @@ QStringList ()); // Wait while the user is responding to message box. - uiwidget_creator.waitcondition.wait (&uiwidget_creator.mutex); + uiwidget_creator.wait (); // The GUI has sent a signal and the thread has been awakened. int answer = uiwidget_creator.get_dialog_result (); - uiwidget_creator.mutex.unlock (); + uiwidget_creator.unlock (); return answer; } @@ -169,7 +169,7 @@ btn << QString::fromStdString (btn3); // Lock mutex before signaling. - uiwidget_creator.mutex.lock (); + uiwidget_creator.lock (); uiwidget_creator.signal_dialog (QString::fromStdString (msg), QString::fromStdString (title), @@ -179,13 +179,13 @@ role); // Wait while the user is responding to message box. - uiwidget_creator.waitcondition.wait (&uiwidget_creator.mutex); + uiwidget_creator.wait (); // The GUI has sent a signal and the thread has been awakened. std::string answer = uiwidget_creator.get_dialog_button ().toStdString (); - uiwidget_creator.mutex.unlock (); + uiwidget_creator.unlock (); return answer; } @@ -249,7 +249,7 @@ const std::string& cancel_string) { // Lock mutex before signaling. - uiwidget_creator.mutex.lock (); + uiwidget_creator.lock (); uiwidget_creator.signal_listview (make_qstring_list (list), QString::fromStdString (mode), @@ -261,14 +261,14 @@ QString::fromStdString (cancel_string)); // Wait while the user is responding to message box. - uiwidget_creator.waitcondition.wait (&uiwidget_creator.mutex); + uiwidget_creator.wait (); // The GUI has sent a signal and the thread has been awakened. const QIntList *selected = uiwidget_creator.get_list_index (); int ok = uiwidget_creator.get_dialog_result (); - uiwidget_creator.mutex.unlock (); + uiwidget_creator.unlock (); return std::pair<std::list<int>, int> (selected->toStdList (), ok); } @@ -283,7 +283,7 @@ std::list<std::string> retval; // Lock mutex before signaling. - uiwidget_creator.mutex.lock (); + uiwidget_creator.lock (); uiwidget_creator.signal_inputlayout (make_qstring_list (prompt), QString::fromStdString (title), @@ -292,13 +292,13 @@ make_qstring_list (defaults)); // Wait while the user is responding to message box. - uiwidget_creator.waitcondition.wait (&uiwidget_creator.mutex); + uiwidget_creator.wait (); // The GUI has sent a signal and the thread has been awakened. const QStringList *inputLine = uiwidget_creator.get_string_list (); - uiwidget_creator.mutex.unlock (); + uiwidget_creator.unlock (); for (QStringList::const_iterator it = inputLine->begin (); it != inputLine->end (); it++) @@ -319,7 +319,7 @@ std::list<std::string> retval; // Lock mutex before signaling. - uiwidget_creator.mutex.lock (); + uiwidget_creator.lock (); uiwidget_creator.signal_filedialog (make_filter_list (filter), QString::fromStdString (title), @@ -328,7 +328,7 @@ QString::fromStdString (multimode)); // Wait while the user is responding to dialog. - uiwidget_creator.waitcondition.wait (&uiwidget_creator.mutex); + uiwidget_creator.wait (); // The GUI has sent a signal and the thread has been awakened. @@ -343,7 +343,7 @@ retval.push_back ((QString ("%1").arg ( uiwidget_creator.get_dialog_result ())).toStdString ()); - uiwidget_creator.mutex.unlock (); + uiwidget_creator.unlock (); return retval; } @@ -386,18 +386,18 @@ role << "RejectRole"; // Lock mutex before signaling. - uiwidget_creator.mutex.lock (); + uiwidget_creator.lock (); uiwidget_creator.signal_dialog (msg, title, "quest", btn, cancel_txt, role); // Wait while the user is responding to message box. - uiwidget_creator.waitcondition.wait (&uiwidget_creator.mutex); + uiwidget_creator.wait (); // The GUI has sent a signal and the thread has been awakened. QString result = uiwidget_creator.get_dialog_button (); - uiwidget_creator.mutex.unlock (); + uiwidget_creator.unlock (); if (result == cd_txt) retval = 1;
--- a/libgui/src/octave-qt-link.h Wed Sep 06 13:56:50 2017 -0400 +++ b/libgui/src/octave-qt-link.h Wed Sep 06 15:00:52 2017 -0400 @@ -145,8 +145,10 @@ void shutdown_confirmation (bool sd) { m_shutdown_confirm_result = sd; } - QMutex mutex; - QWaitCondition waitcondition; + void lock (void) { m_mutex.lock (); } + void wait (void) { m_waitcondition.wait (&m_mutex); } + void unlock (void) { m_mutex.unlock (); } + void wake_all (void) { m_waitcondition.wakeAll (); } private: @@ -160,6 +162,9 @@ QString m_current_directory; bool m_new_dir; + QMutex m_mutex; + QWaitCondition m_waitcondition; + signals: void copy_image_to_clipboard_signal (const QString& file, bool remove_file);