| 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_THREADING_THREAD_CHECKER_H_ | 5 #ifndef BASE_THREADING_THREAD_CHECKER_H_ |
| 6 #define BASE_THREADING_THREAD_CHECKER_H_ | 6 #define BASE_THREADING_THREAD_CHECKER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #ifndef NDEBUG | 9 #ifndef NDEBUG |
| 10 #include "base/lock.h" | 10 #include "base/synchronization/lock.h" |
| 11 #include "base/threading/platform_thread.h" | 11 #include "base/threading/platform_thread.h" |
| 12 #endif // NDEBUG | 12 #endif // NDEBUG |
| 13 | 13 |
| 14 namespace base { | 14 namespace base { |
| 15 | 15 |
| 16 // Before using this class, please consider using NonThreadSafe as it | 16 // Before using this class, please consider using NonThreadSafe as it |
| 17 // makes it much easier to determine the nature of your class. | 17 // makes it much easier to determine the nature of your class. |
| 18 // | 18 // |
| 19 // A helper class used to help verify that some methods of a class are | 19 // A helper class used to help verify that some methods of a class are |
| 20 // called from the same thread. One can inherit from this class and use | 20 // called from the same thread. One can inherit from this class and use |
| (...skipping 23 matching lines...) Expand all Loading... |
| 44 bool CalledOnValidThread() const; | 44 bool CalledOnValidThread() const; |
| 45 | 45 |
| 46 // Changes the thread that is checked for in CalledOnValidThread. This may | 46 // Changes the thread that is checked for in CalledOnValidThread. This may |
| 47 // be useful when an object may be created on one thread and then used | 47 // be useful when an object may be created on one thread and then used |
| 48 // exclusively on another thread. | 48 // exclusively on another thread. |
| 49 void DetachFromThread(); | 49 void DetachFromThread(); |
| 50 | 50 |
| 51 private: | 51 private: |
| 52 void EnsureThreadIdAssigned() const; | 52 void EnsureThreadIdAssigned() const; |
| 53 | 53 |
| 54 mutable Lock lock_; | 54 mutable base::Lock lock_; |
| 55 // This is mutable so that CalledOnValidThread can set it. | 55 // This is mutable so that CalledOnValidThread can set it. |
| 56 // It's guarded by |lock_|. | 56 // It's guarded by |lock_|. |
| 57 mutable PlatformThreadId valid_thread_id_; | 57 mutable PlatformThreadId valid_thread_id_; |
| 58 }; | 58 }; |
| 59 #else | 59 #else |
| 60 // Do nothing in release mode. | 60 // Do nothing in release mode. |
| 61 class ThreadChecker { | 61 class ThreadChecker { |
| 62 public: | 62 public: |
| 63 bool CalledOnValidThread() const { | 63 bool CalledOnValidThread() const { |
| 64 return true; | 64 return true; |
| 65 } | 65 } |
| 66 | 66 |
| 67 void DetachFromThread() {} | 67 void DetachFromThread() {} |
| 68 }; | 68 }; |
| 69 #endif // NDEBUG | 69 #endif // NDEBUG |
| 70 | 70 |
| 71 } // namespace base | 71 } // namespace base |
| 72 | 72 |
| 73 #endif // BASE_THREADING_THREAD_CHECKER_H_ | 73 #endif // BASE_THREADING_THREAD_CHECKER_H_ |
| OLD | NEW |