[vlc-devel] commit: Fixed QT4 message dialogs thread safety. (Laurent Aimar )

git version control git at videolan.org
Sat Jan 10 20:03:24 CET 2009


vlc | branch: master | Laurent Aimar <fenrir at videolan.org> | Sat Jan 10 20:00:54 2009 +0100| [92e66f6eadb1f6a23a54b998648c99ec236a892b] | committer: Laurent Aimar 

Fixed QT4 message dialogs thread safety.

I used customEvent to implement it.
(It renders the dedicated message thread per subscriber useless with qt4).

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

 modules/gui/qt4/dialogs/messages.cpp |   66 +++++++++++++++++++++++----------
 modules/gui/qt4/dialogs/messages.hpp |    7 ++--
 modules/gui/qt4/qt4.hpp              |    3 +-
 3 files changed, 52 insertions(+), 24 deletions(-)

diff --git a/modules/gui/qt4/dialogs/messages.cpp b/modules/gui/qt4/dialogs/messages.cpp
index b2e20b3..538de94 100644
--- a/modules/gui/qt4/dialogs/messages.cpp
+++ b/modules/gui/qt4/dialogs/messages.cpp
@@ -39,13 +39,35 @@
 #include <QHeaderView>
 #include <QMutex>
 
+#include <assert.h>
+
 MessagesDialog *MessagesDialog::instance = NULL;
 
+enum {
+    MsgEvent_Type = QEvent::User + MsgEventType + 1,
+};
+
+class MsgEvent : public QEvent
+{
+public:
+    MsgEvent( msg_item_t *msg )
+        : msg(msg), QEvent( (QEvent::Type)MsgEvent_Type )
+    {
+        msg_Hold( msg );
+    }
+    virtual ~MsgEvent()
+    {
+        msg_Release( msg );
+    }
+
+    msg_item_t *msg;
+};
+
 struct msg_cb_data_t
 {
     MessagesDialog *self;
-    QMutex          lock; /**< protects MessagesDialog::messages */
 };
+static void MsgCallback( msg_cb_data_t *, msg_item_t *, unsigned );
 
 MessagesDialog::MessagesDialog( intf_thread_t *_p_intf)
                : QVLCFrame( _p_intf )
@@ -116,16 +138,16 @@ MessagesDialog::MessagesDialog( intf_thread_t *_p_intf)
 
 
     /* Hook up to LibVLC messaging */
-    cb_data = new msg_cb_data_t;
-    cb_data->self = this;
-    sub = msg_Subscribe (_p_intf->p_libvlc, sinkMessage, cb_data);
+    cbData = new msg_cb_data_t;
+    cbData->self = this;
+    sub = msg_Subscribe( p_intf->p_libvlc, MsgCallback, cbData );
 }
 
-MessagesDialog::~MessagesDialog ()
+MessagesDialog::~MessagesDialog()
 {
     writeSettings( "Messages" );
-    msg_Unsubscribe (sub);
-    delete cb_data;
+    msg_Unsubscribe( sub );
+    delete cbData;
 };
 
 void MessagesDialog::updateTab( int index )
@@ -149,17 +171,6 @@ void MessagesDialog::updateTab( int index )
     }
 }
 
-void MessagesDialog::sinkMessage (msg_cb_data_t *data, msg_item_t *item,
-                                  unsigned overruns)
-{
-    MessagesDialog *self = data->self;
-    int canc = vlc_savecancel ();
-    QMutexLocker locker (&data->lock);
-
-    self->sinkMessage (item, overruns);
-    vlc_restorecancel (canc);
-}
-
 void MessagesDialog::sinkMessage (msg_item_t *item, unsigned)
 {
     if ((item->i_type == VLC_MSG_WARN && verbosityBox->value() < 1)
@@ -210,6 +221,13 @@ void MessagesDialog::sinkMessage (msg_item_t *item, unsigned)
     cur.movePosition( QTextCursor::NextCharacter, QTextCursor::KeepAnchor, endPos - startPos );
     messages->setTextCursor( cur );
 }
+void MessagesDialog::customEvent( QEvent *event )
+{
+    MsgEvent *msg = dynamic_cast<MsgEvent*>(event);
+
+    assert( msg );
+    sinkMessage( msg->msg, 0 );
+}
 
 void MessagesDialog::buildTree( QTreeWidgetItem *parentItem,
                                 vlc_object_t *p_obj )
@@ -253,7 +271,6 @@ void MessagesDialog::updateTree()
 
 void MessagesDialog::clear()
 {
-    QMutexLocker locker (&cb_data->lock);
     messages->clear();
 }
 
@@ -276,10 +293,19 @@ bool MessagesDialog::save()
         }
 
         QTextStream out( &file );
-        QMutexLocker locker (&cb_data->lock);
         out << messages->toPlainText() << "\n";
 
         return true;
     }
     return false;
 }
+
+static void MsgCallback( msg_cb_data_t *data, msg_item_t *item, unsigned )
+{
+    int canc = vlc_savecancel();
+
+    QApplication::postEvent( data->self, new MsgEvent( item ) );
+
+    vlc_restorecancel( canc );
+}
+
diff --git a/modules/gui/qt4/dialogs/messages.hpp b/modules/gui/qt4/dialogs/messages.hpp
index b4cc521..a4881cb 100644
--- a/modules/gui/qt4/dialogs/messages.hpp
+++ b/modules/gui/qt4/dialogs/messages.hpp
@@ -64,9 +64,10 @@ private:
     QPushButton *clearUpdateButton;
     QPushButton *saveLogButton;
     msg_subscription_t *sub;
-    msg_cb_data_t *cb_data;
-    static void sinkMessage (msg_cb_data_t *, msg_item_t *, unsigned);
-    void sinkMessage (msg_item_t *item, unsigned);
+    msg_cb_data_t *cbData;
+    static void sinkMessage( msg_cb_data_t *, msg_item_t *, unsigned );
+    void customEvent( QEvent * );
+    void sinkMessage( msg_item_t *item, unsigned );
 
 private slots:
     void updateTab( int );
diff --git a/modules/gui/qt4/qt4.hpp b/modules/gui/qt4/qt4.hpp
index cf27ae2..66283c8 100644
--- a/modules/gui/qt4/qt4.hpp
+++ b/modules/gui/qt4/qt4.hpp
@@ -48,7 +48,8 @@ enum {
 enum {
     DialogEventType = 0,
     IMEventType     = 100,
-    PLEventType     = 200
+    PLEventType     = 200,
+    MsgEventType    = 300,
 };
 
 class QApplication;




More information about the vlc-devel mailing list