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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef RTC_BASE_SEQUENCED_TASK_CHECKER_H_ |
| 12 | #define RTC_BASE_SEQUENCED_TASK_CHECKER_H_ |
perkj | 9c16fe8 | 2016-07-12 15:04:07 -0700 | [diff] [blame] | 13 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 14 | // Apart from debug builds, we also enable the sequence checker in |
| 15 | // builds with RTC_DCHECK_IS_ON so that trybots and waterfall bots |
| 16 | // with this define will get the same level of checking as debug bots. |
danilchap | 42a70e3 | 2017-09-06 01:38:35 -0700 | [diff] [blame] | 17 | #define ENABLE_SEQUENCED_TASK_CHECKER RTC_DCHECK_IS_ON |
perkj | 9c16fe8 | 2016-07-12 15:04:07 -0700 | [diff] [blame] | 18 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 20 | #include "rtc_base/constructor_magic.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "rtc_base/sequenced_task_checker_impl.h" |
| 22 | #include "rtc_base/thread_annotations.h" |
perkj | 9c16fe8 | 2016-07-12 15:04:07 -0700 | [diff] [blame] | 23 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 24 | namespace rtc { |
Tommi | 10b40ce | 2018-02-19 13:34:00 +0100 | [diff] [blame] | 25 | namespace internal { |
| 26 | // Forward declaration of the internal implementation of RTC_GUARDED_BY(). |
| 27 | // SequencedTaskChecker grants this class access to call its IsCurrent() method. |
| 28 | // See thread_checker.h for more details. |
| 29 | class AnnounceOnThread; |
| 30 | } // namespace internal |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 31 | |
| 32 | // Do nothing implementation, for use in release mode. |
| 33 | // |
| 34 | // Note: You should almost always use the SequencedTaskChecker class to get the |
| 35 | // right version for your build configuration. |
| 36 | class SequencedTaskCheckerDoNothing { |
| 37 | public: |
| 38 | bool CalledSequentially() const { return true; } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 39 | void Detach() {} |
Tommi | 10b40ce | 2018-02-19 13:34:00 +0100 | [diff] [blame] | 40 | |
| 41 | private: |
| 42 | friend class internal::AnnounceOnThread; |
| 43 | bool IsCurrent() const { return CalledSequentially(); } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 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. |
danilchap | 42a70e3 | 2017-09-06 01:38:35 -0700 | [diff] [blame] | 65 | #if ENABLE_SEQUENCED_TASK_CHECKER |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 66 | class RTC_LOCKABLE SequencedTaskChecker : public SequencedTaskCheckerImpl {}; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 67 | #else |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 68 | class RTC_LOCKABLE SequencedTaskChecker : public SequencedTaskCheckerDoNothing { |
| 69 | }; |
danilchap | 42a70e3 | 2017-09-06 01:38:35 -0700 | [diff] [blame] | 70 | #endif // ENABLE_SEQUENCED_TASK_CHECKER_H_ |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 71 | |
| 72 | namespace internal { |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 73 | class RTC_SCOPED_LOCKABLE SequencedTaskCheckerScope { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 74 | public: |
| 75 | explicit SequencedTaskCheckerScope(const SequencedTaskChecker* checker) |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 76 | RTC_EXCLUSIVE_LOCK_FUNCTION(checker); |
| 77 | ~SequencedTaskCheckerScope() RTC_UNLOCK_FUNCTION(); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | } // namespace internal |
| 81 | |
| 82 | #define RTC_DCHECK_CALLED_SEQUENTIALLY(x) \ |
| 83 | rtc::internal::SequencedTaskCheckerScope checker(x) |
| 84 | |
danilchap | 42a70e3 | 2017-09-06 01:38:35 -0700 | [diff] [blame] | 85 | #undef ENABLE_SEQUENCED_TASK_CHECKER |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 86 | |
| 87 | } // namespace rtc |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 88 | #endif // RTC_BASE_SEQUENCED_TASK_CHECKER_H_ |