blob: 85a7f82a3c856dfeef2166c718781dfe2a2d6fcb [file] [log] [blame]
Jamie Gennisd1700752013-10-14 12:22:52 -07001/*
2 * Copyright (C) 2013 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
Lloyd Pique755e3192018-01-31 16:46:15 -080017#include <pthread.h>
18#include <sched.h>
19#include <sys/resource.h>
20
21#include <cutils/sched_policy.h>
22#include <log/log.h>
23#include <system/thread_defs.h>
24
Jamie Gennisd1700752013-10-14 12:22:52 -070025#include "EventControlThread.h"
Jamie Gennisd1700752013-10-14 12:22:52 -070026
27namespace android {
28
Lloyd Pique379adc12018-01-22 17:31:47 -080029EventControlThread::~EventControlThread() = default;
30
31namespace impl {
32
Lloyd Pique755e3192018-01-31 16:46:15 -080033EventControlThread::EventControlThread(EventControlThread::SetVSyncEnabledFunction function)
Dominik Laskowski98041832019-08-01 18:35:59 -070034 : mSetVSyncEnabled(std::move(function)) {
Lloyd Pique755e3192018-01-31 16:46:15 -080035 pthread_setname_np(mThread.native_handle(), "EventControlThread");
Jamie Gennisd1700752013-10-14 12:22:52 -070036
Lloyd Pique755e3192018-01-31 16:46:15 -080037 pid_t tid = pthread_gettid_np(mThread.native_handle());
38 setpriority(PRIO_PROCESS, tid, ANDROID_PRIORITY_URGENT_DISPLAY);
39 set_sched_policy(tid, SP_FOREGROUND);
Jamie Gennisd1700752013-10-14 12:22:52 -070040}
41
Lloyd Pique755e3192018-01-31 16:46:15 -080042EventControlThread::~EventControlThread() {
43 {
44 std::lock_guard<std::mutex> lock(mMutex);
45 mKeepRunning = false;
46 mCondition.notify_all();
Jamie Gennisd1700752013-10-14 12:22:52 -070047 }
Lloyd Pique755e3192018-01-31 16:46:15 -080048 mThread.join();
49}
Jamie Gennisd1700752013-10-14 12:22:52 -070050
Lloyd Pique755e3192018-01-31 16:46:15 -080051void EventControlThread::setVsyncEnabled(bool enabled) {
52 std::lock_guard<std::mutex> lock(mMutex);
53 mVsyncEnabled = enabled;
54 mCondition.notify_all();
55}
56
57// Unfortunately std::unique_lock gives warnings with -Wthread-safety
58void EventControlThread::threadMain() NO_THREAD_SAFETY_ANALYSIS {
59 auto keepRunning = true;
60 auto currentVsyncEnabled = false;
61
62 while (keepRunning) {
63 mSetVSyncEnabled(currentVsyncEnabled);
64
65 std::unique_lock<std::mutex> lock(mMutex);
66 mCondition.wait(lock, [this, currentVsyncEnabled, keepRunning]() NO_THREAD_SAFETY_ANALYSIS {
67 return currentVsyncEnabled != mVsyncEnabled || keepRunning != mKeepRunning;
68 });
69 currentVsyncEnabled = mVsyncEnabled;
70 keepRunning = mKeepRunning;
71 }
Jamie Gennisd1700752013-10-14 12:22:52 -070072}
73
Lloyd Pique379adc12018-01-22 17:31:47 -080074} // namespace impl
Jamie Gennisd1700752013-10-14 12:22:52 -070075} // namespace android