| 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 | /* | 
|  | 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 |  | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 56 | protected: | 
|  | 57 | TimeKeeper(TimeKeeper const&) = delete; | 
|  | 58 | TimeKeeper& operator=(TimeKeeper const&) = delete; | 
|  | 59 | TimeKeeper() = default; | 
|  | 60 | }; | 
|  | 61 |  | 
|  | 62 | } // namespace android::scheduler |