author | simon <simon> | 2002-12-14 17:55:12 (UTC) |
---|---|---|
committer | simon <simon> | 2002-12-14 17:55:12 (UTC) |
commit | 309c34a61e7cf6a199275c32c00ec1eaaa511d9b (patch) (unidiff) | |
tree | 5aa46eaefc040373397fcb3157ae6b5dd46a9c49 | |
parent | 03c4518324c328c530eb795705b6a4281d65055a (diff) | |
download | opie-309c34a61e7cf6a199275c32c00ec1eaaa511d9b.zip opie-309c34a61e7cf6a199275c32c00ec1eaaa511d9b.tar.gz opie-309c34a61e7cf6a199275c32c00ec1eaaa511d9b.tar.bz2 |
- make it thread-safe
-rw-r--r-- | noncore/multimedia/opieplayer2/singleton.h | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/noncore/multimedia/opieplayer2/singleton.h b/noncore/multimedia/opieplayer2/singleton.h index 83e228f..825344e 100644 --- a/noncore/multimedia/opieplayer2/singleton.h +++ b/noncore/multimedia/opieplayer2/singleton.h | |||
@@ -20,6 +20,8 @@ | |||
20 | #ifndef SINGLETON_H | 20 | #ifndef SINGLETON_H |
21 | #define SINGLETON_H | 21 | #define SINGLETON_H |
22 | 22 | ||
23 | #include "threadutil.h" | ||
24 | |||
23 | template <class Product> | 25 | template <class Product> |
24 | struct DefaultSingletonCreator | 26 | struct DefaultSingletonCreator |
25 | { | 27 | { |
@@ -42,8 +44,11 @@ class Singleton | |||
42 | public: | 44 | public: |
43 | static T &self() | 45 | static T &self() |
44 | { | 46 | { |
47 | if ( !s_self ) { | ||
48 | ThreadUtil::AutoLock lock( s_guard ); | ||
45 | if ( !s_self ) | 49 | if ( !s_self ) |
46 | s_self = Creator<T>::create(); | 50 | s_self = Creator<T>::create(); |
51 | } | ||
47 | return *s_self; | 52 | return *s_self; |
48 | } | 53 | } |
49 | 54 | ||
@@ -58,11 +63,15 @@ private: | |||
58 | Singleton<T, Creator> &operator=( const Singleton<T, Creator> &rhs ); | 63 | Singleton<T, Creator> &operator=( const Singleton<T, Creator> &rhs ); |
59 | 64 | ||
60 | static T *s_self; | 65 | static T *s_self; |
66 | static ThreadUtil::Mutex s_guard; | ||
61 | }; | 67 | }; |
62 | 68 | ||
63 | template <class T, template <class> class Creator> | 69 | template <class T, template <class> class Creator> |
64 | T *Singleton<T, Creator>::s_self = 0; | 70 | T *Singleton<T, Creator>::s_self = 0; |
65 | 71 | ||
72 | template <class T, template <class> class Creator> | ||
73 | ThreadUtil::Mutex Singleton<T, Creator>::s_guard; | ||
74 | |||
66 | #endif // SINGLETON_H | 75 | #endif // SINGLETON_H |
67 | /* vim: et sw=4 ts=4 | 76 | /* vim: et sw=4 ts=4 |
68 | */ | 77 | */ |