| @ -0,0 +1,51 @@ | |||
| // Copyright (C) 2020 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_BOOST_H_ | |||
| #define I18N_PHONENUMBERS_BASE_MEMORY_SINGLETON_BOOST_H_ | |||
| #include <boost/scoped_ptr.hpp> | |||
| #include <boost/thread/once.hpp> | |||
| #include <boost/utility.hpp> | |||
| namespace i18n { | |||
| namespace phonenumbers { | |||
| template <class T> | |||
| class Singleton : private boost::noncopyable { | |||
| public: | |||
| Singleton() {} | |||
| virtual ~Singleton() {} | |||
| static T* GetInstance() { | |||
| boost::call_once(Init, flag_); | |||
| return instance_.get(); | |||
| } | |||
| private: | |||
| static void Init() { | |||
| instance_.reset(new T()); | |||
| } | |||
| static boost::scoped_ptr<T> instance_; | |||
| static boost::once_flag flag_; | |||
| }; | |||
| template <class T> boost::scoped_ptr<T> Singleton<T>::instance_; | |||
| template <class T> boost::once_flag Singleton<T>::flag_ = BOOST_ONCE_INIT; | |||
| } // namespace phonenumbers | |||
| } // namespace i18n | |||
| #endif // I18N_PHONENUMBERS_BASE_MEMORY_SINGLETON_BOOST_H_ | |||
| @ -0,0 +1,62 @@ | |||
| // Copyright (C) 2020 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_STDMUTEX_H_ | |||
| #define I18N_PHONENUMBERS_BASE_MEMORY_SINGLETON_STDMUTEX_H_ | |||
| #include <mutex> | |||
| #include "phonenumbers/base/basictypes.h" | |||
| namespace i18n { | |||
| namespace phonenumbers { | |||
| template <class T> | |||
| class Singleton { | |||
| public: | |||
| Singleton() {} | |||
| virtual ~Singleton() {} | |||
| static T* GetInstance() { | |||
| if (once_init_) { | |||
| singleton_mutex_.lock(); | |||
| if (once_init_) { | |||
| Init(); | |||
| once_init_ = false; | |||
| } | |||
| singleton_mutex_.unlock(); | |||
| } | |||
| return instance_; | |||
| } | |||
| private: | |||
| DISALLOW_COPY_AND_ASSIGN(Singleton); | |||
| static void Init() { | |||
| instance_ = new T(); | |||
| } | |||
| static T* instance_; // Leaky singleton. | |||
| static std::mutex singleton_mutex_; | |||
| static bool once_init_; | |||
| }; | |||
| template <class T> T* Singleton<T>::instance_; | |||
| template <class T> std::mutex Singleton<T>::singleton_mutex_; | |||
| template <class T> bool Singleton<T>::once_init_ = true; | |||
| } // namespace phonenumbers | |||
| } // namespace i18n | |||
| #endif // I18N_PHONENUMBERS_BASE_MEMORY_SINGLETON_STDMUTEX_H_ | |||
| @ -0,0 +1,49 @@ | |||
| // 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_UNSAFE_H_ | |||
| #define I18N_PHONENUMBERS_BASE_MEMORY_SINGLETON_UNSAFE_H_ | |||
| #include "phonenumbers/base/logging.h" | |||
| #include "phonenumbers/base/thread_checker.h" | |||
| namespace i18n { | |||
| namespace phonenumbers { | |||
| // Note that this implementation is not thread-safe. For a thread-safe | |||
| // implementation on non-POSIX platforms, please compile with | |||
| // -DI18N_PHONENUMBERS_USE_BOOST. | |||
| template <class T> | |||
| class Singleton { | |||
| public: | |||
| Singleton() : thread_checker_() {} | |||
| virtual ~Singleton() {} | |||
| static T* GetInstance() { | |||
| static T* instance = NULL; | |||
| if (!instance) { | |||
| instance = new T(); | |||
| } | |||
| DCHECK(instance->thread_checker_.CalledOnValidThread()); | |||
| return instance; | |||
| } | |||
| private: | |||
| const ThreadChecker thread_checker_; | |||
| }; | |||
| } // namespace phonenumbers | |||
| } // namespace i18n | |||
| #endif // I18N_PHONENUMBERS_BASE_MEMORY_SINGLETON_UNSAFE_H_ | |||
| @ -0,0 +1,69 @@ | |||
| // Copyright (C) 2020 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_WIN32_H_ | |||
| #define I18N_PHONENUMBERS_BASE_MEMORY_SINGLETON_WIN32_H_ | |||
| #include <windows.h> | |||
| #include <synchapi.h> | |||
| #include "phonenumbers/base/basictypes.h" | |||
| namespace i18n { | |||
| namespace phonenumbers { | |||
| template <class T> | |||
| class Singleton { | |||
| public: | |||
| Singleton() {} | |||
| virtual ~Singleton() {} | |||
| static T* GetInstance() { | |||
| if (once_init_) { | |||
| EnterCriticalSection(&critical_section_); | |||
| if (once_init_) { | |||
| Init(); | |||
| once_init_ = false; | |||
| } | |||
| LeaveCriticalSection(&critical_section_); | |||
| } | |||
| return instance_; | |||
| } | |||
| private: | |||
| DISALLOW_COPY_AND_ASSIGN(Singleton); | |||
| static void Init() { | |||
| instance_ = new T(); | |||
| } | |||
| static T* instance_; // Leaky singleton. | |||
| static CRITICAL_SECTION critical_section_; | |||
| static bool once_init_; | |||
| }; | |||
| static bool perform_init_crit(CRITICAL_SECTION& cs) | |||
| { | |||
| InitializeCriticalSection(&cs); | |||
| return true; | |||
| } | |||
| template <class T> T* Singleton<T>::instance_; | |||
| template <class T> CRITICAL_SECTION Singleton<T>::critical_section_; | |||
| template <class T> bool Singleton<T>::once_init_=perform_init_crit(Singleton<T>::critical_section_); | |||
| } // namespace phonenumbers | |||
| } // namespace i18n | |||
| #endif // I18N_PHONENUMBERS_BASE_MEMORY_SINGLETON_WIN32_H_ | |||
| @ -0,0 +1,31 @@ | |||
| // Copyright (C) 2020 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. | |||
| // Author: Philippe Liard | |||
| #ifndef I18N_PHONENUMBERS_BASE_SYNCHRONIZATION_LOCK_BOOST_H_ | |||
| #define I18N_PHONENUMBERS_BASE_SYNCHRONIZATION_LOCK_BOOST_H_ | |||
| #include <boost/thread/mutex.hpp> | |||
| namespace i18n { | |||
| namespace phonenumbers { | |||
| typedef boost::mutex Lock; | |||
| typedef boost::mutex::scoped_lock AutoLock; | |||
| } // namespace phonenumbers | |||
| } // namespace i18n | |||
| #endif // I18N_PHONENUMBERS_BASE_SYNCHRONIZATION_LOCK_BOOST_H_ | |||
| @ -0,0 +1,47 @@ | |||
| // Copyright (C) 2020 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. | |||
| // Author: Philippe Liard | |||
| #ifndef I18N_PHONENUMBERS_BASE_SYNCHRONIZATION_LOCK_STDMUTEX_H_ | |||
| #define I18N_PHONENUMBERS_BASE_SYNCHRONIZATION_LOCK_STDMUTEX_H_ | |||
| #include <mutex> | |||
| #include "phonenumbers/base/basictypes.h" | |||
| namespace i18n { | |||
| namespace phonenumbers { | |||
| class Lock { | |||
| public: | |||
| Lock() = default; | |||
| void Acquire() const { | |||
| mutex_.lock(); | |||
| } | |||
| void Release() const { | |||
| mutex_.unlock(); | |||
| } | |||
| private: | |||
| DISALLOW_COPY_AND_ASSIGN(Lock); | |||
| mutable std::mutex mutex_; | |||
| }; | |||
| } // namespace phonenumbers | |||
| } // namespace i18n | |||
| #endif // I18N_PHONENUMBERS_BASE_SYNCHRONIZATION_LOCK_STDMUTEX_H_ | |||
| @ -0,0 +1,51 @@ | |||
| // Copyright (C) 2020 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. | |||
| // Author: Philippe Liard | |||
| #ifndef I18N_PHONENUMBERS_BASE_SYNCHRONIZATION_LOCK_UNSAFE_H_ | |||
| #define I18N_PHONENUMBERS_BASE_SYNCHRONIZATION_LOCK_UNSAFE_H_ | |||
| #include "phonenumbers/base/logging.h" | |||
| #include "phonenumbers/base/thread_checker.h" | |||
| // Dummy lock implementation on non-POSIX platforms. If you are running on a | |||
| // different platform and care about thread-safety, please compile with | |||
| // -DI18N_PHONENUMBERS_USE_BOOST. | |||
| namespace i18n { | |||
| namespace phonenumbers { | |||
| class Lock { | |||
| public: | |||
| Lock() {} | |||
| void Acquire() const { | |||
| DCHECK(thread_checker_.CalledOnValidThread()); | |||
| IGNORE_UNUSED(thread_checker_); | |||
| } | |||
| void Release() const { | |||
| DCHECK(thread_checker_.CalledOnValidThread()); | |||
| IGNORE_UNUSED(thread_checker_); | |||
| } | |||
| private: | |||
| DISALLOW_COPY_AND_ASSIGN(Lock); | |||
| const ThreadChecker thread_checker_; | |||
| }; | |||
| } // namespace phonenumbers | |||
| } // namespace i18n | |||
| #endif // I18N_PHONENUMBERS_BASE_SYNCHRONIZATION_LOCK_UNSAFE_H_ | |||
| @ -0,0 +1,54 @@ | |||
| // Copyright (C) 2020 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. | |||
| // Author: Philippe Liard | |||
| #ifndef I18N_PHONENUMBERS_BASE_SYNCHRONIZATION_LOCK_WINDOWS_H_ | |||
| #define I18N_PHONENUMBERS_BASE_SYNCHRONIZATION_LOCK_WINDOWS_H_ | |||
| #include <windows.h> | |||
| #include <synchapi.h> | |||
| #include "phonenumbers/base/basictypes.h" | |||
| namespace i18n { | |||
| namespace phonenumbers { | |||
| class Lock { | |||
| public: | |||
| Lock() { | |||
| InitializeCriticalSection(&cs_); | |||
| } | |||
| ~Lock() { | |||
| DeleteCriticalSection(&cs_); | |||
| } | |||
| void Acquire() { | |||
| EnterCriticalSection(&cs_); | |||
| } | |||
| void Release() { | |||
| LeaveCriticalSection(&cs_); | |||
| } | |||
| private: | |||
| DISALLOW_COPY_AND_ASSIGN(Lock); | |||
| CRITICAL_SECTION cs_; | |||
| }; | |||
| } // namespace phonenumbers | |||
| } // namespace i18n | |||
| #endif // I18N_PHONENUMBERS_BASE_SYNCHRONIZATION_LOCK_POSIX_H_ | |||