1 /*
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 
22 namespace android::scheduler {
23 
24 class Clock {
25 public:
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 
32 protected:
33     Clock() = default;
34     Clock(Clock const&) = delete;
35     Clock& operator=(Clock const&) = delete;
36 };
37 
38 /*
39  * TimeKeeper is the interface for a single-shot timer primitive.
40  */
41 class TimeKeeper : public Clock {
42 public:
43     virtual ~TimeKeeper();
44 
45     /*
46      * Arms callback to fired in time nanoseconds.
47      * There is only one timer, and subsequent calls will reset the callback function and the time.
48      */
49     virtual void alarmIn(std::function<void()> const& callback, nsecs_t time) = 0;
50 
51     /*
52      * Cancels an existing pending callback
53      */
54     virtual void alarmCancel() = 0;
55 
56     virtual void dump(std::string& result) const = 0;
57 
58 protected:
59     TimeKeeper(TimeKeeper const&) = delete;
60     TimeKeeper& operator=(TimeKeeper const&) = delete;
61     TimeKeeper() = default;
62 };
63 
64 } // namespace android::scheduler
65