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 19 #include <Tracing/LocklessStack.h> 20 #include <android-base/thread_annotations.h> 21 #include <ftl/small_vector.h> 22 #include <semaphore.h> 23 #include <utils/Singleton.h> 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(); 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); 39 40 private: 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; 55 std::thread mThread; 56 }; 57 58 } // namespace android 59