Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(550)

Unified Diff: base/thread_restrictions.h

Issue 3824006: base: add a thread-safety assertion checker (Closed)
Patch Set: fix function name Created 10 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/thread_local.h ('k') | base/thread_restrictions.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/thread_restrictions.h
diff --git a/base/thread_restrictions.h b/base/thread_restrictions.h
new file mode 100644
index 0000000000000000000000000000000000000000..4aa2cd69d2827dc5ba4d919f881eed605681646c
--- /dev/null
+++ b/base/thread_restrictions.h
@@ -0,0 +1,47 @@
+// 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.
+
+#ifndef BASE_THREAD_RESTRICTIONS_H_
+#define BASE_THREAD_RESTRICTIONS_H_
+
+namespace base {
+
+// ThreadRestrictions helps protect threads that should not block from
+// making blocking calls. It works like this:
+//
+// 1) If a thread should not be allowed to make IO calls, mark it:
+// base::ThreadRestrictions::SetIOAllowed(false);
+// By default, threads *are* allowed to make IO calls.
+// In Chrome browser code, IO calls should be proxied to the File thread.
+//
+// 2) If a function makes a call that will go out to disk, check whether the
+// current thread is allowed:
+// base::ThreadRestrictions::AssertIOAllowed();
+//
+// ThreadRestrictions does nothing in release builds; it is debug-only.
+//
+class ThreadRestrictions {
+ public:
+ // Set whether the current thread to make IO calls.
+ // Threads start out in the *allowed* state.
+ static void SetIOAllowed(bool allowed);
darin (slow to review) 2010/10/22 06:30:44 I think it is confusing to use the term "IO" here
+
+ // Check whether the current thread is allowed to make IO calls,
+ // and DCHECK if not.
+ static void AssertIOAllowed();
+
+ private:
+ ThreadRestrictions(); // class for namespacing only
darin (slow to review) 2010/10/22 06:30:44 DISALLOW_IMPLICIT_CONSTRUCTORS?
+};
+
+// In Release builds, inline the empty definitions of these functions so
+// that they can be compiled out.
+#ifdef NDEBUG
+void ThreadRestrictions::SetIOAllowed(bool allowed) {}
+void ThreadRestrictions::AssertIOAllowed() {}
+#endif
+
+} // namespace base
+
+#endif // BASE_THREAD_RESTRICTIONS_H_
« no previous file with comments | « base/thread_local.h ('k') | base/thread_restrictions.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698