This lets make sure that the library is only called in Chromium from the UI
thread.
BUG=http://crbug.com/236272
R=jia.shao.peng@gmail.com
Review URL: https://codereview.appspot.com/9048043
git-svn-id: http://libphonenumber.googlecode.com/svn/trunk@570
ee073f10-1060-11df-b6a4-
87a95322a99c
#else // !I18N_PHONENUMBERS_USE_BOOST
-#include "phonenumbers/base/thread_safety_check.h"
+#include "phonenumbers/base/logging.h"
+#include "phonenumbers/base/thread_checker.h"
namespace i18n {
namespace phonenumbers {
template <class T>
class Singleton {
public:
+ Singleton() : thread_checker_() {}
+
virtual ~Singleton() {}
static T* GetInstance() {
if (!instance) {
instance = new T();
}
+ DCHECK(instance->thread_checker_.CalledOnValidThread());
return instance;
}
+
+ private:
+ const ThreadChecker thread_checker_;
};
#endif // !I18N_PHONENUMBERS_USE_BOOST
} // namespace i18n
#else // I18N_PHONENUMBERS_USE_BOOST
-#include "phonenumbers/base/thread_safety_check.h"
+
+#include "phonenumbers/base/logging.h"
+#include "phonenumbers/base/thread_checker.h"
namespace i18n {
namespace phonenumbers {
// Dummy lock implementation. If you care about thread-safety, please compile
// with -DI18N_PHONENUMBERS_USE_BOOST.
-struct Lock {};
+class Lock {
+ public:
+ Lock() : thread_checker_() {}
+
+ void Acquire() const {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ }
+
+ // No need for Release() since Acquire() is a no-op and Release() is not used
+ // in the codebase.
+
+ private:
+ const ThreadChecker thread_checker_;
+};
-struct AutoLock {
- AutoLock(Lock) {}
+class AutoLock {
+ public:
+ AutoLock(Lock& lock) {
+ lock.Acquire();
+ }
};
} // namespace phonenumbers
#endif
+#if !defined(NDEBUG) && !defined(I18N_PHONENUMBERS_USE_BOOST) && \
+ (defined(__linux__) || defined(__apple__))
+
+#include <pthread.h>
+
+namespace i18n {
+namespace phonenumbers {
+
+class ThreadChecker {
+ public:
+ ThreadChecker() : thread_id_(pthread_self()) {}
+
+ bool CalledOnValidThread() const {
+ return thread_id_ == pthread_self();
+ }
+
+ private:
+ const pthread_t thread_id_;
+};
+
+#else
+
+namespace i18n {
+namespace phonenumbers {
+
+class ThreadChecker {
+ public:
+ bool CalledOnValidThread() const {
+ return true;
+ }
+};
+
+#endif
+
+} // namespace phonenumbers
+} // namespace i18n
+
#endif // I18N_PHONENUMBERS_BASE_THREAD_SAFETY_CHECK_H_