1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105

base / sequence_token.cc [blame]

// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/sequence_token.h"

#include "base/atomic_sequence_num.h"

namespace base {
namespace internal {

namespace {

base::AtomicSequenceNumber g_sequence_token_generator;

base::AtomicSequenceNumber g_task_token_generator;

constinit thread_local SequenceToken current_sequence_token;
constinit thread_local TaskToken current_task_token;
constinit thread_local bool current_task_is_thread_bound = true;
constinit thread_local bool current_task_is_running_synchronously = false;

}  // namespace

bool SequenceToken::operator==(const SequenceToken& other) const {
  return token_ == other.token_ && IsValid();
}

bool SequenceToken::operator!=(const SequenceToken& other) const {
  return !(*this == other);
}

bool SequenceToken::IsValid() const {
  return token_ != kInvalidSequenceToken;
}

int SequenceToken::ToInternalValue() const {
  return token_;
}

SequenceToken SequenceToken::Create() {
  return SequenceToken(g_sequence_token_generator.GetNext());
}

SequenceToken SequenceToken::GetForCurrentThread() {
  if (!current_sequence_token.IsValid()) {
    current_sequence_token = SequenceToken::Create();
    DCHECK(current_task_is_thread_bound);
  }
  return current_sequence_token;
}

bool TaskToken::operator==(const TaskToken& other) const {
  return token_ == other.token_ && IsValid();
}

bool TaskToken::operator!=(const TaskToken& other) const {
  return !(*this == other);
}

bool TaskToken::IsValid() const {
  return token_ != kInvalidTaskToken;
}

TaskToken TaskToken::Create() {
  return TaskToken(g_task_token_generator.GetNext());
}

TaskToken TaskToken::GetForCurrentThread() {
  return current_task_token;
}

bool CurrentTaskIsThreadBound() {
  return current_task_is_thread_bound;
}

TaskScope::TaskScope(SequenceToken sequence_token,
                     bool is_single_threaded,
                     bool is_running_synchronously)
    : previous_task_token_(TaskToken::GetForCurrentThread()),
      previous_sequence_token_(SequenceToken::GetForCurrentThread()),
      previous_task_is_thread_bound_(current_task_is_thread_bound),
      previous_task_is_running_synchronously_(
          current_task_is_running_synchronously) {
  current_task_token = TaskToken::Create();
  current_sequence_token = sequence_token;
  current_task_is_thread_bound = is_single_threaded;
  current_task_is_running_synchronously = is_running_synchronously;
}

TaskScope::~TaskScope() {
  current_task_token = previous_task_token_;
  current_sequence_token = previous_sequence_token_;
  current_task_is_thread_bound = previous_task_is_thread_bound_;
  current_task_is_running_synchronously =
      previous_task_is_running_synchronously_;
}

}  // namespace internal

bool CurrentTaskIsRunningSynchronously() {
  return internal::current_task_is_running_synchronously;
}

}  // namespace base