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 <functional>
20 #include <string>
21 
22 #include <utils/Timers.h>
23 
24 namespace android::scheduler {
25 
26 class Clock {
27 public:
28     virtual ~Clock();
29 
30     /*
31      * Returns the SYSTEM_TIME_MONOTONIC, used by testing infra to stub time.
32      */
33     virtual nsecs_t now() const = 0;
34 
35 protected:
36     Clock() = default;
37 
38     Clock(const Clock&) = delete;
39     Clock& operator=(const Clock&) = delete;
40 };
41 
42 /*
43  * TimeKeeper is the interface for a single-shot timer primitive.
44  */
45 class TimeKeeper : public Clock {
46 public:
47     virtual ~TimeKeeper();
48 
49     /*
50      * Arms callback to fired when time is current based on CLOCK_MONOTONIC
51      * There is only one timer, and subsequent calls will reset the callback function and the time.
52      */
53     virtual void alarmAt(std::function<void()>, nsecs_t time) = 0;
54 
55     /*
56      * Cancels an existing pending callback
57      */
58     virtual void alarmCancel() = 0;
59 
60     virtual void dump(std::string&) const = 0;
61 
62 protected:
63     TimeKeeper() = default;
64 
65     TimeKeeper(const TimeKeeper&) = delete;
66     TimeKeeper& operator=(const TimeKeeper&) = delete;
67 };
68 
69 } // namespace android::scheduler
70