Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 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 | |
Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 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 | |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 38 | /* |
| 39 | * TimeKeeper is the interface for a single-shot timer primitive. |
| 40 | */ |
Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 41 | class TimeKeeper : public Clock { |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 42 | public: |
| 43 | virtual ~TimeKeeper(); |
| 44 | |
| 45 | /* |
Ady Abraham | b491c90 | 2020-08-15 15:47:56 -0700 | [diff] [blame] | 46 | * Arms callback to fired when time is current based on CLOCK_MONOTONIC |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 47 | * There is only one timer, and subsequent calls will reset the callback function and the time. |
| 48 | */ |
Ady Abraham | b491c90 | 2020-08-15 15:47:56 -0700 | [diff] [blame] | 49 | virtual void alarmAt(std::function<void()> const& callback, nsecs_t time) = 0; |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 50 | |
| 51 | /* |
| 52 | * Cancels an existing pending callback |
| 53 | */ |
| 54 | virtual void alarmCancel() = 0; |
| 55 | |
Ady Abraham | 7539872 | 2020-04-07 14:08:45 -0700 | [diff] [blame] | 56 | virtual void dump(std::string& result) const = 0; |
| 57 | |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 58 | protected: |
| 59 | TimeKeeper(TimeKeeper const&) = delete; |
| 60 | TimeKeeper& operator=(TimeKeeper const&) = delete; |
| 61 | TimeKeeper() = default; |
| 62 | }; |
| 63 | |
| 64 | } // namespace android::scheduler |