summaryrefslogtreecommitdiffstats
path: root/base/threading/thread_checker.cc
diff options
context:
space:
mode:
Diffstat (limited to 'base/threading/thread_checker.cc')
-rw-r--r--base/threading/thread_checker.cc38
1 files changed, 38 insertions, 0 deletions
diff --git a/base/threading/thread_checker.cc b/base/threading/thread_checker.cc
new file mode 100644
index 0000000..28ba400
--- /dev/null
+++ b/base/threading/thread_checker.cc
@@ -0,0 +1,38 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/threading/thread_checker.h"
+
+// This code is only done in debug builds.
+#ifndef NDEBUG
+
+namespace base {
+
+ThreadChecker::ThreadChecker() : valid_thread_id_(kInvalidThreadId) {
+ EnsureThreadIdAssigned();
+}
+
+ThreadChecker::~ThreadChecker() {}
+
+bool ThreadChecker::CalledOnValidThread() const {
+ EnsureThreadIdAssigned();
+ AutoLock auto_lock(lock_);
+ return valid_thread_id_ == PlatformThread::CurrentId();
+}
+
+void ThreadChecker::DetachFromThread() {
+ AutoLock auto_lock(lock_);
+ valid_thread_id_ = kInvalidThreadId;
+}
+
+void ThreadChecker::EnsureThreadIdAssigned() const {
+ AutoLock auto_lock(lock_);
+ if (valid_thread_id_ != kInvalidThreadId)
+ return;
+ valid_thread_id_ = PlatformThread::CurrentId();
+}
+
+} // namespace base
+
+#endif // NDEBUG