1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker * Copyright 2021 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker *
4*38e8c45fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker *
8*38e8c45fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker *
10*38e8c45fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker */
16*38e8c45fSAndroid Build Coastguard Worker
17*38e8c45fSAndroid Build Coastguard Worker //#define LOG_NDEBUG 0
18*38e8c45fSAndroid Build Coastguard Worker #undef LOG_TAG
19*38e8c45fSAndroid Build Coastguard Worker #define LOG_TAG "BackgroundExecutor"
20*38e8c45fSAndroid Build Coastguard Worker #define ATRACE_TAG ATRACE_TAG_GRAPHICS
21*38e8c45fSAndroid Build Coastguard Worker
22*38e8c45fSAndroid Build Coastguard Worker #include <processgroup/sched_policy.h>
23*38e8c45fSAndroid Build Coastguard Worker #include <pthread.h>
24*38e8c45fSAndroid Build Coastguard Worker #include <sched.h>
25*38e8c45fSAndroid Build Coastguard Worker #include <utils/Log.h>
26*38e8c45fSAndroid Build Coastguard Worker #include <mutex>
27*38e8c45fSAndroid Build Coastguard Worker
28*38e8c45fSAndroid Build Coastguard Worker #include "BackgroundExecutor.h"
29*38e8c45fSAndroid Build Coastguard Worker
30*38e8c45fSAndroid Build Coastguard Worker namespace android {
31*38e8c45fSAndroid Build Coastguard Worker
32*38e8c45fSAndroid Build Coastguard Worker namespace {
33*38e8c45fSAndroid Build Coastguard Worker
set_thread_priority(bool highPriority)34*38e8c45fSAndroid Build Coastguard Worker void set_thread_priority(bool highPriority) {
35*38e8c45fSAndroid Build Coastguard Worker set_sched_policy(0, highPriority ? SP_FOREGROUND : SP_BACKGROUND);
36*38e8c45fSAndroid Build Coastguard Worker struct sched_param param = {0};
37*38e8c45fSAndroid Build Coastguard Worker param.sched_priority = highPriority ? 2 : 0 /* must be 0 for non-RT */;
38*38e8c45fSAndroid Build Coastguard Worker sched_setscheduler(gettid(), highPriority ? SCHED_FIFO : SCHED_NORMAL, ¶m);
39*38e8c45fSAndroid Build Coastguard Worker }
40*38e8c45fSAndroid Build Coastguard Worker
41*38e8c45fSAndroid Build Coastguard Worker } // anonymous namespace
42*38e8c45fSAndroid Build Coastguard Worker
BackgroundExecutor(bool highPriority)43*38e8c45fSAndroid Build Coastguard Worker BackgroundExecutor::BackgroundExecutor(bool highPriority) {
44*38e8c45fSAndroid Build Coastguard Worker // mSemaphore must be initialized before any calls to
45*38e8c45fSAndroid Build Coastguard Worker // BackgroundExecutor::sendCallbacks. For this reason, we initialize it
46*38e8c45fSAndroid Build Coastguard Worker // within the constructor instead of within mThread.
47*38e8c45fSAndroid Build Coastguard Worker LOG_ALWAYS_FATAL_IF(sem_init(&mSemaphore, 0, 0), "sem_init failed");
48*38e8c45fSAndroid Build Coastguard Worker mThread = std::thread([&, highPriority]() {
49*38e8c45fSAndroid Build Coastguard Worker set_thread_priority(highPriority);
50*38e8c45fSAndroid Build Coastguard Worker while (!mDone) {
51*38e8c45fSAndroid Build Coastguard Worker LOG_ALWAYS_FATAL_IF(sem_wait(&mSemaphore), "sem_wait failed (%d)", errno);
52*38e8c45fSAndroid Build Coastguard Worker auto callbacks = mCallbacksQueue.pop();
53*38e8c45fSAndroid Build Coastguard Worker if (!callbacks) {
54*38e8c45fSAndroid Build Coastguard Worker continue;
55*38e8c45fSAndroid Build Coastguard Worker }
56*38e8c45fSAndroid Build Coastguard Worker for (auto& callback : *callbacks) {
57*38e8c45fSAndroid Build Coastguard Worker callback();
58*38e8c45fSAndroid Build Coastguard Worker }
59*38e8c45fSAndroid Build Coastguard Worker }
60*38e8c45fSAndroid Build Coastguard Worker });
61*38e8c45fSAndroid Build Coastguard Worker if (highPriority) {
62*38e8c45fSAndroid Build Coastguard Worker pthread_setname_np(mThread.native_handle(), "BckgrndExec HP");
63*38e8c45fSAndroid Build Coastguard Worker } else {
64*38e8c45fSAndroid Build Coastguard Worker pthread_setname_np(mThread.native_handle(), "BckgrndExec LP");
65*38e8c45fSAndroid Build Coastguard Worker }
66*38e8c45fSAndroid Build Coastguard Worker }
67*38e8c45fSAndroid Build Coastguard Worker
~BackgroundExecutor()68*38e8c45fSAndroid Build Coastguard Worker BackgroundExecutor::~BackgroundExecutor() {
69*38e8c45fSAndroid Build Coastguard Worker mDone = true;
70*38e8c45fSAndroid Build Coastguard Worker LOG_ALWAYS_FATAL_IF(sem_post(&mSemaphore), "sem_post failed");
71*38e8c45fSAndroid Build Coastguard Worker if (mThread.joinable()) {
72*38e8c45fSAndroid Build Coastguard Worker mThread.join();
73*38e8c45fSAndroid Build Coastguard Worker LOG_ALWAYS_FATAL_IF(sem_destroy(&mSemaphore), "sem_destroy failed");
74*38e8c45fSAndroid Build Coastguard Worker }
75*38e8c45fSAndroid Build Coastguard Worker }
76*38e8c45fSAndroid Build Coastguard Worker
sendCallbacks(Callbacks && tasks)77*38e8c45fSAndroid Build Coastguard Worker void BackgroundExecutor::sendCallbacks(Callbacks&& tasks) {
78*38e8c45fSAndroid Build Coastguard Worker mCallbacksQueue.push(std::move(tasks));
79*38e8c45fSAndroid Build Coastguard Worker LOG_ALWAYS_FATAL_IF(sem_post(&mSemaphore), "sem_post failed");
80*38e8c45fSAndroid Build Coastguard Worker }
81*38e8c45fSAndroid Build Coastguard Worker
flushQueue()82*38e8c45fSAndroid Build Coastguard Worker void BackgroundExecutor::flushQueue() {
83*38e8c45fSAndroid Build Coastguard Worker std::mutex mutex;
84*38e8c45fSAndroid Build Coastguard Worker std::condition_variable cv;
85*38e8c45fSAndroid Build Coastguard Worker bool flushComplete = false;
86*38e8c45fSAndroid Build Coastguard Worker sendCallbacks({[&]() {
87*38e8c45fSAndroid Build Coastguard Worker std::scoped_lock lock{mutex};
88*38e8c45fSAndroid Build Coastguard Worker flushComplete = true;
89*38e8c45fSAndroid Build Coastguard Worker cv.notify_one();
90*38e8c45fSAndroid Build Coastguard Worker }});
91*38e8c45fSAndroid Build Coastguard Worker std::unique_lock<std::mutex> lock{mutex};
92*38e8c45fSAndroid Build Coastguard Worker cv.wait(lock, [&]() { return flushComplete; });
93*38e8c45fSAndroid Build Coastguard Worker }
94*38e8c45fSAndroid Build Coastguard Worker
95*38e8c45fSAndroid Build Coastguard Worker } // namespace android
96