perkj | 9c16fe8 | 2016-07-12 15:04:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #ifndef WEBRTC_BASE_SEQUENCED_TASK_CHECKER_H_ |
| 12 | #define WEBRTC_BASE_SEQUENCED_TASK_CHECKER_H_ |
| 13 | |
| 14 | // Apart from debug builds, we also enable the sequence checker in |
| 15 | // builds with DCHECK_ALWAYS_ON so that trybots and waterfall bots |
| 16 | // with this define will get the same level of checking as debug bots. |
| 17 | // |
| 18 | // Note that this does not perfectly match situations where RTC_DCHECK is |
| 19 | // enabled. For example a non-official release build may have |
| 20 | // DCHECK_ALWAYS_ON undefined (and therefore SequencedTaskChecker would be |
| 21 | // disabled) but have RTC_DCHECKs enabled at runtime. |
| 22 | #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) |
| 23 | #define ENABLE_SEQUENCED_TASK_CHECKER 1 |
| 24 | #else |
| 25 | #define ENABLE_SEQUENCED_TASK_CHECKER 0 |
| 26 | #endif |
| 27 | |
| 28 | #include "webrtc/base/checks.h" |
| 29 | #include "webrtc/base/constructormagic.h" |
| 30 | #include "webrtc/base/thread_annotations.h" |
| 31 | #include "webrtc/base/sequenced_task_checker_impl.h" |
| 32 | |
| 33 | namespace rtc { |
| 34 | |
| 35 | // Do nothing implementation, for use in release mode. |
| 36 | // |
| 37 | // Note: You should almost always use the SequencedTaskChecker class to get the |
| 38 | // right version for your build configuration. |
| 39 | class SequencedTaskCheckerDoNothing { |
| 40 | public: |
| 41 | bool CalledSequentially() const { return true; } |
| 42 | |
| 43 | void Detach() {} |
| 44 | }; |
| 45 | |
| 46 | // SequencedTaskChecker is a helper class used to help verify that some methods |
| 47 | // of a class are called on the same task queue or thread. A |
| 48 | // SequencedTaskChecker is bound to a a task queue if the object is |
| 49 | // created on a task queue, or a thread otherwise. |
| 50 | // |
| 51 | // |
| 52 | // Example: |
| 53 | // class MyClass { |
| 54 | // public: |
| 55 | // void Foo() { |
| 56 | // RTC_DCHECK(sequence_checker_.CalledSequentially()); |
| 57 | // ... (do stuff) ... |
| 58 | // } |
| 59 | // |
| 60 | // private: |
| 61 | // SequencedTaskChecker sequence_checker_; |
| 62 | // } |
| 63 | // |
| 64 | // In Release mode, CalledOnValidThread will always return true. |
| 65 | #if ENABLE_SEQUENCED_TASK_CHECKER |
| 66 | class LOCKABLE SequencedTaskChecker : public SequencedTaskCheckerImpl {}; |
| 67 | #else |
| 68 | class LOCKABLE SequencedTaskChecker : public SequencedTaskCheckerDoNothing {}; |
| 69 | #endif // ENABLE_SEQUENCED_TASK_CHECKER_H_ |
| 70 | |
| 71 | namespace internal { |
| 72 | class SCOPED_LOCKABLE SequencedTaskCheckerScope { |
| 73 | public: |
| 74 | explicit SequencedTaskCheckerScope(const SequencedTaskChecker* checker) |
| 75 | EXCLUSIVE_LOCK_FUNCTION(checker); |
| 76 | ~SequencedTaskCheckerScope() UNLOCK_FUNCTION(); |
| 77 | }; |
| 78 | |
| 79 | } // namespace internal |
| 80 | |
| 81 | #define RTC_DCHECK_CALLED_SEQUENTIALLY(x) \ |
| 82 | rtc::internal::SequencedTaskCheckerScope checker(x) |
| 83 | |
| 84 | #undef ENABLE_SEQUENCED_TASK_CHECKER |
| 85 | |
| 86 | } // namespace rtc |
| 87 | #endif // WEBRTC_BASE_SEQUENCED_TASK_CHECKER_H_ |