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 | #pragma once |
| 18 | |
Alec Mouri | 8d7d0f4 | 2022-05-10 23:33:40 +0000 | [diff] [blame] | 19 | #include <ftl/small_vector.h> |
| 20 | #include <semaphore.h> |
Vishnu Nair | 34eb9ca | 2021-11-18 15:23:23 -0800 | [diff] [blame] | 21 | #include <thread> |
| 22 | |
Patrick Williams | 13310b8 | 2023-05-17 14:40:18 -0500 | [diff] [blame] | 23 | #include "LocklessQueue.h" |
| 24 | |
Vishnu Nair | 34eb9ca | 2021-11-18 15:23:23 -0800 | [diff] [blame] | 25 | namespace android { |
| 26 | |
| 27 | // Executes tasks off the main thread. |
Pascal Mütschard | d0afeb7 | 2024-03-13 12:08:26 +0100 | [diff] [blame] | 28 | class BackgroundExecutor { |
Vishnu Nair | 34eb9ca | 2021-11-18 15:23:23 -0800 | [diff] [blame] | 29 | public: |
Vishnu Nair | 34eb9ca | 2021-11-18 15:23:23 -0800 | [diff] [blame] | 30 | ~BackgroundExecutor(); |
Pascal Mütschard | d0afeb7 | 2024-03-13 12:08:26 +0100 | [diff] [blame] | 31 | |
| 32 | static BackgroundExecutor& getInstance() { |
| 33 | static BackgroundExecutor instance(true); |
| 34 | return instance; |
| 35 | } |
| 36 | |
| 37 | static BackgroundExecutor& getLowPriorityInstance() { |
| 38 | static BackgroundExecutor instance(false); |
| 39 | return instance; |
| 40 | } |
| 41 | |
Alec Mouri | 8d7d0f4 | 2022-05-10 23:33:40 +0000 | [diff] [blame] | 42 | using Callbacks = ftl::SmallVector<std::function<void()>, 10>; |
| 43 | // Queues callbacks onto a work queue to be executed by a background thread. |
Patrick Williams | 13310b8 | 2023-05-17 14:40:18 -0500 | [diff] [blame] | 44 | // This is safe to call from multiple threads. |
Alec Mouri | 8d7d0f4 | 2022-05-10 23:33:40 +0000 | [diff] [blame] | 45 | void sendCallbacks(Callbacks&& tasks); |
Patrick Williams | acd2258 | 2023-07-12 13:47:28 -0500 | [diff] [blame] | 46 | void flushQueue(); |
Vishnu Nair | 34eb9ca | 2021-11-18 15:23:23 -0800 | [diff] [blame] | 47 | |
| 48 | private: |
Pascal Mütschard | d0afeb7 | 2024-03-13 12:08:26 +0100 | [diff] [blame] | 49 | BackgroundExecutor(bool highPriority); |
| 50 | |
Alec Mouri | 8d7d0f4 | 2022-05-10 23:33:40 +0000 | [diff] [blame] | 51 | sem_t mSemaphore; |
| 52 | std::atomic_bool mDone = false; |
| 53 | |
Patrick Williams | 13310b8 | 2023-05-17 14:40:18 -0500 | [diff] [blame] | 54 | LocklessQueue<Callbacks> mCallbacksQueue; |
Vishnu Nair | 34eb9ca | 2021-11-18 15:23:23 -0800 | [diff] [blame] | 55 | std::thread mThread; |
Vishnu Nair | 34eb9ca | 2021-11-18 15:23:23 -0800 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | } // namespace android |