OLD | NEW |
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_NON_THREAD_SAFE_H_ | 5 #ifndef BASE_NON_THREAD_SAFE_H_ |
6 #define BASE_NON_THREAD_SAFE_H_ | 6 #define BASE_NON_THREAD_SAFE_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/platform_thread.h" | 9 #include "base/threading/thread_checker.h" |
10 #include "base/thread_checker.h" | |
11 | 10 |
12 // A helper class used to help verify that methods of a class are | 11 // A helper class used to help verify that methods of a class are |
13 // called from the same thread. One can inherit from this class and use | 12 // called from the same thread. One can inherit from this class and use |
14 // CalledOnValidThread() to verify. | 13 // CalledOnValidThread() to verify. |
15 // | 14 // |
16 // This is intended to be used with classes that appear to be thread safe, but | 15 // This is intended to be used with classes that appear to be thread safe, but |
17 // aren't. For example, a service or a singleton like the preferences system. | 16 // aren't. For example, a service or a singleton like the preferences system. |
18 // | 17 // |
19 // Example: | 18 // Example: |
20 // class MyClass : public NonThreadSafe { | 19 // class MyClass : public NonThreadSafe { |
(...skipping 15 matching lines...) Expand all Loading... |
36 | 35 |
37 protected: | 36 protected: |
38 // Changes the thread that is checked for in CalledOnValidThread. The next | 37 // Changes the thread that is checked for in CalledOnValidThread. The next |
39 // call to CalledOnValidThread will attach this class to a new thread. It is | 38 // call to CalledOnValidThread will attach this class to a new thread. It is |
40 // up to the NonThreadSafe derived class to decide to expose this or not. | 39 // up to the NonThreadSafe derived class to decide to expose this or not. |
41 // This may be useful when an object may be created on one thread and then | 40 // This may be useful when an object may be created on one thread and then |
42 // used exclusively on another thread. | 41 // used exclusively on another thread. |
43 void DetachFromThread(); | 42 void DetachFromThread(); |
44 | 43 |
45 private: | 44 private: |
46 ThreadChecker thread_checker_; | 45 base::ThreadChecker thread_checker_; |
47 }; | 46 }; |
48 #else | 47 #else |
49 // Do nothing in release mode. | 48 // Do nothing in release mode. |
50 class NonThreadSafe { | 49 class NonThreadSafe { |
51 public: | 50 public: |
52 bool CalledOnValidThread() const { | 51 bool CalledOnValidThread() const { |
53 return true; | 52 return true; |
54 } | 53 } |
55 | 54 |
56 protected: | 55 protected: |
57 void DetachFromThread() {} | 56 void DetachFromThread() {} |
58 }; | 57 }; |
59 #endif // NDEBUG | 58 #endif // NDEBUG |
60 | 59 |
61 #endif // BASE_NON_THREAD_SAFE_H_ | 60 #endif // BASE_NON_THREAD_SAFE_H_ |
OLD | NEW |