John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 1 | /* |
| 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> |
| 29 | #include <variant> |
| 30 | #include <vector> |
| 31 | |
| 32 | namespace android::uirenderer { |
| 33 | |
| 34 | struct MonotonicClock { |
| 35 | static nsecs_t now() { return systemTime(CLOCK_MONOTONIC); } |
| 36 | }; |
| 37 | |
| 38 | class WorkQueue { |
| 39 | PREVENT_COPY_AND_ASSIGN(WorkQueue); |
| 40 | public: |
| 41 | using clock = MonotonicClock; |
| 42 | |
| 43 | private: |
| 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 | |
| 58 | public: |
| 59 | WorkQueue(std::function<void()>&& wakeFunc, std::mutex& lock) |
| 60 | : mWakeFunc(move(wakeFunc)) |
| 61 | , mLock(lock) {} |
| 62 | |
| 63 | void process() { |
| 64 | auto now = clock::now(); |
| 65 | std::vector<WorkItem> toProcess; |
| 66 | { |
| 67 | std::unique_lock _lock{mLock}; |
| 68 | if (mWorkQueue.empty()) return; |
| 69 | toProcess = std::move(mWorkQueue); |
| 70 | auto moveBack = find_if(std::begin(toProcess), std::end(toProcess), |
| 71 | [&now](WorkItem& item) { |
| 72 | return item.runAt > now; |
| 73 | }); |
| 74 | if (moveBack != std::end(toProcess)) { |
| 75 | mWorkQueue.reserve(std::distance(moveBack, std::end(toProcess)) + 5); |
| 76 | std::move(moveBack, std::end(toProcess), std::back_inserter(mWorkQueue)); |
| 77 | toProcess.erase(moveBack, std::end(toProcess)); |
| 78 | } |
| 79 | } |
| 80 | for (auto& item : toProcess) { |
| 81 | item.work(); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | template<class F> |
| 86 | void postAt(nsecs_t time, F&& func) { |
| 87 | enqueue(WorkItem{time, std::function<void()>(std::forward<F>(func))}); |
| 88 | } |
| 89 | |
| 90 | template<class F> |
| 91 | void postDelayed(nsecs_t delay, F&& func) { |
| 92 | enqueue(WorkItem{clock::now() + delay, std::function<void()>(std::forward<F>(func))}); |
| 93 | } |
| 94 | |
| 95 | template<class F> |
| 96 | void post(F&& func) { |
| 97 | postAt(0, std::forward<F>(func)); |
| 98 | } |
| 99 | |
| 100 | template<class F> |
| 101 | auto async(F&& func) -> std::future<decltype(func())> { |
| 102 | typedef std::packaged_task<decltype(func())()> task_t; |
| 103 | auto task = std::make_shared<task_t>(std::forward<F>(func)); |
| 104 | post([task]() { std::invoke(*task); }); |
| 105 | return task->get_future(); |
| 106 | } |
| 107 | |
| 108 | template<class F> |
| 109 | auto runSync(F&& func) -> decltype(func()) { |
| 110 | std::packaged_task<decltype(func())()> task{std::forward<F>(func)}; |
| 111 | post([&task]() { std::invoke(task); }); |
| 112 | return task.get_future().get(); |
| 113 | }; |
| 114 | |
| 115 | nsecs_t nextWakeup(std::unique_lock<std::mutex> &lock) { |
| 116 | if (mWorkQueue.empty()) { |
| 117 | return std::numeric_limits<nsecs_t>::max(); |
| 118 | } else { |
| 119 | return std::begin(mWorkQueue)->runAt; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | private: |
| 124 | void enqueue(WorkItem&& item) { |
| 125 | bool needsWakeup; |
| 126 | { |
| 127 | std::unique_lock _lock{mLock}; |
| 128 | auto insertAt = std::find_if(std::begin(mWorkQueue), std::end(mWorkQueue), |
| 129 | [time = item.runAt](WorkItem& item) { |
| 130 | return item.runAt > time; |
| 131 | }); |
| 132 | needsWakeup = std::begin(mWorkQueue) == insertAt; |
| 133 | mWorkQueue.emplace(insertAt, std::move(item)); |
| 134 | } |
| 135 | if (needsWakeup) { |
| 136 | mWakeFunc(); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | std::function<void()> mWakeFunc; |
| 141 | |
| 142 | std::mutex& mLock; |
| 143 | std::vector<WorkItem> mWorkQueue; |
| 144 | }; |
| 145 | |
| 146 | } // namespace android::uirenderer |
| 147 | |
| 148 | #endif //HWUI_WORKQUEUE_H |