[vlc-devel] commit: Qt4 window: use a proper condition variable instead of msleep() ( Rémi Denis-Courmont )

git version control git at videolan.org
Wed Jun 25 20:38:04 CEST 2008


vlc | branch: master | Rémi Denis-Courmont <rdenis at simphalempin.com> | Wed Jun 25 21:38:23 2008 +0300| [8c6064c922a385a981ed1e9140ba676047e93363]

Qt4 window: use a proper condition variable instead of msleep()

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=8c6064c922a385a981ed1e9140ba676047e93363
---

 modules/gui/qt4/main_interface.cpp |    5 -----
 modules/gui/qt4/qt4.cpp            |   34 +++++++++++++++++++++++++---------
 2 files changed, 25 insertions(+), 14 deletions(-)

diff --git a/modules/gui/qt4/main_interface.cpp b/modules/gui/qt4/main_interface.cpp
index 9de601e..d21cca0 100644
--- a/modules/gui/qt4/main_interface.cpp
+++ b/modules/gui/qt4/main_interface.cpp
@@ -437,11 +437,6 @@ void MainInterface::handleMainUi( QSettings *settings )
     {
         videoWidget = new VideoWidget( p_intf );
         mainLayout->insertWidget( 0, videoWidget, 10 );
-
-        var_Create( p_intf, "window_widget", VLC_VAR_ADDRESS );
-        vlc_value_t val;
-        val.p_address = this;
-        var_Set( p_intf, "window_widget", val );
     }
 
     /* Finish the sizing */
diff --git a/modules/gui/qt4/qt4.cpp b/modules/gui/qt4/qt4.cpp
index a425f1b..a9e2a65 100644
--- a/modules/gui/qt4/qt4.cpp
+++ b/modules/gui/qt4/qt4.cpp
@@ -29,6 +29,9 @@
 #include <QLocale>
 #include <QTranslator>
 #include <QDate>
+#include <QMutex>
+#include <QMutexLocker>
+#include <QWaitCondition>
 
 #include "qt4.hpp"
 #include "dialogs_provider.hpp"
@@ -241,6 +244,7 @@ static int Open( vlc_object_t *p_this )
     /* Listen to the messages */
     p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
 
+    var_Create( p_this, "window_widget", VLC_VAR_ADDRESS );
     return VLC_SUCCESS;
 }
 
@@ -291,8 +295,12 @@ static void Run( intf_thread_t *p_intf )
         Init( p_intf );
 }
 
+static QMutex windowLock;
+static QWaitCondition windowWait;
+
 static void Init( intf_thread_t *p_intf )
 {
+    vlc_value_t val;
     char dummy[] = "";
     char *argv[] = { dummy };
     int argc = 1;
@@ -331,6 +339,11 @@ static void Init( intf_thread_t *p_intf )
         /* We don't show it because it is done in the MainInterface constructor
         p_mi->show(); */
         p_intf->p_sys->b_isDialogProvider = false;
+
+        val.p_address = p_intf->p_sys->p_mi;
+        QMutexLocker locker (&windowLock);
+        var_Set (p_intf, "window_widget", val);
+        windowWait.wakeAll ();
     }
     else
     {
@@ -389,6 +402,9 @@ static void Init( intf_thread_t *p_intf )
 
     /* And quit */
 
+    QMutexLocker locker (&windowLock);
+    val.p_address = NULL;
+    var_Set (p_intf, "window_widget", val);
     /* Destroy first the main interface because it is connected to some slots
        in the MainInputManager */
     delete p_intf->p_sys->p_mi;
@@ -455,24 +471,24 @@ static int OpenWindow (vlc_object_t *obj)
     var_Create (intf, "window_mutex", VLC_VAR_MUTEX);
     var_Create (intf, "window_widget", VLC_VAR_ADDRESS);
 
-    vlc_value_t lockval, ptrval;
-    var_Get (intf, "window_mutex", &lockval);
+    vlc_value_t ptrval;
 
-    vlc_mutex_lock ((vlc_mutex_t *)lockval.p_address);
+    windowLock.lock ();
     msg_Dbg (obj, "waiting for interface...");
-    do
+    for (;;)
     {
         var_Get (intf, "window_widget", &ptrval);
-        /* FIXME A condition variable would be way more appropriate. */
-        msleep (INTF_IDLE_SLEEP);
-    } while (ptrval.p_address == NULL);
+        if (ptrval.p_address != NULL)
+            break;
+        windowWait.wait (&windowLock);
+    }
 
-    msg_Dbg (obj, "requestiong window...");
     MainInterface *mi = (MainInterface *)ptrval.p_address;
+    msg_Dbg (obj, "requesting window (%p)...", mi);
 
     wnd->handle = mi->requestVideo (wnd->vout, &wnd->pos_x, &wnd->pos_y,
                                     &wnd->width, &wnd->height);
-    vlc_mutex_unlock ((vlc_mutex_t *)lockval.p_address);
+    windowLock.unlock ();
     wnd->control = ControlWindow;
     wnd->p_private = intf;
     return VLC_SUCCESS;




More information about the vlc-devel mailing list