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 //#define LOG_NDEBUG 0
18 #undef LOG_TAG
19 #define LOG_TAG "BackgroundExecutor"
20 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
21 
22 #include <processgroup/sched_policy.h>
23 #include <pthread.h>
24 #include <sched.h>
25 #include <utils/Log.h>
26 #include <mutex>
27 
28 #include "BackgroundExecutor.h"
29 
30 namespace android {
31 
32 namespace {
33 
set_thread_priority(bool highPriority)34 void 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 
BackgroundExecutor(bool highPriority)43 BackgroundExecutor::BackgroundExecutor(bool highPriority) {
44     // 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");
48     mThread = std::thread([&, highPriority]() {
49         set_thread_priority(highPriority);
50         while (!mDone) {
51             LOG_ALWAYS_FATAL_IF(sem_wait(&mSemaphore), "sem_wait failed (%d)", errno);
52             auto callbacks = mCallbacksQueue.pop();
53             if (!callbacks) {
54                 continue;
55             }
56             for (auto& callback : *callbacks) {
57                 callback();
58             }
59         }
60     });
61     if (highPriority) {
62         pthread_setname_np(mThread.native_handle(), "BckgrndExec HP");
63     } else {
64         pthread_setname_np(mThread.native_handle(), "BckgrndExec LP");
65     }
66 }
67 
~BackgroundExecutor()68 BackgroundExecutor::~BackgroundExecutor() {
69     mDone = true;
70     LOG_ALWAYS_FATAL_IF(sem_post(&mSemaphore), "sem_post failed");
71     if (mThread.joinable()) {
72         mThread.join();
73         LOG_ALWAYS_FATAL_IF(sem_destroy(&mSemaphore), "sem_destroy failed");
74     }
75 }
76 
sendCallbacks(Callbacks && tasks)77 void BackgroundExecutor::sendCallbacks(Callbacks&& tasks) {
78     mCallbacksQueue.push(std::move(tasks));
79     LOG_ALWAYS_FATAL_IF(sem_post(&mSemaphore), "sem_post failed");
80 }
81 
flushQueue()82 void 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 
95 } // namespace android
96