diff options
author | willchan@chromium.org <willchan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-21 17:16:00 +0000 |
---|---|---|
committer | willchan@chromium.org <willchan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-21 17:16:00 +0000 |
commit | 146185df383870a8b4dba30061aa6a1118c58239 (patch) | |
tree | f3d2ce8097c98f88f98a200b7102321c9df9a06c /base | |
parent | cfeb7656524ced75c771017876429f5f1640ae81 (diff) | |
download | chromium_src-146185df383870a8b4dba30061aa6a1118c58239.zip chromium_src-146185df383870a8b4dba30061aa6a1118c58239.tar.gz chromium_src-146185df383870a8b4dba30061aa6a1118c58239.tar.bz2 |
Fix ThreadChecker to use Locks and not use scoped_ptr.
It needs to synchronize its checks, since in order to assert correctly, it needs to make sure the thread id is synchronized on all threads.
It doesn't need scoped_ptr. It was trying to use NULL to catch invalid thread ids. 0 is already assumed to be invalid (see base::Thread's use).
Eliminating scoped_ptr fixes a valgrind/heapcheck issue where they don't follow LazyInstance objects' member pointers. So they think the ThreadChecker's member variable is leaked, even though the global object still has a pointer to it.
Removing the scoped_ptr.h caused a bunch of other lame files to fail to compile. I had to fix those places. #include what you use please :(
TBR=levin (I want to green the memory bots)
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/5180006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@66915 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/platform_thread.h | 18 | ||||
-rw-r--r-- | base/thread.h | 4 | ||||
-rw-r--r-- | base/thread_checker.cc | 13 | ||||
-rw-r--r-- | base/thread_checker.h | 8 |
4 files changed, 26 insertions, 17 deletions
diff --git a/base/platform_thread.h b/base/platform_thread.h index e330422..43bf298 100644 --- a/base/platform_thread.h +++ b/base/platform_thread.h @@ -34,9 +34,19 @@ typedef pid_t PlatformThreadId; #endif #endif +const PlatformThreadId kInvalidThreadId = 0; + // A namespace for low-level thread functions. class PlatformThread { public: + // Implement this interface to run code on a background thread. Your + // ThreadMain method will be called on the newly created thread. + class Delegate { + public: + virtual ~Delegate() {} + virtual void ThreadMain() = 0; + }; + // Gets the current thread id, which may be useful for logging purposes. static PlatformThreadId CurrentId(); @@ -49,14 +59,6 @@ class PlatformThread { // Sets the thread name visible to a debugger. This has no effect otherwise. static void SetName(const char* name); - // Implement this interface to run code on a background thread. Your - // ThreadMain method will be called on the newly created thread. - class Delegate { - public: - virtual ~Delegate() {} - virtual void ThreadMain() = 0; - }; - // Creates a new thread. The |stack_size| parameter can be 0 to indicate // that the default stack size should be used. Upon success, // |*thread_handle| will be assigned a handle to the newly created thread, diff --git a/base/thread.h b/base/thread.h index 572eb8a..fc542f0 100644 --- a/base/thread.h +++ b/base/thread.h @@ -126,8 +126,8 @@ class Thread : PlatformThread::Delegate { PlatformThreadId thread_id() const { return thread_id_; } // Returns true if the thread has been started, and not yet stopped. - // When a thread is running, the thread_id_ is non-zero. - bool IsRunning() const { return thread_id_ != 0; } + // When a thread is running, |thread_id_| is a valid id. + bool IsRunning() const { return thread_id_ != kInvalidThreadId; } protected: // Called just prior to starting the message loop diff --git a/base/thread_checker.cc b/base/thread_checker.cc index 394a90a..52f9847 100644 --- a/base/thread_checker.cc +++ b/base/thread_checker.cc @@ -7,7 +7,7 @@ // This code is only done in debug builds. #ifndef NDEBUG -ThreadChecker::ThreadChecker() { +ThreadChecker::ThreadChecker() : valid_thread_id_(kInvalidThreadId) { EnsureThreadIdAssigned(); } @@ -15,17 +15,20 @@ ThreadChecker::~ThreadChecker() {} bool ThreadChecker::CalledOnValidThread() const { EnsureThreadIdAssigned(); - return *valid_thread_id_ == PlatformThread::CurrentId(); + AutoLock auto_lock(lock_); + return valid_thread_id_ == PlatformThread::CurrentId(); } void ThreadChecker::DetachFromThread() { - valid_thread_id_.reset(); + AutoLock auto_lock(lock_); + valid_thread_id_ = kInvalidThreadId; } void ThreadChecker::EnsureThreadIdAssigned() const { - if (valid_thread_id_.get()) + AutoLock auto_lock(lock_); + if (valid_thread_id_ != kInvalidThreadId) return; - valid_thread_id_.reset(new PlatformThreadId(PlatformThread::CurrentId())); + valid_thread_id_ = PlatformThread::CurrentId(); } #endif // NDEBUG diff --git a/base/thread_checker.h b/base/thread_checker.h index 5d5445e..c09bcbe 100644 --- a/base/thread_checker.h +++ b/base/thread_checker.h @@ -6,8 +6,10 @@ #define BASE_THREAD_CHECKER_H_ #pragma once +#ifndef NDEBUG +#include "base/lock.h" #include "base/platform_thread.h" -#include "base/scoped_ptr.h" +#endif // NDEBUG // Before using this class, please consider using NonThreadSafe as it // makes it much easier to determine the nature of your class. @@ -47,8 +49,10 @@ class ThreadChecker { private: void EnsureThreadIdAssigned() const; + mutable Lock lock_; // This is mutable so that CalledOnValidThread can set it. - mutable scoped_ptr<PlatformThreadId> valid_thread_id_; + // It's guarded by |lock_|. + mutable PlatformThreadId valid_thread_id_; }; #else // Do nothing in release mode. |