Jamie Gennis | d170075 | 2013-10-14 12:22:52 -0700 | [diff] [blame] | 1 | /* |
| 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 Pique | 755e319 | 2018-01-31 16:46:15 -0800 | [diff] [blame^] | 17 | #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 Gennis | d170075 | 2013-10-14 12:22:52 -0700 | [diff] [blame] | 25 | #include "EventControlThread.h" |
Jamie Gennis | d170075 | 2013-10-14 12:22:52 -0700 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | |
Lloyd Pique | 755e319 | 2018-01-31 16:46:15 -0800 | [diff] [blame^] | 29 | EventControlThread::EventControlThread(EventControlThread::SetVSyncEnabledFunction function) |
| 30 | : mSetVSyncEnabled(function) { |
| 31 | pthread_setname_np(mThread.native_handle(), "EventControlThread"); |
Jamie Gennis | d170075 | 2013-10-14 12:22:52 -0700 | [diff] [blame] | 32 | |
Lloyd Pique | 755e319 | 2018-01-31 16:46:15 -0800 | [diff] [blame^] | 33 | pid_t tid = pthread_gettid_np(mThread.native_handle()); |
| 34 | setpriority(PRIO_PROCESS, tid, ANDROID_PRIORITY_URGENT_DISPLAY); |
| 35 | set_sched_policy(tid, SP_FOREGROUND); |
Jamie Gennis | d170075 | 2013-10-14 12:22:52 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Lloyd Pique | 755e319 | 2018-01-31 16:46:15 -0800 | [diff] [blame^] | 38 | EventControlThread::~EventControlThread() { |
| 39 | { |
| 40 | std::lock_guard<std::mutex> lock(mMutex); |
| 41 | mKeepRunning = false; |
| 42 | mCondition.notify_all(); |
Jamie Gennis | d170075 | 2013-10-14 12:22:52 -0700 | [diff] [blame] | 43 | } |
Lloyd Pique | 755e319 | 2018-01-31 16:46:15 -0800 | [diff] [blame^] | 44 | mThread.join(); |
| 45 | } |
Jamie Gennis | d170075 | 2013-10-14 12:22:52 -0700 | [diff] [blame] | 46 | |
Lloyd Pique | 755e319 | 2018-01-31 16:46:15 -0800 | [diff] [blame^] | 47 | void EventControlThread::setVsyncEnabled(bool enabled) { |
| 48 | std::lock_guard<std::mutex> lock(mMutex); |
| 49 | mVsyncEnabled = enabled; |
| 50 | mCondition.notify_all(); |
| 51 | } |
| 52 | |
| 53 | // Unfortunately std::unique_lock gives warnings with -Wthread-safety |
| 54 | void EventControlThread::threadMain() NO_THREAD_SAFETY_ANALYSIS { |
| 55 | auto keepRunning = true; |
| 56 | auto currentVsyncEnabled = false; |
| 57 | |
| 58 | while (keepRunning) { |
| 59 | mSetVSyncEnabled(currentVsyncEnabled); |
| 60 | |
| 61 | std::unique_lock<std::mutex> lock(mMutex); |
| 62 | mCondition.wait(lock, [this, currentVsyncEnabled, keepRunning]() NO_THREAD_SAFETY_ANALYSIS { |
| 63 | return currentVsyncEnabled != mVsyncEnabled || keepRunning != mKeepRunning; |
| 64 | }); |
| 65 | currentVsyncEnabled = mVsyncEnabled; |
| 66 | keepRunning = mKeepRunning; |
| 67 | } |
Jamie Gennis | d170075 | 2013-10-14 12:22:52 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | } // namespace android |