blob: 40dd8410a3d328003e2745fadbcbfc6e4ddef4ec [file] [log] [blame]
Kevin DuBois305bef12019-10-09 13:23:27 -07001/*
2 * Copyright 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <utils/Timers.h>
20#include <functional>
21
22namespace android::scheduler {
23
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080024class Clock {
25public:
26 virtual ~Clock();
27 /*
28 * Returns the SYSTEM_TIME_MONOTONIC, used by testing infra to stub time.
29 */
30 virtual nsecs_t now() const = 0;
31
32protected:
33 Clock() = default;
34 Clock(Clock const&) = delete;
35 Clock& operator=(Clock const&) = delete;
36};
37
Kevin DuBois305bef12019-10-09 13:23:27 -070038/*
39 * TimeKeeper is the interface for a single-shot timer primitive.
40 */
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080041class TimeKeeper : public Clock {
Kevin DuBois305bef12019-10-09 13:23:27 -070042public:
43 virtual ~TimeKeeper();
44
45 /*
Ady Abrahamb491c902020-08-15 15:47:56 -070046 * Arms callback to fired when time is current based on CLOCK_MONOTONIC
Kevin DuBois305bef12019-10-09 13:23:27 -070047 * There is only one timer, and subsequent calls will reset the callback function and the time.
48 */
Ady Abrahamb491c902020-08-15 15:47:56 -070049 virtual void alarmAt(std::function<void()> const& callback, nsecs_t time) = 0;
Kevin DuBois305bef12019-10-09 13:23:27 -070050
51 /*
52 * Cancels an existing pending callback
53 */
54 virtual void alarmCancel() = 0;
55
Ady Abraham75398722020-04-07 14:08:45 -070056 virtual void dump(std::string& result) const = 0;
57
Kevin DuBois305bef12019-10-09 13:23:27 -070058protected:
59 TimeKeeper(TimeKeeper const&) = delete;
60 TimeKeeper& operator=(TimeKeeper const&) = delete;
61 TimeKeeper() = default;
62};
63
64} // namespace android::scheduler