summaryrefslogtreecommitdiff
path: root/common/scoped_task_id.h
blob: 91a2986051b495957c818bfb726bf4c1ffcd3175 (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
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
122
123
//
// Copyright (C) 2021 The Android Open Source Project
//
// 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
//
//      http://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 UPDATE_ENGINE_SCOPED_TASK_ID_H_
#define UPDATE_ENGINE_SCOPED_TASK_ID_H_

#include <type_traits>
#include <utility>

#include <base/bind.h>
#include <brillo/message_loops/message_loop.h>

namespace chromeos_update_engine {

// This class provides unique_ptr like semantic for |MessageLoop::TaskId|, when
// instance of this class goes out of scope, underlying task will be cancelled.
class ScopedTaskId {
  using MessageLoop = brillo::MessageLoop;

 public:
  // Move only type similar to unique_ptr.
  ScopedTaskId(const ScopedTaskId&) = delete;
  ScopedTaskId& operator=(const ScopedTaskId&) = delete;

  constexpr ScopedTaskId() = default;

  constexpr ScopedTaskId(ScopedTaskId&& other) noexcept {
    *this = std::move(other);
  }

  constexpr ScopedTaskId& operator=(ScopedTaskId&& other) noexcept {
    std::swap(task_id_, other.task_id_);
    return *this;
  }

  // Post a callback on current message loop, return true if succeeded, false if
  // the previous callback hasn't run yet, or scheduling failed at MessageLoop
  // side.
  [[nodiscard]] bool PostTask(const base::Location& from_here,
                              base::OnceClosure&& callback,
                              base::TimeDelta delay = {}) noexcept {
    return PostTask<decltype(callback)>(from_here, std::move(callback), delay);
  }
  [[nodiscard]] bool PostTask(const base::Location& from_here,
                              std::function<void()>&& callback,
                              base::TimeDelta delay = {}) noexcept {
    return PostTask<decltype(callback)>(from_here, std::move(callback), delay);
  }

  ~ScopedTaskId() noexcept { Cancel(); }

  // Cancel the underlying managed task, true if cancel successful. False if no
  // task scheduled or task cancellation failed
  bool Cancel() noexcept {
    if (task_id_ != MessageLoop::kTaskIdNull) {
      if (MessageLoop::current()->CancelTask(task_id_)) {
        LOG(INFO) << "Cancelled task id " << task_id_;
        task_id_ = MessageLoop::kTaskIdNull;
        return true;
      }
    }
    return false;
  }

  [[nodiscard]] constexpr bool IsScheduled() const noexcept {
    return task_id_ != MessageLoop::kTaskIdNull;
  }

  [[nodiscard]] constexpr bool operator==(const ScopedTaskId& other) const
      noexcept {
    return other.task_id_ == task_id_;
  }

  [[nodiscard]] constexpr bool operator<(const ScopedTaskId& other) const
      noexcept {
    return task_id_ < other.task_id_;
  }

 private:
  template <typename Callable>
  [[nodiscard]] bool PostTask(const base::Location& from_here,
                              Callable&& callback,
                              base::TimeDelta delay) noexcept {
    if (task_id_ != MessageLoop::kTaskIdNull) {
      LOG(ERROR) << "Scheduling another task but task id " << task_id_
                 << " isn't executed yet! This can cause the old task to leak.";
      return false;
    }
    task_id_ = MessageLoop::current()->PostDelayedTask(
        from_here,
        base::BindOnce(&ScopedTaskId::ExecuteTask<decltype(callback)>,
                       base::Unretained(this),
                       std::move(callback)),
        delay);
    return task_id_ != MessageLoop::kTaskIdNull;
  }
  template <typename Callable>
  void ExecuteTask(Callable&& callback) {
    task_id_ = MessageLoop::kTaskIdNull;
    if constexpr (std::is_same_v<Callable&&, base::OnceClosure&&>) {
      std::move(callback).Run();
    } else {
      std::move(callback)();
    }
  }
  MessageLoop::TaskId task_id_{MessageLoop::kTaskIdNull};
};
}  // namespace chromeos_update_engine

#endif