This source file includes following definitions.
- DoStuff
- DetachFromThread
- non_thread_safe_class_
- Run
- non_thread_safe_class_
- Run
- TEST
- TEST
- MethodOnDifferentThreadImpl
- TEST
- TEST
- DestructorOnDifferentThreadImpl
- TEST
- TEST
#include "base/basictypes.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/threading/non_thread_safe.h"
#include "base/threading/simple_thread.h"
#include "testing/gtest/include/gtest/gtest.h"
#if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON))
#define ENABLE_NON_THREAD_SAFE 1
#else
#define ENABLE_NON_THREAD_SAFE 0
#endif
namespace base {
namespace {
class NonThreadSafeClass : public NonThreadSafe {
public:
NonThreadSafeClass() {}
void DoStuff() {
DCHECK(CalledOnValidThread());
}
void DetachFromThread() {
NonThreadSafe::DetachFromThread();
}
static void MethodOnDifferentThreadImpl();
static void DestructorOnDifferentThreadImpl();
private:
DISALLOW_COPY_AND_ASSIGN(NonThreadSafeClass);
};
class CallDoStuffOnThread : public SimpleThread {
public:
explicit CallDoStuffOnThread(NonThreadSafeClass* non_thread_safe_class)
: SimpleThread("call_do_stuff_on_thread"),
non_thread_safe_class_(non_thread_safe_class) {
}
virtual void Run() OVERRIDE {
non_thread_safe_class_->DoStuff();
}
private:
NonThreadSafeClass* non_thread_safe_class_;
DISALLOW_COPY_AND_ASSIGN(CallDoStuffOnThread);
};
class DeleteNonThreadSafeClassOnThread : public SimpleThread {
public:
explicit DeleteNonThreadSafeClassOnThread(
NonThreadSafeClass* non_thread_safe_class)
: SimpleThread("delete_non_thread_safe_class_on_thread"),
non_thread_safe_class_(non_thread_safe_class) {
}
virtual void Run() OVERRIDE {
non_thread_safe_class_.reset();
}
private:
scoped_ptr<NonThreadSafeClass> non_thread_safe_class_;
DISALLOW_COPY_AND_ASSIGN(DeleteNonThreadSafeClassOnThread);
};
}
TEST(NonThreadSafeTest, CallsAllowedOnSameThread) {
scoped_ptr<NonThreadSafeClass> non_thread_safe_class(
new NonThreadSafeClass);
non_thread_safe_class->DoStuff();
non_thread_safe_class.reset();
}
TEST(NonThreadSafeTest, DetachThenDestructOnDifferentThread) {
scoped_ptr<NonThreadSafeClass> non_thread_safe_class(
new NonThreadSafeClass);
non_thread_safe_class->DetachFromThread();
DeleteNonThreadSafeClassOnThread delete_on_thread(
non_thread_safe_class.release());
delete_on_thread.Start();
delete_on_thread.Join();
}
#if GTEST_HAS_DEATH_TEST || !ENABLE_NON_THREAD_SAFE
void NonThreadSafeClass::MethodOnDifferentThreadImpl() {
scoped_ptr<NonThreadSafeClass> non_thread_safe_class(
new NonThreadSafeClass);
CallDoStuffOnThread call_on_thread(non_thread_safe_class.get());
call_on_thread.Start();
call_on_thread.Join();
}
#if ENABLE_NON_THREAD_SAFE
TEST(NonThreadSafeDeathTest, MethodNotAllowedOnDifferentThreadInDebug) {
ASSERT_DEATH({
NonThreadSafeClass::MethodOnDifferentThreadImpl();
}, "");
}
#else
TEST(NonThreadSafeTest, MethodAllowedOnDifferentThreadInRelease) {
NonThreadSafeClass::MethodOnDifferentThreadImpl();
}
#endif
void NonThreadSafeClass::DestructorOnDifferentThreadImpl() {
scoped_ptr<NonThreadSafeClass> non_thread_safe_class(
new NonThreadSafeClass);
DeleteNonThreadSafeClassOnThread delete_on_thread(
non_thread_safe_class.release());
delete_on_thread.Start();
delete_on_thread.Join();
}
#if ENABLE_NON_THREAD_SAFE
TEST(NonThreadSafeDeathTest, DestructorNotAllowedOnDifferentThreadInDebug) {
ASSERT_DEATH({
NonThreadSafeClass::DestructorOnDifferentThreadImpl();
}, "");
}
#else
TEST(NonThreadSafeTest, DestructorAllowedOnDifferentThreadInRelease) {
NonThreadSafeClass::DestructorOnDifferentThreadImpl();
}
#endif
#endif
#undef ENABLE_NON_THREAD_SAFE
}