Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1143)

Unified Diff: cpp/src/phonenumbers/base/memory/singleton_posix.h

Issue 10244046: CPP: Add suport for thread-safety on Linux and Mac in non-Boost base/. (Closed) Base URL: https://libphonenumber.googlecode.com/svn/trunk
Patch Set: Fix missing namespace Created 10 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « cpp/src/phonenumbers/base/memory/singleton.h ('k') | cpp/src/phonenumbers/base/synchronization/lock.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: cpp/src/phonenumbers/base/memory/singleton_posix.h
diff --git a/cpp/src/phonenumbers/base/memory/singleton_posix.h b/cpp/src/phonenumbers/base/memory/singleton_posix.h
new file mode 100644
index 0000000000000000000000000000000000000000..52cd04dee60e76672457d572a2f6bec1f2abeee6
--- /dev/null
+++ b/cpp/src/phonenumbers/base/memory/singleton_posix.h
@@ -0,0 +1,53 @@
+// Copyright (C) 2013 The Libphonenumber Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef I18N_PHONENUMBERS_BASE_MEMORY_SINGLETON_POSIX_H_
+#define I18N_PHONENUMBERS_BASE_MEMORY_SINGLETON_POSIX_H_
+
+#include <pthread.h>
+
+#include "phonenumbers/base/logging.h"
+
+namespace i18n {
+namespace phonenumbers {
+
+template <class T>
+class Singleton {
+ public:
+ virtual ~Singleton() {}
+
+ static T* GetInstance() {
+ const int ret = pthread_once(&once_control_, &Init);
+ (void) ret;
+ DCHECK_EQ(0, ret);
+ return instance_;
+ }
+
+ private:
+ static void Init() {
+ instance_ = new T();
+ }
+
+ static T* instance_; // Leaky singleton.
+ static pthread_once_t once_control_;
+};
+
+template <class T> T* Singleton<T>::instance_;
+template <class T> pthread_once_t Singleton<T>::once_control_ =
+ PTHREAD_ONCE_INIT;
+
+} // namespace phonenumbers
+} // namespace i18n
+
+#endif // I18N_PHONENUMBERS_BASE_MEMORY_SINGLETON_POSIX_H_
« no previous file with comments | « cpp/src/phonenumbers/base/memory/singleton.h ('k') | cpp/src/phonenumbers/base/synchronization/lock.h » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b