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 <Tracing/LocklessStack.h> |
Vishnu Nair | 03ccbd6 | 2021-12-01 17:21:16 -0800 | [diff] [blame] | 20 | #include <android-base/thread_annotations.h> |
Alec Mouri | 8d7d0f4 | 2022-05-10 23:33:40 +0000 | [diff] [blame^] | 21 | #include <ftl/small_vector.h> |
| 22 | #include <semaphore.h> |
Vishnu Nair | 34eb9ca | 2021-11-18 15:23:23 -0800 | [diff] [blame] | 23 | #include <utils/Singleton.h> |
Vishnu Nair | 34eb9ca | 2021-11-18 15:23:23 -0800 | [diff] [blame] | 24 | #include <mutex> |
| 25 | #include <queue> |
| 26 | #include <thread> |
| 27 | |
| 28 | namespace android { |
| 29 | |
| 30 | // Executes tasks off the main thread. |
| 31 | class BackgroundExecutor : public Singleton<BackgroundExecutor> { |
| 32 | public: |
| 33 | BackgroundExecutor(); |
| 34 | ~BackgroundExecutor(); |
Alec Mouri | 8d7d0f4 | 2022-05-10 23:33:40 +0000 | [diff] [blame^] | 35 | using Callbacks = ftl::SmallVector<std::function<void()>, 10>; |
| 36 | // Queues callbacks onto a work queue to be executed by a background thread. |
| 37 | // Note that this is not thread-safe - a single producer is assumed. |
| 38 | void sendCallbacks(Callbacks&& tasks); |
Vishnu Nair | 34eb9ca | 2021-11-18 15:23:23 -0800 | [diff] [blame] | 39 | |
| 40 | private: |
Alec Mouri | 8d7d0f4 | 2022-05-10 23:33:40 +0000 | [diff] [blame^] | 41 | sem_t mSemaphore; |
| 42 | std::atomic_bool mDone = false; |
| 43 | |
| 44 | // Sequence number for work items. |
| 45 | // Work items are batched by sequence number. Work items for earlier sequence numbers are |
| 46 | // executed first. Work items with the same sequence number are executed in the same order they |
| 47 | // were added to the stack (meaning the stack must reverse the order after popping from the |
| 48 | // queue) |
| 49 | int32_t mSequence = 0; |
| 50 | struct Work { |
| 51 | int32_t sequence = 0; |
| 52 | Callbacks tasks; |
| 53 | }; |
| 54 | LocklessStack<Work> mWorks; |
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 |