blob: 3cef875a1dcfa607118ebd913173814aa83854f8 [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//#define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "BackgroundExecutor"
20#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
Pascal Mütschardd0afeb72024-03-13 12:08:26 +010022#include <processgroup/sched_policy.h>
23#include <pthread.h>
24#include <sched.h>
Alec Mouri8d7d0f42022-05-10 23:33:40 +000025#include <utils/Log.h>
Patrick Williamsacd22582023-07-12 13:47:28 -050026#include <mutex>
Alec Mouri8d7d0f42022-05-10 23:33:40 +000027
Vishnu Nair34eb9ca2021-11-18 15:23:23 -080028#include "BackgroundExecutor.h"
29
30namespace android {
31
Pascal Mütschardd0afeb72024-03-13 12:08:26 +010032namespace {
Vishnu Nair34eb9ca2021-11-18 15:23:23 -080033
Pascal Mütschardd0afeb72024-03-13 12:08:26 +010034void set_thread_priority(bool highPriority) {
35 set_sched_policy(0, highPriority ? SP_FOREGROUND : SP_BACKGROUND);
36 struct sched_param param = {0};
37 param.sched_priority = highPriority ? 2 : 0 /* must be 0 for non-RT */;
38 sched_setscheduler(gettid(), highPriority ? SCHED_FIFO : SCHED_NORMAL, &param);
39}
40
41} // anonymous namespace
42
43BackgroundExecutor::BackgroundExecutor(bool highPriority) {
Patrick Williams13310b82023-05-17 14:40:18 -050044 // mSemaphore must be initialized before any calls to
45 // BackgroundExecutor::sendCallbacks. For this reason, we initialize it
46 // within the constructor instead of within mThread.
47 LOG_ALWAYS_FATAL_IF(sem_init(&mSemaphore, 0, 0), "sem_init failed");
Pascal Mütschardd0afeb72024-03-13 12:08:26 +010048 mThread = std::thread([&, highPriority]() {
49 set_thread_priority(highPriority);
Alec Mouri8d7d0f42022-05-10 23:33:40 +000050 while (!mDone) {
51 LOG_ALWAYS_FATAL_IF(sem_wait(&mSemaphore), "sem_wait failed (%d)", errno);
Patrick Williams13310b82023-05-17 14:40:18 -050052 auto callbacks = mCallbacksQueue.pop();
53 if (!callbacks) {
54 continue;
Alec Mouri8d7d0f42022-05-10 23:33:40 +000055 }
Patrick Williams13310b82023-05-17 14:40:18 -050056 for (auto& callback : *callbacks) {
57 callback();
Vishnu Nair34eb9ca2021-11-18 15:23:23 -080058 }
59 }
60 });
Pascal Mütschardd0afeb72024-03-13 12:08:26 +010061 if (highPriority) {
62 pthread_setname_np(mThread.native_handle(), "BckgrndExec HP");
63 } else {
64 pthread_setname_np(mThread.native_handle(), "BckgrndExec LP");
65 }
Vishnu Nair34eb9ca2021-11-18 15:23:23 -080066}
67
68BackgroundExecutor::~BackgroundExecutor() {
Alec Mouri8d7d0f42022-05-10 23:33:40 +000069 mDone = true;
70 LOG_ALWAYS_FATAL_IF(sem_post(&mSemaphore), "sem_post failed");
Vishnu Nair34eb9ca2021-11-18 15:23:23 -080071 if (mThread.joinable()) {
72 mThread.join();
Alec Mouri8d7d0f42022-05-10 23:33:40 +000073 LOG_ALWAYS_FATAL_IF(sem_destroy(&mSemaphore), "sem_destroy failed");
Vishnu Nair34eb9ca2021-11-18 15:23:23 -080074 }
75}
76
Alec Mouri8d7d0f42022-05-10 23:33:40 +000077void BackgroundExecutor::sendCallbacks(Callbacks&& tasks) {
Patrick Williams13310b82023-05-17 14:40:18 -050078 mCallbacksQueue.push(std::move(tasks));
Alec Mouri8d7d0f42022-05-10 23:33:40 +000079 LOG_ALWAYS_FATAL_IF(sem_post(&mSemaphore), "sem_post failed");
Vishnu Nair34eb9ca2021-11-18 15:23:23 -080080}
81
Patrick Williamsacd22582023-07-12 13:47:28 -050082void BackgroundExecutor::flushQueue() {
83 std::mutex mutex;
84 std::condition_variable cv;
85 bool flushComplete = false;
86 sendCallbacks({[&]() {
87 std::scoped_lock lock{mutex};
88 flushComplete = true;
89 cv.notify_one();
90 }});
91 std::unique_lock<std::mutex> lock{mutex};
92 cv.wait(lock, [&]() { return flushComplete; });
93}
94
Patrick Williams13310b82023-05-17 14:40:18 -050095} // namespace android