VirtualBox

Changeset 33666 in vbox


Ignore:
Timestamp:
Nov 1, 2010 7:21:19 PM (14 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
67286
Message:

FE/Qt: 4989: Lazy init VM settings dialog: QEventLoop replaced with QMutex + QWaitCondition.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/src/settings/UISettingsDialogSpecific.cpp

    r33639 r33666  
    2121#include <QStackedWidget>
    2222#include <QThread>
    23 #include <QTimer>
     23#include <QMutex>
     24#include <QWaitCondition>
    2425
    2526/* Local includes */
     
    7778        , m_direction(direction)
    7879        , m_data(data)
     80        , m_fConditionDone(false)
    7981        , m_iPageIdWeAreWaitingFor(-1)
    8082        , m_iIdOfHighPriorityPage(-1)
     
    8385        connect(this, SIGNAL(sigNotifyAboutPageProcessed(int)), this, SLOT(sltHandleProcessedPage(int)), Qt::QueuedConnection);
    8486        connect(this, SIGNAL(sigNotifyAboutPagesProcessed()), this, SLOT(sltHandleProcessedPages()), Qt::QueuedConnection);
    85         connect(this, SIGNAL(finished()), this, SLOT(sltDestroySerializer()), Qt::QueuedConnection);
     87        connect(this, SIGNAL(finished()), this, SLOT(deleteLater()), Qt::QueuedConnection);
    8688
    8789        /* Set instance: */
     
    100102        if (isRunning())
    101103            wait();
    102 
    103         /* If serializer still having event loop running,
    104          * we should quit it now: */
    105         if (m_eventLoop.isRunning())
    106         {
    107             m_eventLoop.processEvents();
    108             m_eventLoop.quit();
    109         }
    110104    }
    111105
     
    123117    void waitForPageToBeProcessed(int iPageId)
    124118    {
    125         if (!isRunning())
    126             return;
    127119        m_iPageIdWeAreWaitingFor = iPageId;
    128         m_eventLoop.exec();
     120        blockGUIthread();
    129121    }
    130122
     
    132124    void waitForPagesToBeProcessed()
    133125    {
    134         if (!isRunning())
    135             return;
    136126        m_iPageIdWeAreWaitingFor = -1;
    137         m_eventLoop.exec();
     127        blockGUIthread();
    138128    }
    139129
     
    192182                m_pages[iPageId]->getFromCache();
    193183        }
    194         /* If thats the page we are waiting for, unlock the loop,
    195          * after all the events of the page which is currently fetching
    196          * from cache will be processed: */
    197         if (iPageId == m_iPageIdWeAreWaitingFor && m_eventLoop.isRunning())
    198             QTimer::singleShot(0, this, SLOT(sltStopEventLoop()));
     184        /* If thats the page we are waiting for,
     185         * we should flag GUI thread to unlock itself: */
     186        if (iPageId == m_iPageIdWeAreWaitingFor && !m_fConditionDone)
     187            m_fConditionDone = true;
    199188    }
    200189
     
    202191    void sltHandleProcessedPages()
    203192    {
    204         /* If all the pages were processed, unlock the loop,
    205          * after all the events of the last page will be processed: */
    206         if (m_eventLoop.isRunning())
    207             QTimer::singleShot(0, this, SLOT(sltStopEventLoop()));
    208     }
    209 
    210     /* Slot to destroy serializer: */
    211     void sltDestroySerializer()
    212     {
    213         /* If event loop is still running,
    214          * we should try to destroy serializer only on next iteration: */
    215         if (m_eventLoop.isRunning())
    216             QTimer::singleShot(0, this, SLOT(sltDestroySerializer()));
    217         /* Else really make a request to destroy serializer: */
    218         else
    219             deleteLater();
    220     }
    221 
    222     /* Slot to stop event loop: */
    223     void sltStopEventLoop()
    224     {
    225         /* If event loop is still running, we should stop it: */
    226         if (m_eventLoop.isRunning())
    227         {
    228             m_eventLoop.processEvents();
    229             m_eventLoop.quit();
    230             m_eventLoop.processEvents();
    231         }
     193        /* We should flag GUI thread to unlock itself: */
     194        if (!m_fConditionDone)
     195            m_fConditionDone = true;
    232196    }
    233197
    234198protected:
     199
     200    /* GUI thread locker: */
     201    void blockGUIthread()
     202    {
     203        m_fConditionDone = false;
     204        while (!m_fConditionDone)
     205        {
     206            /* Lock mutex initially: */
     207            m_mutex.lock();
     208            /* Perform idle-processing every 100ms,
     209             * and waiting for direct wake up signal: */
     210            m_condition.wait(&m_mutex, 100);
     211            /* Process queued signals posted to GUI thread: */
     212            qApp->processEvents();
     213            /* Unlock mutex finally: */
     214            m_mutex.unlock();
     215        }
     216    }
    235217
    236218    /* Settings processor: */
     
    257239            /* Remember what page was processed: */
    258240            pPage->setProcessed(true);
     241            /* Remove processed page from our map: */
     242            pages.remove(pPage->id());
    259243            /* Notify listeners about page was processed: */
    260244            emit sigNotifyAboutPageProcessed(pPage->id());
    261             /* Remove processed page from our map: */
    262             pages.remove(pPage->id());
     245            /* Try to wake up GUI thread, but
     246             * it can be busy idle-processing for loaded pages: */
     247            if (!m_fConditionDone)
     248                m_condition.wakeAll();
    263249        }
    264250        /* Notify listeners about all pages were processed: */
    265251        emit sigNotifyAboutPagesProcessed();
     252        /* Try to wake up GUI thread, but
     253         * it can be busy idle-processing loaded pages: */
     254        if (!m_fConditionDone)
     255            m_condition.wakeAll();
    266256    }
    267257
     
    269259    UISettingsSerializeDirection m_direction;
    270260    QVariant m_data;
    271     QEventLoop m_eventLoop;
    272261    UISettingsPageMap m_pages;
     262    bool m_fConditionDone;
    273263    int m_iPageIdWeAreWaitingFor;
    274264    int m_iIdOfHighPriorityPage;
     265    QMutex m_mutex;
     266    QWaitCondition m_condition;
    275267    static UISettingsSerializer *m_pInstance;
    276268};
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette