1b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Use of this source code is governed by a BSD-style license that can be
3b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// found in the LICENSE file.
4b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
5b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include "base/sequence_checker_impl.h"
6b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
7b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratnamespace base {
8b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
9b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratSequenceCheckerImpl::SequenceCheckerImpl()
10b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    : sequence_token_assigned_(false) {
11b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  AutoLock auto_lock(lock_);
12b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  EnsureSequenceTokenAssigned();
13b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
14b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
15b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratSequenceCheckerImpl::~SequenceCheckerImpl() {}
16b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
17b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratbool SequenceCheckerImpl::CalledOnValidSequencedThread() const {
18b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  AutoLock auto_lock(lock_);
19b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  EnsureSequenceTokenAssigned();
20b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
21b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // If this thread is not associated with a SequencedWorkerPool,
22b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // SequenceChecker behaves as a ThreadChecker. See header for details.
23b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  if (!sequence_token_.IsValid())
24b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    return thread_checker_.CalledOnValidThread();
25b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
26b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return sequence_token_.Equals(
27b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat      SequencedWorkerPool::GetSequenceTokenForCurrentThread());
28b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
29b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
30b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratvoid SequenceCheckerImpl::DetachFromSequence() {
31b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  AutoLock auto_lock(lock_);
32b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  thread_checker_.DetachFromThread();
33b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  sequence_token_assigned_ = false;
34b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  sequence_token_ = SequencedWorkerPool::SequenceToken();
35b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
36b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
37b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratvoid SequenceCheckerImpl::EnsureSequenceTokenAssigned() const {
38b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  lock_.AssertAcquired();
39b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  if (sequence_token_assigned_)
40b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    return;
41b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
42b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  sequence_token_assigned_ = true;
43b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  sequence_token_ = SequencedWorkerPool::GetSequenceTokenForCurrentThread();
44b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
45b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
46b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}  // namespace base
47