blob: f2751d2a6cc7cd36bee2927df16d4499025baadd [file] [log] [blame]
John Reckf8441e62017-10-23 13:10:41 -07001/*
2 * Copyright (C) 2017 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#ifndef HWUI_WORKQUEUE_H
18#define HWUI_WORKQUEUE_H
19
20#include "utils/Macros.h"
21
22#include <log/log.h>
23#include <utils/Timers.h>
24
25#include <condition_variable>
26#include <functional>
27#include <future>
28#include <mutex>
John Reckf8441e62017-10-23 13:10:41 -070029#include <vector>
30
31namespace android::uirenderer {
32
33struct MonotonicClock {
Jerome Gaillarde218c692019-06-14 12:58:57 +010034 static nsecs_t now() { return systemTime(SYSTEM_TIME_MONOTONIC); }
John Reckf8441e62017-10-23 13:10:41 -070035};
36
37class WorkQueue {
38 PREVENT_COPY_AND_ASSIGN(WorkQueue);
John Reck1bcacfd2017-11-03 10:12:19 -070039
John Reckf8441e62017-10-23 13:10:41 -070040public:
41 using clock = MonotonicClock;
42
43private:
44 struct WorkItem {
45 WorkItem() = delete;
46 WorkItem(const WorkItem& other) = delete;
47 WorkItem& operator=(const WorkItem& other) = delete;
48 WorkItem(WorkItem&& other) = default;
49 WorkItem& operator=(WorkItem&& other) = default;
50
51 WorkItem(nsecs_t runAt, std::function<void()>&& work)
52 : runAt(runAt), work(std::move(work)) {}
53
54 nsecs_t runAt;
55 std::function<void()> work;
56 };
57
58public:
59 WorkQueue(std::function<void()>&& wakeFunc, std::mutex& lock)
Kohsuke Yatoha4e93972022-07-28 00:14:06 +000060 : mWakeFunc(std::move(wakeFunc)), mLock(lock) {}
John Reckf8441e62017-10-23 13:10:41 -070061
62 void process() {
63 auto now = clock::now();
64 std::vector<WorkItem> toProcess;
65 {
66 std::unique_lock _lock{mLock};
67 if (mWorkQueue.empty()) return;
68 toProcess = std::move(mWorkQueue);
69 auto moveBack = find_if(std::begin(toProcess), std::end(toProcess),
John Reck1bcacfd2017-11-03 10:12:19 -070070 [&now](WorkItem& item) { return item.runAt > now; });
John Reckf8441e62017-10-23 13:10:41 -070071 if (moveBack != std::end(toProcess)) {
72 mWorkQueue.reserve(std::distance(moveBack, std::end(toProcess)) + 5);
73 std::move(moveBack, std::end(toProcess), std::back_inserter(mWorkQueue));
74 toProcess.erase(moveBack, std::end(toProcess));
75 }
76 }
77 for (auto& item : toProcess) {
78 item.work();
79 }
80 }
81
John Reck1bcacfd2017-11-03 10:12:19 -070082 template <class F>
John Reckf8441e62017-10-23 13:10:41 -070083 void postAt(nsecs_t time, F&& func) {
84 enqueue(WorkItem{time, std::function<void()>(std::forward<F>(func))});
85 }
86
John Reck1bcacfd2017-11-03 10:12:19 -070087 template <class F>
John Reckf8441e62017-10-23 13:10:41 -070088 void postDelayed(nsecs_t delay, F&& func) {
89 enqueue(WorkItem{clock::now() + delay, std::function<void()>(std::forward<F>(func))});
90 }
91
John Reck1bcacfd2017-11-03 10:12:19 -070092 template <class F>
John Reckf8441e62017-10-23 13:10:41 -070093 void post(F&& func) {
94 postAt(0, std::forward<F>(func));
95 }
96
John Reck1bcacfd2017-11-03 10:12:19 -070097 template <class F>
John Reckf8441e62017-10-23 13:10:41 -070098 auto async(F&& func) -> std::future<decltype(func())> {
99 typedef std::packaged_task<decltype(func())()> task_t;
100 auto task = std::make_shared<task_t>(std::forward<F>(func));
101 post([task]() { std::invoke(*task); });
102 return task->get_future();
103 }
104
John Reck1bcacfd2017-11-03 10:12:19 -0700105 template <class F>
John Reckf8441e62017-10-23 13:10:41 -0700106 auto runSync(F&& func) -> decltype(func()) {
107 std::packaged_task<decltype(func())()> task{std::forward<F>(func)};
108 post([&task]() { std::invoke(task); });
109 return task.get_future().get();
110 };
111
John Reck1bcacfd2017-11-03 10:12:19 -0700112 nsecs_t nextWakeup(std::unique_lock<std::mutex>& lock) {
John Reckf8441e62017-10-23 13:10:41 -0700113 if (mWorkQueue.empty()) {
114 return std::numeric_limits<nsecs_t>::max();
115 } else {
116 return std::begin(mWorkQueue)->runAt;
117 }
118 }
119
120private:
121 void enqueue(WorkItem&& item) {
122 bool needsWakeup;
123 {
124 std::unique_lock _lock{mLock};
John Reck1bcacfd2017-11-03 10:12:19 -0700125 auto insertAt = std::find_if(
126 std::begin(mWorkQueue), std::end(mWorkQueue),
127 [time = item.runAt](WorkItem & item) { return item.runAt > time; });
John Reckf8441e62017-10-23 13:10:41 -0700128 needsWakeup = std::begin(mWorkQueue) == insertAt;
129 mWorkQueue.emplace(insertAt, std::move(item));
130 }
131 if (needsWakeup) {
132 mWakeFunc();
133 }
134 }
135
136 std::function<void()> mWakeFunc;
137
138 std::mutex& mLock;
139 std::vector<WorkItem> mWorkQueue;
140};
141
John Reck1bcacfd2017-11-03 10:12:19 -0700142} // namespace android::uirenderer
John Reckf8441e62017-10-23 13:10:41 -0700143
John Reck1bcacfd2017-11-03 10:12:19 -0700144#endif // HWUI_WORKQUEUE_H