blob: daa774b0f919f495e16144ae2b112c4fec7c84e3 [file] [log] [blame]
[email protected]399ed422012-12-27 19:58:001// 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#include "base/sequence_checker_impl.h"
6
fdorayeed5fa72016-07-26 22:28:457#include "base/logging.h"
tzik0c2fcf52017-02-16 08:52:318#include "base/memory/ptr_util.h"
9#include "base/sequence_token.h"
tzik0c2fcf52017-02-16 08:52:3110#include "base/threading/thread_checker_impl.h"
fdorayeed5fa72016-07-26 22:28:4511
[email protected]399ed422012-12-27 19:58:0012namespace base {
13
tzik0c2fcf52017-02-16 08:52:3114class SequenceCheckerImpl::Core {
15 public:
Francois Doray4914ec142018-02-22 12:48:3816 Core() : sequence_token_(SequenceToken::GetForCurrentThread()) {}
[email protected]399ed422012-12-27 19:58:0017
tzik0c2fcf52017-02-16 08:52:3118 ~Core() = default;
19
gab768802392017-03-31 00:15:1720 bool CalledOnValidSequence() const {
tzik0c2fcf52017-02-16 08:52:3121 if (sequence_token_.IsValid())
22 return sequence_token_ == SequenceToken::GetForCurrentThread();
23
tzik0c2fcf52017-02-16 08:52:3124 // SequenceChecker behaves as a ThreadChecker when it is not bound to a
25 // valid sequence token.
26 return thread_checker_.CalledOnValidThread();
27 }
28
29 private:
30 SequenceToken sequence_token_;
31
Francois Doray4914ec142018-02-22 12:48:3832 // Used when |sequence_token_| is invalid.
tzik0c2fcf52017-02-16 08:52:3133 ThreadCheckerImpl thread_checker_;
34};
35
Jeremy Roman9532f252017-08-16 23:27:2436SequenceCheckerImpl::SequenceCheckerImpl() : core_(std::make_unique<Core>()) {}
fdorayeed5fa72016-07-26 22:28:4537SequenceCheckerImpl::~SequenceCheckerImpl() = default;
[email protected]399ed422012-12-27 19:58:0038
fdoraye2b19a12016-07-29 02:30:1639bool SequenceCheckerImpl::CalledOnValidSequence() const {
[email protected]399ed422012-12-27 19:58:0040 AutoLock auto_lock(lock_);
tzik0c2fcf52017-02-16 08:52:3141 if (!core_)
Jeremy Roman9532f252017-08-16 23:27:2442 core_ = std::make_unique<Core>();
gab768802392017-03-31 00:15:1743 return core_->CalledOnValidSequence();
[email protected]399ed422012-12-27 19:58:0044}
45
[email protected]d52426c2013-07-30 19:26:4046void SequenceCheckerImpl::DetachFromSequence() {
[email protected]399ed422012-12-27 19:58:0047 AutoLock auto_lock(lock_);
tzik0c2fcf52017-02-16 08:52:3148 core_.reset();
[email protected]399ed422012-12-27 19:58:0049}
50
51} // namespace base