blob: eeaf3bdd2e04ccfb81448fe0a225d7792bad01d2 [file] [log] [blame]
Vishnu Nair34eb9ca2021-11-18 15:23:23 -08001/*
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 Mouri8d7d0f42022-05-10 23:33:40 +000019#include <Tracing/LocklessStack.h>
Vishnu Nair03ccbd62021-12-01 17:21:16 -080020#include <android-base/thread_annotations.h>
Alec Mouri8d7d0f42022-05-10 23:33:40 +000021#include <ftl/small_vector.h>
22#include <semaphore.h>
Vishnu Nair34eb9ca2021-11-18 15:23:23 -080023#include <utils/Singleton.h>
Vishnu Nair34eb9ca2021-11-18 15:23:23 -080024#include <mutex>
25#include <queue>
26#include <thread>
27
28namespace android {
29
30// Executes tasks off the main thread.
31class BackgroundExecutor : public Singleton<BackgroundExecutor> {
32public:
33 BackgroundExecutor();
34 ~BackgroundExecutor();
Alec Mouri8d7d0f42022-05-10 23:33:40 +000035 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 Nair34eb9ca2021-11-18 15:23:23 -080039
40private:
Alec Mouri8d7d0f42022-05-10 23:33:40 +000041 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 Nair34eb9ca2021-11-18 15:23:23 -080055 std::thread mThread;
Vishnu Nair34eb9ca2021-11-18 15:23:23 -080056};
57
58} // namespace android