CPP: Add base/thread_checker.h.
authorphilip.liard@gmail.com <philip.liard@gmail.com@ee073f10-1060-11df-b6a4-87a95322a99c>
Thu, 2 May 2013 13:26:39 +0000 (13:26 +0000)
committerphilip.liard@gmail.com <philip.liard@gmail.com@ee073f10-1060-11df-b6a4-87a95322a99c>
Thu, 2 May 2013 13:26:39 +0000 (13:26 +0000)
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

cpp/src/phonenumbers/base/memory/singleton.h
cpp/src/phonenumbers/base/synchronization/lock.h
cpp/src/phonenumbers/base/thread_checker.h [moved from cpp/src/phonenumbers/base/thread_safety_check.h with 66% similarity]

index 43c5a1a..38b4bfc 100644 (file)
@@ -50,7 +50,8 @@ template <class T> boost::once_flag Singleton<T>::flag = BOOST_ONCE_INIT;
 
 #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 {
@@ -60,6 +61,8 @@ namespace phonenumbers {
 template <class T>
 class Singleton {
  public:
+  Singleton() : thread_checker_() {}
+
   virtual ~Singleton() {}
 
   static T* GetInstance() {
@@ -67,8 +70,12 @@ class Singleton {
     if (!instance) {
       instance = new T();
     }
+    DCHECK(instance->thread_checker_.CalledOnValidThread());
     return instance;
   }
+
+ private:
+  const ThreadChecker thread_checker_;
 };
 
 #endif  // !I18N_PHONENUMBERS_USE_BOOST
index c68fefd..7ef136c 100644 (file)
@@ -30,17 +30,35 @@ typedef boost::mutex::scoped_lock AutoLock;
 }  // 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
similarity index 66%
rename from cpp/src/phonenumbers/base/thread_safety_check.h
rename to cpp/src/phonenumbers/base/thread_checker.h
index b11bdf5..38ef54c 100644 (file)
 
 #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_