summaryrefslogtreecommitdiff
path: root/chromium/third_party/nearby/src/cpp/platform/base/cancellation_flag.h
blob: e861bfe0ab4c05e5968140b8ad889091e5540069 (plain)
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 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef PLATFORM_BASE_CANCELLATION_FLAG_H_
#define PLATFORM_BASE_CANCELLATION_FLAG_H_

#include <memory>

#include "absl/container/flat_hash_set.h"
#include "absl/synchronization/mutex.h"

namespace location {
namespace nearby {

// A cancellation flag to mark an operation has been cancelled and should be
// cleaned up as soon as possible.
class CancellationFlag {
 public:
  // The listener for cancellation.
  using CancelListener = std::function<void()>;

  CancellationFlag();
  explicit CancellationFlag(bool cancelled);
  CancellationFlag(const CancellationFlag &) = delete;
  CancellationFlag &operator=(const CancellationFlag &) = delete;
  CancellationFlag(CancellationFlag &&) = default;
  CancellationFlag &operator=(CancellationFlag &&) = default;
  virtual ~CancellationFlag();

  // Set the flag as cancelled.
  void Cancel() ABSL_LOCKS_EXCLUDED(mutex_);

  // Returns true if the flag has been set to cancelled.
  bool Cancelled() const ABSL_LOCKS_EXCLUDED(mutex_);

 private:
  friend class CancellationFlagListener;
  friend class CancellationFlagPeer;

  // The registration inserts the pointer of caller's listener callback into
  // `listeners_`, a flat hash set which support the pointer type for hashing
  // function. It conducts that 2 different pointers might point to the same
  // callback function which is unusal and should avoid. Hence we make it as
  // private and use `CancellationFlagListener` as a RAII to wrap the function.
  // The caller should register listener as lambda or std::function
  // via `CancellationFlagListener`.
  void RegisterOnCancelListener(CancelListener *listener)
      ABSL_LOCKS_EXCLUDED(mutex_);

  // The un-registration erases the pointer of caller's listener callback from
  // `listeners_`. This is paired to RegisterOnCancelListener which is
  // guaranteed to be called under `CancellationFlagListener`.
  void UnregisterOnCancelListener(CancelListener *listener)
      ABSL_LOCKS_EXCLUDED(mutex_);

  int CancelListenersSize() const ABSL_LOCKS_EXCLUDED(mutex_) {
    absl::MutexLock lock(mutex_.get());
    return listeners_.size();
  }

  std::unique_ptr<absl::Mutex> mutex_;
  bool cancelled_ ABSL_GUARDED_BY(mutex_) = false;
  absl::flat_hash_set<CancelListener *> ABSL_GUARDED_BY(mutex_) listeners_;
};

}  // namespace nearby
}  // namespace location

#endif  // PLATFORM_BASE_CANCELLATION_FLAG_H_