summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorphilip.liard@gmail.com <philip.liard@gmail.com@ee073f10-1060-11df-b6a4-87a95322a99c>2013-05-02 13:26:39 +0000
committerphilip.liard@gmail.com <philip.liard@gmail.com@ee073f10-1060-11df-b6a4-87a95322a99c>2013-05-02 13:26:39 +0000
commit96b80a3b937dfdd22d738ab9a71bfb10d5c5ad60 (patch)
tree410cf7e4af5d6ffb1e626d75e69171c693391a34
parent94f3dc686d5f369ee596aa3e22344683f6773494 (diff)
downloadphonenumbers-96b80a3b937dfdd22d738ab9a71bfb10d5c5ad60.tar.gz
CPP: Add base/thread_checker.h.
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/cpp/src/phonenumbers@570 ee073f10-1060-11df-b6a4-87a95322a99c
-rw-r--r--base/memory/singleton.h9
-rw-r--r--base/synchronization/lock.h26
-rw-r--r--base/thread_checker.h (renamed from base/thread_safety_check.h)37
3 files changed, 67 insertions, 5 deletions
diff --git a/base/memory/singleton.h b/base/memory/singleton.h
index 43c5a1a..38b4bfc 100644
--- a/base/memory/singleton.h
+++ b/base/memory/singleton.h
@@ -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
diff --git a/base/synchronization/lock.h b/base/synchronization/lock.h
index c68fefd..7ef136c 100644
--- a/base/synchronization/lock.h
+++ b/base/synchronization/lock.h
@@ -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
diff --git a/base/thread_safety_check.h b/base/thread_checker.h
index b11bdf5..38ef54c 100644
--- a/base/thread_safety_check.h
+++ b/base/thread_checker.h
@@ -28,4 +28,41 @@
#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_