]> git.sesse.net Git - vlc/commitdiff
* async_queue.*: AsyncQueue::remove is now thread-safe to avoid potential
authorCyril Deguet <asmax@videolan.org>
Sun, 29 Aug 2004 09:00:03 +0000 (09:00 +0000)
committerCyril Deguet <asmax@videolan.org>
Sun, 29 Aug 2004 09:00:03 +0000 (09:00 +0000)
  (but *very* unlikely) segfaults

modules/gui/skins2/commands/async_queue.cpp
modules/gui/skins2/commands/async_queue.hpp

index 43059ac8bc4b794979f833396c03b5a226b048b7..9d71edaccd4b2f2af2da8647ca59c82af4707b1c 100644 (file)
@@ -29,6 +29,9 @@
 
 AsyncQueue::AsyncQueue( intf_thread_t *pIntf ): SkinObject( pIntf )
 {
+    // Initialize the mutex
+    vlc_mutex_init( pIntf, &m_lock );
+
     // Create a timer
     OSFactory *pOsFactory = OSFactory::instance( pIntf );
     m_pTimer = pOsFactory->createOSTimer( Callback( this, &doFlush ) );
@@ -41,6 +44,7 @@ AsyncQueue::AsyncQueue( intf_thread_t *pIntf ): SkinObject( pIntf )
 AsyncQueue::~AsyncQueue()
 {
     delete( m_pTimer );
+    vlc_mutex_destroy( &m_lock );
 }
 
 
@@ -78,6 +82,8 @@ void AsyncQueue::push( const CmdGenericPtr &rcCommand )
 
 void AsyncQueue::remove( const string &rType )
 {
+    vlc_mutex_lock( &m_lock );
+
     list<CmdGenericPtr>::iterator it;
     for( it = m_cmdList.begin(); it != m_cmdList.end(); it++ )
     {
@@ -90,19 +96,25 @@ void AsyncQueue::remove( const string &rType )
             it = itNew;
         }
     }
+
+    vlc_mutex_unlock( &m_lock );
 }
 
 
 void AsyncQueue::flush()
 {
+    vlc_mutex_lock( &m_lock );
+
     while( m_cmdList.size() > 0 )
     {
-        // Execute the first command in the queue
-        CmdGenericPtr &rcCommand = m_cmdList.front();
-        rcCommand.get()->execute();
-        // And remove it
+        // Pop the first command from the queue
+        CmdGenericPtr cCommand = m_cmdList.front();
         m_cmdList.pop_front();
+        // And execute it
+        cCommand.get()->execute();
     }
+
+    vlc_mutex_unlock( &m_lock );
 }
 
 
index 8c7853016a7dcdebf5edd9d28830ac7b134a3900..5108e902acc3b3ad52bb2ae7d58a5693b051aaa5 100644 (file)
@@ -2,7 +2,7 @@
  * async_queue.hpp
  *****************************************************************************
  * Copyright (C) 2003 VideoLAN
- * $Id: async_queue.hpp,v 1.1 2004/01/03 23:31:33 asmax Exp $
+ * $Id$
  *
  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
  *          Olivier Teulière <ipkiss@via.ecp.fr>
@@ -58,6 +58,8 @@ class AsyncQueue: public SkinObject
         list<CmdGenericPtr> m_cmdList;
         /// Timer
         OSTimer *m_pTimer;
+        /// Mutex
+        vlc_mutex_t m_lock;
 
         // Private because it is a singleton
         AsyncQueue( intf_thread_t *pIntf );