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 | |
Pascal Mütschard | d0afeb7 | 2024-03-13 12:08:26 +0100 | [diff] [blame] | 22 | #include <processgroup/sched_policy.h> |
| 23 | #include <pthread.h> |
| 24 | #include <sched.h> |
Alec Mouri | 8d7d0f4 | 2022-05-10 23:33:40 +0000 | [diff] [blame] | 25 | #include <utils/Log.h> |
Patrick Williams | acd2258 | 2023-07-12 13:47:28 -0500 | [diff] [blame] | 26 | #include <mutex> |
Alec Mouri | 8d7d0f4 | 2022-05-10 23:33:40 +0000 | [diff] [blame] | 27 | |
Vishnu Nair | 34eb9ca | 2021-11-18 15:23:23 -0800 | [diff] [blame] | 28 | #include "BackgroundExecutor.h" |
| 29 | |
| 30 | namespace android { |
| 31 | |
Pascal Mütschard | d0afeb7 | 2024-03-13 12:08:26 +0100 | [diff] [blame] | 32 | namespace { |
Vishnu Nair | 34eb9ca | 2021-11-18 15:23:23 -0800 | [diff] [blame] | 33 | |
Pascal Mütschard | d0afeb7 | 2024-03-13 12:08:26 +0100 | [diff] [blame] | 34 | void set_thread_priority(bool highPriority) { |
| 35 | set_sched_policy(0, highPriority ? SP_FOREGROUND : SP_BACKGROUND); |
| 36 | struct sched_param param = {0}; |
| 37 | param.sched_priority = highPriority ? 2 : 0 /* must be 0 for non-RT */; |
| 38 | sched_setscheduler(gettid(), highPriority ? SCHED_FIFO : SCHED_NORMAL, ¶m); |
| 39 | } |
| 40 | |
| 41 | } // anonymous namespace |
| 42 | |
| 43 | BackgroundExecutor::BackgroundExecutor(bool highPriority) { |
Patrick Williams | 13310b8 | 2023-05-17 14:40:18 -0500 | [diff] [blame] | 44 | // mSemaphore must be initialized before any calls to |
| 45 | // BackgroundExecutor::sendCallbacks. For this reason, we initialize it |
| 46 | // within the constructor instead of within mThread. |
| 47 | LOG_ALWAYS_FATAL_IF(sem_init(&mSemaphore, 0, 0), "sem_init failed"); |
Pascal Mütschard | d0afeb7 | 2024-03-13 12:08:26 +0100 | [diff] [blame] | 48 | mThread = std::thread([&, highPriority]() { |
| 49 | set_thread_priority(highPriority); |
Alec Mouri | 8d7d0f4 | 2022-05-10 23:33:40 +0000 | [diff] [blame] | 50 | while (!mDone) { |
| 51 | LOG_ALWAYS_FATAL_IF(sem_wait(&mSemaphore), "sem_wait failed (%d)", errno); |
Patrick Williams | 13310b8 | 2023-05-17 14:40:18 -0500 | [diff] [blame] | 52 | auto callbacks = mCallbacksQueue.pop(); |
| 53 | if (!callbacks) { |
| 54 | continue; |
Alec Mouri | 8d7d0f4 | 2022-05-10 23:33:40 +0000 | [diff] [blame] | 55 | } |
Patrick Williams | 13310b8 | 2023-05-17 14:40:18 -0500 | [diff] [blame] | 56 | for (auto& callback : *callbacks) { |
| 57 | callback(); |
Vishnu Nair | 34eb9ca | 2021-11-18 15:23:23 -0800 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | }); |
Pascal Mütschard | d0afeb7 | 2024-03-13 12:08:26 +0100 | [diff] [blame] | 61 | if (highPriority) { |
| 62 | pthread_setname_np(mThread.native_handle(), "BckgrndExec HP"); |
| 63 | } else { |
| 64 | pthread_setname_np(mThread.native_handle(), "BckgrndExec LP"); |
| 65 | } |
Vishnu Nair | 34eb9ca | 2021-11-18 15:23:23 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | BackgroundExecutor::~BackgroundExecutor() { |
Alec Mouri | 8d7d0f4 | 2022-05-10 23:33:40 +0000 | [diff] [blame] | 69 | mDone = true; |
| 70 | LOG_ALWAYS_FATAL_IF(sem_post(&mSemaphore), "sem_post failed"); |
Vishnu Nair | 34eb9ca | 2021-11-18 15:23:23 -0800 | [diff] [blame] | 71 | if (mThread.joinable()) { |
| 72 | mThread.join(); |
Alec Mouri | 8d7d0f4 | 2022-05-10 23:33:40 +0000 | [diff] [blame] | 73 | LOG_ALWAYS_FATAL_IF(sem_destroy(&mSemaphore), "sem_destroy failed"); |
Vishnu Nair | 34eb9ca | 2021-11-18 15:23:23 -0800 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
Alec Mouri | 8d7d0f4 | 2022-05-10 23:33:40 +0000 | [diff] [blame] | 77 | void BackgroundExecutor::sendCallbacks(Callbacks&& tasks) { |
Patrick Williams | 13310b8 | 2023-05-17 14:40:18 -0500 | [diff] [blame] | 78 | mCallbacksQueue.push(std::move(tasks)); |
Alec Mouri | 8d7d0f4 | 2022-05-10 23:33:40 +0000 | [diff] [blame] | 79 | LOG_ALWAYS_FATAL_IF(sem_post(&mSemaphore), "sem_post failed"); |
Vishnu Nair | 34eb9ca | 2021-11-18 15:23:23 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Patrick Williams | acd2258 | 2023-07-12 13:47:28 -0500 | [diff] [blame] | 82 | void BackgroundExecutor::flushQueue() { |
| 83 | std::mutex mutex; |
| 84 | std::condition_variable cv; |
| 85 | bool flushComplete = false; |
| 86 | sendCallbacks({[&]() { |
| 87 | std::scoped_lock lock{mutex}; |
| 88 | flushComplete = true; |
| 89 | cv.notify_one(); |
| 90 | }}); |
| 91 | std::unique_lock<std::mutex> lock{mutex}; |
| 92 | cv.wait(lock, [&]() { return flushComplete; }); |
| 93 | } |
| 94 | |
Patrick Williams | 13310b8 | 2023-05-17 14:40:18 -0500 | [diff] [blame] | 95 | } // namespace android |