summaryrefslogtreecommitdiffstats
path: root/base/thread_restrictions.h
diff options
context:
space:
mode:
authorevan@chromium.org <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-10-19 20:28:03 +0000
committerevan@chromium.org <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-10-19 20:28:03 +0000
commit5db27db7e09cf3f72c68a8ca668b96177c97101f (patch)
tree3cfcb4c2c3fdf7543564e94603f33d640167d25e /base/thread_restrictions.h
parentfe23e9a748e6a1849f22d3d0add4502a3a41604f (diff)
downloadchromium_src-5db27db7e09cf3f72c68a8ca668b96177c97101f.zip
chromium_src-5db27db7e09cf3f72c68a8ca668b96177c97101f.tar.gz
chromium_src-5db27db7e09cf3f72c68a8ca668b96177c97101f.tar.bz2
base: add a thread-safety assertion checker
ThreadAssertions lets us assert that the current thread is allowed to make blocking calls. See the header for more background. This change implements ThreadAssertions but doesn't turn it on for any of Chrome. BUG=59575 Review URL: http://codereview.chromium.org/3824006 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@63091 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/thread_restrictions.h')
-rw-r--r--base/thread_restrictions.h47
1 files changed, 47 insertions, 0 deletions
diff --git a/base/thread_restrictions.h b/base/thread_restrictions.h
new file mode 100644
index 0000000..4aa2cd6
--- /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);
+
+ // Check whether the current thread is allowed to make IO calls,
+ // and DCHECK if not.
+ static void AssertIOAllowed();
+
+ private:
+ ThreadRestrictions(); // class for namespacing only
+};
+
+// 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_