blob: 6f6d305280cc5f019e731e65fab00812edb14da8 [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
19#include <utils/Singleton.h>
20#include <condition_variable>
21#include <mutex>
22#include <queue>
23#include <thread>
24
25namespace android {
26
27// Executes tasks off the main thread.
28class BackgroundExecutor : public Singleton<BackgroundExecutor> {
29public:
30 BackgroundExecutor();
31 ~BackgroundExecutor();
32 void execute(std::function<void()>);
33
34private:
35 std::mutex mMutex;
Jaineel Mehtaac331c52021-11-29 21:38:10 +000036 std::condition_variable mWorkAvailableCv;
Vishnu Nair34eb9ca2021-11-18 15:23:23 -080037 std::thread mThread;
Jaineel Mehtaac331c52021-11-29 21:38:10 +000038 bool mDone = false;
39 std::vector<std::function<void()>> mTasks;
Vishnu Nair34eb9ca2021-11-18 15:23:23 -080040};
41
42} // namespace android