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

Unified Diff: base/thread_restrictions.h

Issue 3872002: Thread IO safety: file_util annotated, IO thread blocked from doing IO (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix 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/shared_memory_posix.cc ('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
index 4aa2cd69d2827dc5ba4d919f881eed605681646c..b942d3920d2ef82366906bf7bd2ca99d2828408b 100644
--- a/base/thread_restrictions.h
+++ b/base/thread_restrictions.h
@@ -21,14 +21,35 @@ namespace base {
//
// ThreadRestrictions does nothing in release builds; it is debug-only.
//
+// Style tip: where should you put AssertIOAllowed checks? It's best
+// if you put them as close to the disk access as possible, at the
+// lowest level. This rule is simple to follow and helps catch all
+// callers. For example, if your function GoDoSomeBlockingDiskCall()
+// only calls other functions in Chrome and not fopen(), you should go
+// add the AssertIOAllowed checks in the helper functions.
+
class ThreadRestrictions {
public:
+ // Constructing a ScopedAllowIO temporarily allows IO for the current
+ // thread. Doing this is almost certainly always incorrect. This object
+ // is available to temporarily work around bugs.
+ class ScopedAllowIO {
+ public:
+ ScopedAllowIO() { previous_value_ = SetIOAllowed(true); }
+ ~ScopedAllowIO() { SetIOAllowed(previous_value_); }
+ private:
+ // Whether IO is allowed when the ScopedAllowIO was constructed.
+ bool previous_value_;
+ };
+
// Set whether the current thread to make IO calls.
// Threads start out in the *allowed* state.
- static void SetIOAllowed(bool allowed);
+ // Returns the previous value.
+ static bool SetIOAllowed(bool allowed);
// Check whether the current thread is allowed to make IO calls,
- // and DCHECK if not.
+ // and DCHECK if not. See the block comment above the class for
+ // a discussion of where to add these checks.
static void AssertIOAllowed();
private:
@@ -38,7 +59,7 @@ class ThreadRestrictions {
// In Release builds, inline the empty definitions of these functions so
// that they can be compiled out.
#ifdef NDEBUG
-void ThreadRestrictions::SetIOAllowed(bool allowed) {}
+bool ThreadRestrictions::SetIOAllowed(bool allowed) { return true; }
void ThreadRestrictions::AssertIOAllowed() {}
#endif
« no previous file with comments | « base/shared_memory_posix.cc ('k') | base/thread_restrictions.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698