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

Side by Side Diff: base/threading/non_thread_safe.h

Issue 6599004: Modify ThreadChecker and NonThreadSafe so that their functionality is (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase to current lkgr Created 9 years, 9 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | base/threading/non_thread_safe.cc » ('j') | net/disk_cache/block_files.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef BASE_THREADING_NON_THREAD_SAFE_H_ 5 #ifndef BASE_THREADING_NON_THREAD_SAFE_H_
6 #define BASE_THREADING_NON_THREAD_SAFE_H_ 6 #define BASE_THREADING_NON_THREAD_SAFE_H_
7 #pragma once 7 #pragma once
8 8
9 #include "base/threading/thread_checker.h" 9 #include "base/threading/thread_checker.h"
10 10
11 namespace base { 11 namespace base {
12 12
13 // A helper class used to help verify that methods of a class are 13 class NonThreadSafeImpl;
14 // called from the same thread. One can inherit from this class and use 14 class NonThreadSafeDoNothing;
15 // CalledOnValidThread() to verify. 15
16 // NonThreadSafe is a helper class used to help verify that methods of a
17 // class are called from the same thread. One can inherit from this class
18 // and use CalledOnValidThread() to verify.
16 // 19 //
17 // This is intended to be used with classes that appear to be thread safe, but 20 // This is intended to be used with classes that appear to be thread safe, but
18 // aren't. For example, a service or a singleton like the preferences system. 21 // aren't. For example, a service or a singleton like the preferences system.
19 // 22 //
20 // Example: 23 // Example:
21 // class MyClass : public base::NonThreadSafe { 24 // class MyClass : public base::NonThreadSafe {
22 // public: 25 // public:
23 // void Foo() { 26 // void Foo() {
24 // DCHECK(CalledOnValidThread()); 27 // DCHECK(CalledOnValidThread());
25 // ... (do stuff) ... 28 // ... (do stuff) ...
26 // } 29 // }
27 // } 30 // }
28 // 31 //
29 // In Release mode, CalledOnValidThread will always return true. 32 // In Release mode, CalledOnValidThread will always return true.
30 // 33 //
31 #ifndef NDEBUG 34 #ifndef NDEBUG
32 class NonThreadSafe { 35 typedef NonThreadSafeImpl NonThreadSafe;
36 #else
37 typedef NonThreadSafeDoNothing NonThreadSafe;
38 #endif // NDEBUG
39
40 // Full implementation of NonThreadSafe, for debug mode or for occasional
41 // temporary use in release mode e.g. when you need to CHECK on a thread
42 // bug that only occurs in the wild.
43 //
44 // Note: You should almost always use the NonThreadSafe typedef to get
45 // the right version of the class.
46 class NonThreadSafeImpl {
33 public: 47 public:
34 ~NonThreadSafe(); 48 ~NonThreadSafeImpl();
35 49
36 bool CalledOnValidThread() const; 50 bool CalledOnValidThread() const;
37 51
38 protected: 52 protected:
39 // Changes the thread that is checked for in CalledOnValidThread. The next 53 // Changes the thread that is checked for in CalledOnValidThread. The next
40 // call to CalledOnValidThread will attach this class to a new thread. It is 54 // call to CalledOnValidThread will attach this class to a new thread. It is
41 // up to the NonThreadSafe derived class to decide to expose this or not. 55 // up to the NonThreadSafe derived class to decide to expose this or not.
42 // This may be useful when an object may be created on one thread and then 56 // This may be useful when an object may be created on one thread and then
43 // used exclusively on another thread. 57 // used exclusively on another thread.
44 void DetachFromThread(); 58 void DetachFromThread();
45 59
46 private: 60 private:
47 ThreadChecker thread_checker_; 61 ThreadCheckerImpl thread_checker_;
48 }; 62 };
49 #else 63
50 // Do nothing in release mode. 64 // Do nothing implementation of NonThreadSafe, for release mode.
51 class NonThreadSafe { 65 //
66 // Note: You should almost always use the NonThreadSafe typedef to get
67 // the right version of the class.
68 class NonThreadSafeDoNothing {
52 public: 69 public:
53 bool CalledOnValidThread() const { 70 bool CalledOnValidThread() const {
54 return true; 71 return true;
55 } 72 }
56 73
57 protected: 74 protected:
58 void DetachFromThread() {} 75 void DetachFromThread() {}
59 }; 76 };
60 #endif // NDEBUG
61 77
62 } // namespace base 78 } // namespace base
63 79
64 #endif // BASE_NON_THREAD_SAFE_H_ 80 #endif // BASE_NON_THREAD_SAFE_H_
OLDNEW
« no previous file with comments | « no previous file | base/threading/non_thread_safe.cc » ('j') | net/disk_cache/block_files.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698