blob: 471631844ba7da9a8ceb8a908767b1795eed0721 [file] [log] [blame]
akalin@chromium.org4fb2deb2012-12-28 04:58:00 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef BASE_SEQUENCE_CHECKER_H_
6#define BASE_SEQUENCE_CHECKER_H_
7
akalin@chromium.org4fb2deb2012-12-28 04:58:00 +09008#include "base/sequence_checker_impl.h"
akalin@chromium.org4fb2deb2012-12-28 04:58:00 +09009
10namespace base {
11
akalin@chromium.org4fb2deb2012-12-28 04:58:00 +090012// Do nothing implementation, for use in release mode.
13//
14// Note: You should almost always use the SequenceChecker class to get
15// the right version for your build configuration.
16class SequenceCheckerDoNothing {
17 public:
fdoraye4a58272016-07-29 11:30:16 +090018 bool CalledOnValidSequence() const { return true; }
akalin@chromium.org4fb2deb2012-12-28 04:58:00 +090019
tommycli@chromium.org623e16c2013-07-31 04:26:40 +090020 void DetachFromSequence() {}
akalin@chromium.org4fb2deb2012-12-28 04:58:00 +090021};
22
fdoray4bcf1082016-07-27 07:28:45 +090023// SequenceChecker is a helper class to verify that calls to some methods of a
24// class are sequenced. Calls are sequenced when they are issued:
25// - From tasks posted to SequencedTaskRunners or SingleThreadTaskRunners bound
26// to the same sequence, or,
27// - From a single thread outside of any task.
akalin@chromium.org4fb2deb2012-12-28 04:58:00 +090028//
29// Example:
30// class MyClass {
31// public:
akalin@chromium.org4fb2deb2012-12-28 04:58:00 +090032// void Foo() {
fdoraye4a58272016-07-29 11:30:16 +090033// DCHECK(sequence_checker_.CalledOnValidSequence());
akalin@chromium.org4fb2deb2012-12-28 04:58:00 +090034// ... (do stuff) ...
35// }
36//
37// private:
38// SequenceChecker sequence_checker_;
39// }
40//
fdoraye4a58272016-07-29 11:30:16 +090041// In Release mode, CalledOnValidSequence() will always return true.
gabfb83c5a2016-08-02 05:03:41 +090042#if DCHECK_IS_ON()
akalin@chromium.org4fb2deb2012-12-28 04:58:00 +090043class SequenceChecker : public SequenceCheckerImpl {
akalin@chromium.org4fb2deb2012-12-28 04:58:00 +090044};
45#else
46class SequenceChecker : public SequenceCheckerDoNothing {
akalin@chromium.org4fb2deb2012-12-28 04:58:00 +090047};
gabfb83c5a2016-08-02 05:03:41 +090048#endif // DCHECK_IS_ON()
akalin@chromium.org4fb2deb2012-12-28 04:58:00 +090049
50} // namespace base
51
52#endif // BASE_SEQUENCE_CHECKER_H_