[vlc-commits] qt4: Make Singleton thread safe

Hugo Beauzée-Luyssen git at videolan.org
Thu Dec 10 16:11:35 CET 2015


vlc | branch: master | Hugo Beauzée-Luyssen <hugo at beauzee.fr> | Thu Dec 10 13:08:44 2015 +0100| [a375caeb32d97670aeb5b462d8d95ba28692b732] | committer: Hugo Beauzée-Luyssen

qt4: Make Singleton thread safe

This is unoptimal but could be improved using C++11 (or memory barriers
for double check locking)
fixes #14885

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

 modules/gui/qt4/util/singleton.hpp |   10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/modules/gui/qt4/util/singleton.hpp b/modules/gui/qt4/util/singleton.hpp
index f6f6050..c27e635 100644
--- a/modules/gui/qt4/util/singleton.hpp
+++ b/modules/gui/qt4/util/singleton.hpp
@@ -24,6 +24,8 @@
 #define _SINGLETON_HPP_
 
 #include <stdlib.h>
+#include <vlc_threads.h>
+
 #include "qt4.hpp"
 
 template <typename T>
@@ -32,18 +34,22 @@ class       Singleton
 public:
     static T*      getInstance( intf_thread_t *p_intf = NULL )
     {
+        vlc_mutex_lock( &m_mutex );
         if ( m_instance == NULL )
             m_instance = new T( p_intf );
+        vlc_mutex_unlock( &m_mutex );
         return m_instance;
     }
 
     static void    killInstance()
     {
+        vlc_mutex_lock( &m_mutex );
         if ( m_instance != NULL )
         {
             delete m_instance;
             m_instance = NULL;
         }
+        vlc_mutex_unlock( &m_mutex );
     }
 protected:
     Singleton(){}
@@ -55,9 +61,13 @@ protected:
 
 private:
     static T*      m_instance;
+    static vlc_mutex_t m_mutex;
 };
 
 template <typename T>
 T*  Singleton<T>::m_instance = NULL;
 
+template <typename T>
+vlc_mutex_t Singleton<T>::m_mutex = VLC_STATIC_MUTEX;
+
 #endif // _SINGLETON_HPP_



More information about the vlc-commits mailing list