blob: 0ae82c83a575de6d3907cf1daa934b791feb1d44 [file] [log] [blame]
Kevin DuBoiscc27b502019-11-13 09:40:07 -08001/*
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 "TimeKeeper.h"
20
21#include <android-base/thread_annotations.h>
22#include <array>
23#include <thread>
24
25namespace android::scheduler {
26
27class Timer : public TimeKeeper {
28public:
29 Timer();
30 ~Timer();
31 nsecs_t now() const final;
32
33 // NB: alarmIn and alarmCancel are threadsafe; with the last-returning function being effectual
34 // Most users will want to serialize thes calls so as to be aware of the timer state.
35 void alarmIn(std::function<void()> const& cb, nsecs_t fireIn) final;
36 void alarmCancel() final;
37
38private:
39 int const mTimerFd;
40 int const mEpollFd;
41 std::array<int, 2> mPipes;
42
43 std::thread mDispatchThread;
44 void dispatch();
45 void endDispatch();
46
47 std::mutex mMutex;
48 std::function<void()> mCallback GUARDED_BY(mMutex);
49};
50
51} // namespace android::scheduler