Browse Source

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
pull/567/head
Philippe Liard 13 years ago
committed by Mihaela Rosca
parent
commit
d60d343ae9
3 changed files with 67 additions and 5 deletions
  1. +8
    -1
      cpp/src/phonenumbers/base/memory/singleton.h
  2. +22
    -4
      cpp/src/phonenumbers/base/synchronization/lock.h
  3. +37
    -0
      cpp/src/phonenumbers/base/thread_checker.h

+ 8
- 1
cpp/src/phonenumbers/base/memory/singleton.h View File

@ -50,7 +50,8 @@ template <class T> boost::once_flag Singleton<T>::flag = BOOST_ONCE_INIT;
#else // !I18N_PHONENUMBERS_USE_BOOST #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 i18n {
namespace phonenumbers { namespace phonenumbers {
@ -60,6 +61,8 @@ namespace phonenumbers {
template <class T> template <class T>
class Singleton { class Singleton {
public: public:
Singleton() : thread_checker_() {}
virtual ~Singleton() {} virtual ~Singleton() {}
static T* GetInstance() { static T* GetInstance() {
@ -67,8 +70,12 @@ class Singleton {
if (!instance) { if (!instance) {
instance = new T(); instance = new T();
} }
DCHECK(instance->thread_checker_.CalledOnValidThread());
return instance; return instance;
} }
private:
const ThreadChecker thread_checker_;
}; };
#endif // !I18N_PHONENUMBERS_USE_BOOST #endif // !I18N_PHONENUMBERS_USE_BOOST


+ 22
- 4
cpp/src/phonenumbers/base/synchronization/lock.h View File

@ -30,17 +30,35 @@ typedef boost::mutex::scoped_lock AutoLock;
} // namespace i18n } // namespace i18n
#else // I18N_PHONENUMBERS_USE_BOOST #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 i18n {
namespace phonenumbers { namespace phonenumbers {
// Dummy lock implementation. If you care about thread-safety, please compile // Dummy lock implementation. If you care about thread-safety, please compile
// with -DI18N_PHONENUMBERS_USE_BOOST. // 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 } // namespace phonenumbers


cpp/src/phonenumbers/base/thread_safety_check.h → cpp/src/phonenumbers/base/thread_checker.h View File


Loading…
Cancel
Save