Commit a375caeb authored by Hugo Beauzée-Luyssen's avatar 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
parent 33b2308f
...@@ -24,6 +24,8 @@ ...@@ -24,6 +24,8 @@
#define _SINGLETON_HPP_ #define _SINGLETON_HPP_
#include <stdlib.h> #include <stdlib.h>
#include <vlc_threads.h>
#include "qt4.hpp" #include "qt4.hpp"
template <typename T> template <typename T>
...@@ -32,18 +34,22 @@ class Singleton ...@@ -32,18 +34,22 @@ class Singleton
public: public:
static T* getInstance( intf_thread_t *p_intf = NULL ) static T* getInstance( intf_thread_t *p_intf = NULL )
{ {
vlc_mutex_lock( &m_mutex );
if ( m_instance == NULL ) if ( m_instance == NULL )
m_instance = new T( p_intf ); m_instance = new T( p_intf );
vlc_mutex_unlock( &m_mutex );
return m_instance; return m_instance;
} }
static void killInstance() static void killInstance()
{ {
vlc_mutex_lock( &m_mutex );
if ( m_instance != NULL ) if ( m_instance != NULL )
{ {
delete m_instance; delete m_instance;
m_instance = NULL; m_instance = NULL;
} }
vlc_mutex_unlock( &m_mutex );
} }
protected: protected:
Singleton(){} Singleton(){}
...@@ -55,9 +61,13 @@ protected: ...@@ -55,9 +61,13 @@ protected:
private: private:
static T* m_instance; static T* m_instance;
static vlc_mutex_t m_mutex;
}; };
template <typename T> template <typename T>
T* Singleton<T>::m_instance = NULL; T* Singleton<T>::m_instance = NULL;
template <typename T>
vlc_mutex_t Singleton<T>::m_mutex = VLC_STATIC_MUTEX;
#endif // _SINGLETON_HPP_ #endif // _SINGLETON_HPP_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment