Vishnu Nair | 34eb9ca | 2021-11-18 15:23:23 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 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 | //#define LOG_NDEBUG 0 |
| 18 | #undef LOG_TAG |
| 19 | #define LOG_TAG "BackgroundExecutor" |
| 20 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 21 | |
Alec Mouri | 8d7d0f4 | 2022-05-10 23:33:40 +0000 | [diff] [blame] | 22 | #include <utils/Log.h> |
Patrick Williams | 56cd9b7 | 2023-07-12 13:47:28 -0500 | [diff] [blame] | 23 | #include <mutex> |
Alec Mouri | 8d7d0f4 | 2022-05-10 23:33:40 +0000 | [diff] [blame] | 24 | |
Vishnu Nair | 34eb9ca | 2021-11-18 15:23:23 -0800 | [diff] [blame] | 25 | #include "BackgroundExecutor.h" |
| 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | ANDROID_SINGLETON_STATIC_INSTANCE(BackgroundExecutor); |
| 30 | |
| 31 | BackgroundExecutor::BackgroundExecutor() : Singleton<BackgroundExecutor>() { |
Patrick Williams | 13310b8 | 2023-05-17 14:40:18 -0500 | [diff] [blame] | 32 | // mSemaphore must be initialized before any calls to |
| 33 | // BackgroundExecutor::sendCallbacks. For this reason, we initialize it |
| 34 | // within the constructor instead of within mThread. |
| 35 | LOG_ALWAYS_FATAL_IF(sem_init(&mSemaphore, 0, 0), "sem_init failed"); |
Vishnu Nair | 34eb9ca | 2021-11-18 15:23:23 -0800 | [diff] [blame] | 36 | mThread = std::thread([&]() { |
Alec Mouri | 8d7d0f4 | 2022-05-10 23:33:40 +0000 | [diff] [blame] | 37 | while (!mDone) { |
| 38 | LOG_ALWAYS_FATAL_IF(sem_wait(&mSemaphore), "sem_wait failed (%d)", errno); |
Patrick Williams | 13310b8 | 2023-05-17 14:40:18 -0500 | [diff] [blame] | 39 | auto callbacks = mCallbacksQueue.pop(); |
| 40 | if (!callbacks) { |
| 41 | continue; |
Alec Mouri | 8d7d0f4 | 2022-05-10 23:33:40 +0000 | [diff] [blame] | 42 | } |
Patrick Williams | 13310b8 | 2023-05-17 14:40:18 -0500 | [diff] [blame] | 43 | for (auto& callback : *callbacks) { |
| 44 | callback(); |
Vishnu Nair | 34eb9ca | 2021-11-18 15:23:23 -0800 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | }); |
| 48 | } |
| 49 | |
| 50 | BackgroundExecutor::~BackgroundExecutor() { |
Alec Mouri | 8d7d0f4 | 2022-05-10 23:33:40 +0000 | [diff] [blame] | 51 | mDone = true; |
| 52 | LOG_ALWAYS_FATAL_IF(sem_post(&mSemaphore), "sem_post failed"); |
Vishnu Nair | 34eb9ca | 2021-11-18 15:23:23 -0800 | [diff] [blame] | 53 | if (mThread.joinable()) { |
| 54 | mThread.join(); |
Alec Mouri | 8d7d0f4 | 2022-05-10 23:33:40 +0000 | [diff] [blame] | 55 | LOG_ALWAYS_FATAL_IF(sem_destroy(&mSemaphore), "sem_destroy failed"); |
Vishnu Nair | 34eb9ca | 2021-11-18 15:23:23 -0800 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
Alec Mouri | 8d7d0f4 | 2022-05-10 23:33:40 +0000 | [diff] [blame] | 59 | void BackgroundExecutor::sendCallbacks(Callbacks&& tasks) { |
Patrick Williams | 13310b8 | 2023-05-17 14:40:18 -0500 | [diff] [blame] | 60 | mCallbacksQueue.push(std::move(tasks)); |
Alec Mouri | 8d7d0f4 | 2022-05-10 23:33:40 +0000 | [diff] [blame] | 61 | LOG_ALWAYS_FATAL_IF(sem_post(&mSemaphore), "sem_post failed"); |
Vishnu Nair | 34eb9ca | 2021-11-18 15:23:23 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Patrick Williams | 56cd9b7 | 2023-07-12 13:47:28 -0500 | [diff] [blame] | 64 | void BackgroundExecutor::flushQueue() { |
| 65 | std::mutex mutex; |
| 66 | std::condition_variable cv; |
| 67 | bool flushComplete = false; |
| 68 | sendCallbacks({[&]() { |
| 69 | std::scoped_lock lock{mutex}; |
| 70 | flushComplete = true; |
| 71 | cv.notify_one(); |
| 72 | }}); |
| 73 | std::unique_lock<std::mutex> lock{mutex}; |
| 74 | cv.wait(lock, [&]() { return flushComplete; }); |
| 75 | } |
| 76 | |
Patrick Williams | 13310b8 | 2023-05-17 14:40:18 -0500 | [diff] [blame] | 77 | } // namespace android |