File: execution_fence.cc

package info (click to toggle)
chromium 145.0.7632.109-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,974,804 kB
  • sloc: cpp: 36,197,696; ansic: 7,602,761; javascript: 3,563,590; python: 1,649,324; xml: 838,427; asm: 717,087; pascal: 185,708; sh: 88,786; perl: 88,718; objc: 79,984; sql: 59,811; cs: 42,452; fortran: 24,101; makefile: 21,022; tcl: 15,277; php: 14,022; yacc: 9,066; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,328; ada: 727; jsp: 228; sed: 36
file content (121 lines) | stat: -rw-r--r-- 3,875 bytes parent folder | download | duplicates (5)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2025 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/task/execution_fence.h"

#include <memory>
#include <utility>
#include <vector>

#include "base/check_op.h"
#include "base/feature_list.h"
#include "base/features.h"
#include "base/no_destructor.h"
#include "base/not_fatal_until.h"
#include "base/sequence_checker.h"
#include "base/task/sequence_manager/sequence_manager.h"
#include "base/task/sequence_manager/task_queue.h"
#include "base/task/thread_pool/thread_pool_instance.h"
#include "third_party/abseil-cpp/absl/container/flat_hash_set.h"

namespace base {

using sequence_manager::SequenceManager;
using sequence_manager::TaskQueue;

namespace {

// Global list of SequenceManagers to notify of ScopedBestEffortExecutionFences.
class SequenceManagerRegistry {
 public:
  SequenceManagerRegistry() = default;
  ~SequenceManagerRegistry() = default;

  SequenceManagerRegistry(const SequenceManagerRegistry&) = delete;
  SequenceManagerRegistry& operator=(const SequenceManagerRegistry&) = delete;

  static SequenceManagerRegistry& GetInstance() {
    static NoDestructor<SequenceManagerRegistry> instance;
    return *instance;
  }

  void AddSequenceManager(SequenceManager* sequence_manager) {
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
    auto [_, inserted] = sequence_managers_.emplace(sequence_manager);
    CHECK(inserted, NotFatalUntil::M145);
  }

  void RemoveSequenceManager(SequenceManager* sequence_manager) {
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
    size_t erased = sequence_managers_.erase(sequence_manager);
    CHECK_EQ(erased, 1u, NotFatalUntil::M145);
  }

  std::vector<std::unique_ptr<TaskQueue::QueueEnabledVoter>>
  GetCurrentVoters() {
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
    std::vector<std::unique_ptr<TaskQueue::QueueEnabledVoter>> voters;
    for (auto* sequence_manager : sequence_managers_) {
      for (TaskQueue* task_queue :
           sequence_manager->GetBestEffortTaskQueues()) {
        if (task_queue->IsBlockedByScopedExecutionFences()) {
          voters.push_back(task_queue->CreateQueueEnabledVoter());
          voters.back()->SetVoteToEnable(false);
        }
      }
    }
    return voters;
  }

 private:
  SEQUENCE_CHECKER(sequence_checker_);
  absl::flat_hash_set<SequenceManager*> sequence_managers_
      GUARDED_BY_CONTEXT(sequence_checker_);
};

}  // namespace

ScopedThreadPoolExecutionFence::ScopedThreadPoolExecutionFence() {
  auto* thread_pool = ThreadPoolInstance::Get();
  CHECK(thread_pool, NotFatalUntil::M145);
  thread_pool->BeginFence();
}

ScopedThreadPoolExecutionFence::~ScopedThreadPoolExecutionFence() {
  auto* thread_pool = ThreadPoolInstance::Get();
  CHECK(thread_pool, NotFatalUntil::M145);
  thread_pool->EndFence();
}

ScopedBestEffortExecutionFence::ScopedBestEffortExecutionFence() {
  if (FeatureList::IsEnabled(
          features::kScopedBestEffortExecutionFenceForTaskQueue)) {
    task_queue_voters_ =
        SequenceManagerRegistry::GetInstance().GetCurrentVoters();
  }
  auto* thread_pool = ThreadPoolInstance::Get();
  CHECK(thread_pool, NotFatalUntil::M145);
  thread_pool->BeginBestEffortFence();
}

ScopedBestEffortExecutionFence::~ScopedBestEffortExecutionFence() {
  auto* thread_pool = ThreadPoolInstance::Get();
  CHECK(thread_pool, NotFatalUntil::M145);
  thread_pool->EndBestEffortFence();
}

// static
void ScopedBestEffortExecutionFence::AddSequenceManager(
    SequenceManager* sequence_manager) {
  SequenceManagerRegistry::GetInstance().AddSequenceManager(sequence_manager);
}

// static
void ScopedBestEffortExecutionFence::RemoveSequenceManager(
    SequenceManager* sequence_manager) {
  SequenceManagerRegistry::GetInstance().RemoveSequenceManager(
      sequence_manager);
}

}  // namespace base