File: handle_signal_tracker.cc

package info (click to toggle)
chromium 135.0.7049.95-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,959,392 kB
  • sloc: cpp: 34,198,526; ansic: 7,100,035; javascript: 3,985,800; python: 1,395,489; asm: 896,754; xml: 722,891; pascal: 180,504; sh: 94,909; perl: 88,388; objc: 79,739; sql: 53,020; cs: 41,358; fortran: 24,137; makefile: 22,501; php: 13,699; tcl: 10,142; yacc: 8,822; ruby: 7,350; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; awk: 197; sed: 36
file content (76 lines) | stat: -rw-r--r-- 2,711 bytes parent folder | download | duplicates (9)
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
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "mojo/public/cpp/system/handle_signal_tracker.h"

#include "base/functional/bind.h"
#include "base/synchronization/lock.h"
#include "base/task/sequenced_task_runner.h"
#include "mojo/public/cpp/system/handle_signals_state.h"

namespace mojo {

HandleSignalTracker::HandleSignalTracker(
    Handle handle,
    MojoHandleSignals signals,
    scoped_refptr<base::SequencedTaskRunner> task_runner)
    : high_watcher_(FROM_HERE,
                    SimpleWatcher::ArmingPolicy::MANUAL,
                    task_runner),
      low_watcher_(FROM_HERE,
                   SimpleWatcher::ArmingPolicy::MANUAL,
                   std::move(task_runner)) {
  MojoResult rv = high_watcher_.Watch(
      handle, signals, MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
      base::BindRepeating(&HandleSignalTracker::OnNotify,
                          base::Unretained(this)));
  DCHECK_EQ(MOJO_RESULT_OK, rv);

  rv = low_watcher_.Watch(handle, signals,
                          MOJO_TRIGGER_CONDITION_SIGNALS_UNSATISFIED,
                          base::BindRepeating(&HandleSignalTracker::OnNotify,
                                              base::Unretained(this)));
  DCHECK_EQ(MOJO_RESULT_OK, rv);

  last_known_state_ = handle.QuerySignalsState();

  Arm();
}

HandleSignalTracker::~HandleSignalTracker() = default;

void HandleSignalTracker::Arm() {
  // Arm either the low watcher or high watcher. We cycle until one of them
  // succeeds, which should almost always happen within two iterations.
  bool arm_low_watcher = true;
  for (;;) {
    MojoResult ready_result;
    SimpleWatcher& watcher = arm_low_watcher ? low_watcher_ : high_watcher_;
    MojoResult result = watcher.Arm(&ready_result, &last_known_state_);
    if (result == MOJO_RESULT_OK) {
      // Successfully armed one of the watchers, so we can go back to waiting
      // for a notification.
      return;
    }

    DCHECK_EQ(MOJO_RESULT_FAILED_PRECONDITION, result);
    if (ready_result == MOJO_RESULT_FAILED_PRECONDITION && !arm_low_watcher) {
      // The high watcher failed to arm because the watched signal will never
      // be satisfied again. We can also return in this case, and
      // |last_known_state_| will remain with its current value indefinitely.
      return;
    }
    arm_low_watcher = !arm_low_watcher;
  }
}

void HandleSignalTracker::OnNotify(MojoResult result,
                                   const HandleSignalsState& state) {
  last_known_state_ = state;
  Arm();
  if (notification_callback_)
    notification_callback_.Run(state);
}

}  // namespace mojo