blob: cc369be68ea62854ed7737e302131cfb729a1c79 [file] [log] [blame]
perkj9c16fe82016-07-12 15:04:07 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_SEQUENCED_TASK_CHECKER_H_
12#define RTC_BASE_SEQUENCED_TASK_CHECKER_H_
perkj9c16fe82016-07-12 15:04:07 -070013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014// 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.
danilchap42a70e32017-09-06 01:38:35 -070017#define ENABLE_SEQUENCED_TASK_CHECKER RTC_DCHECK_IS_ON
perkj9c16fe82016-07-12 15:04:07 -070018
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/checks.h"
20#include "rtc_base/constructormagic.h"
21#include "rtc_base/sequenced_task_checker_impl.h"
22#include "rtc_base/thread_annotations.h"
perkj9c16fe82016-07-12 15:04:07 -070023
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020024namespace rtc {
25
26// Do nothing implementation, for use in release mode.
27//
28// Note: You should almost always use the SequencedTaskChecker class to get the
29// right version for your build configuration.
30class SequencedTaskCheckerDoNothing {
31 public:
32 bool CalledSequentially() const { return true; }
33
34 void Detach() {}
35};
36
37// SequencedTaskChecker is a helper class used to help verify that some methods
38// of a class are called on the same task queue or thread. A
39// SequencedTaskChecker is bound to a a task queue if the object is
40// created on a task queue, or a thread otherwise.
41//
42//
43// Example:
44// class MyClass {
45// public:
46// void Foo() {
47// RTC_DCHECK(sequence_checker_.CalledSequentially());
48// ... (do stuff) ...
49// }
50//
51// private:
52// SequencedTaskChecker sequence_checker_;
53// }
54//
55// In Release mode, CalledOnValidThread will always return true.
danilchap42a70e32017-09-06 01:38:35 -070056#if ENABLE_SEQUENCED_TASK_CHECKER
danilchap3c6abd22017-09-06 05:46:29 -070057class RTC_LOCKABLE SequencedTaskChecker : public SequencedTaskCheckerImpl {};
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020058#else
danilchap3c6abd22017-09-06 05:46:29 -070059class RTC_LOCKABLE SequencedTaskChecker : public SequencedTaskCheckerDoNothing {
60};
danilchap42a70e32017-09-06 01:38:35 -070061#endif // ENABLE_SEQUENCED_TASK_CHECKER_H_
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020062
63namespace internal {
danilchap3c6abd22017-09-06 05:46:29 -070064class RTC_SCOPED_LOCKABLE SequencedTaskCheckerScope {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020065 public:
66 explicit SequencedTaskCheckerScope(const SequencedTaskChecker* checker)
danilchap3c6abd22017-09-06 05:46:29 -070067 RTC_EXCLUSIVE_LOCK_FUNCTION(checker);
68 ~SequencedTaskCheckerScope() RTC_UNLOCK_FUNCTION();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020069};
70
71} // namespace internal
72
73#define RTC_DCHECK_CALLED_SEQUENTIALLY(x) \
74 rtc::internal::SequencedTaskCheckerScope checker(x)
75
danilchap42a70e32017-09-06 01:38:35 -070076#undef ENABLE_SEQUENCED_TASK_CHECKER
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020077
78} // namespace rtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020079#endif // RTC_BASE_SEQUENCED_TASK_CHECKER_H_