File: 01-single-task-queue.cc

package info (click to toggle)
chromium 145.0.7632.159-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,976,224 kB
  • sloc: cpp: 36,198,469; ansic: 7,634,080; javascript: 3,564,060; python: 1,649,622; xml: 838,470; 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,144; 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 (80 lines) | stat: -rw-r--r-- 3,721 bytes parent folder | download | duplicates (11)
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
// Copyright 2023 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/logging.h"
#include "base/message_loop/message_pump.h"
#include "base/run_loop.h"
#include "base/task/sequence_manager/sequence_manager.h"
#include "base/task/sequence_manager/task_queue.h"
#include "base/task/single_thread_task_runner.h"

// See the documentation below.
enum class TaskType : unsigned char {
  kMain = 1,
};

void RunTaskAndQuit(base::OnceClosure quit_closure) {
  LOG(INFO) << "Whoah, we're doing a task! Now let's quit";
  std::move(quit_closure).Run();
}

int main() {
  base::PlatformThread::SetName("SchedulingDemoMain");

  // Create MessagePump that drives the SequenceManager that gets bound to this
  // thread further down. This pump's type is "default", which is a really
  // simple kind of platform-agtnostic message pump that only has the ability to
  // listen for tasks (possibly with delays) and run them; it can't, for
  // example:
  //   1.) Watch file descriptors or native sockets
  //   2.) Listen to platform-specific UI events, such as WM_PAINT on Windows or
  //       `NSEvent*`s on macOS.
  std::unique_ptr<base::MessagePump> pump =
      base::MessagePump::Create(base::MessagePumpType::DEFAULT);

  // Create the thread's SequenceManager.
  //
  // What this really does is:
  //   1.) Creates an unbound SequenceManager (the default)
  //   2.) Immediately "binds" it to the current thread
  // What (2) really means is that we invoke
  // `SequenceManagerImpl::CompleteInitializationOnBoundThread()`, which sets
  // the thread-local storage SequenceManager to `sequence_manager` below.
  // That's important so that `RunLoop` objects that have no direct access to
  // `sequence_manager` know which manager to tell to "run" later on.
  std::unique_ptr<base::sequence_manager::SequenceManager> sequence_manager =
      base::sequence_manager::CreateSequenceManagerOnCurrentThreadWithPump(
          std::move(pump),
          base::sequence_manager::SequenceManager::Settings::Builder().Build());

  // Create a default TaskQueue that feeds into the SequenceManager. Inside a
  // SequenceManager, TaskQueue is the basic unit of scheduling...
  base::sequence_manager::TaskQueue::Handle default_task_queue =
      sequence_manager->CreateTaskQueue(base::sequence_manager::TaskQueue::Spec(
          base::sequence_manager::QueueName::DEFAULT_TQ));

  // ... and from a TaskQueue you can get an arbitrary number of TaskRunners,
  // which actually let you post tasks to the underlying/internal queue.
  //
  // Why do we even have this distinction? Why even have multiple TaskRunners
  // associated with the same underlying queue? Answer: this is for task
  // attribution purposes, so you can query metrics to see which TaskRunners
  // (used by different places in your code) are posting what kind of tasks
  // and get other interesting information about them.
  scoped_refptr<base::SingleThreadTaskRunner> task_runner =
      default_task_queue->CreateTaskRunner(static_cast<int>(TaskType::kMain));
  sequence_manager->SetDefaultTaskRunner(task_runner);

  // Now that this thread has a bound sequence manager set up, we can:
  //   1.) Start posting tasks to its queues which are not yet being processed
  //   2.) Start processing tasks from the queues by "running" the loop, which
  //       runs the sequence manager's underlying message pump.
  base::RunLoop run_loop;
  task_runner->PostTask(
      FROM_HERE, base::BindOnce(&RunTaskAndQuit, run_loop.QuitClosure()));
  // This synchronously blocks (by running this thread's loop) until the quit
  // closure is called.
  run_loop.Run();
  return 0;
}