Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -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 | |
John Reck | 21be43e | 2014-08-14 10:25:16 -0700 | [diff] [blame] | 17 | #include <sys/resource.h> |
Yabin Cui | 1610486 | 2015-01-26 19:43:58 -0800 | [diff] [blame] | 18 | #include <sys/sysinfo.h> |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 19 | |
| 20 | #include "Task.h" |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 21 | #include "TaskManager.h" |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 22 | #include "TaskProcessor.h" |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 23 | #include "utils/MathUtils.h" |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 24 | |
| 25 | namespace android { |
| 26 | namespace uirenderer { |
| 27 | |
| 28 | /////////////////////////////////////////////////////////////////////////////// |
| 29 | // Manager |
| 30 | /////////////////////////////////////////////////////////////////////////////// |
| 31 | |
| 32 | TaskManager::TaskManager() { |
| 33 | // Get the number of available CPUs. This value does not change over time. |
Romain Guy | 0a8c51b | 2013-08-21 17:35:38 -0700 | [diff] [blame] | 34 | int cpuCount = sysconf(_SC_NPROCESSORS_CONF); |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 35 | |
John Reck | c452654 | 2015-07-07 12:17:50 -0700 | [diff] [blame] | 36 | // Really no point in making more than 2 of these worker threads, but |
| 37 | // we do want to limit ourselves to 1 worker thread on dual-core devices. |
| 38 | int workerCount = cpuCount > 2 ? 2 : 1; |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 39 | for (int i = 0; i < workerCount; i++) { |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 40 | String8 name; |
| 41 | name.appendFormat("hwuiTask%d", i + 1); |
John Reck | 272a685 | 2015-07-29 16:48:58 -0700 | [diff] [blame] | 42 | mThreads.push_back(new WorkerThread(name)); |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | |
| 46 | TaskManager::~TaskManager() { |
| 47 | for (size_t i = 0; i < mThreads.size(); i++) { |
| 48 | mThreads[i]->exit(); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | bool TaskManager::canRunTasks() const { |
| 53 | return mThreads.size() > 0; |
| 54 | } |
| 55 | |
Romain Guy | c5cbee7 | 2013-03-20 19:15:02 -0700 | [diff] [blame] | 56 | void TaskManager::stop() { |
| 57 | for (size_t i = 0; i < mThreads.size(); i++) { |
| 58 | mThreads[i]->exit(); |
| 59 | } |
| 60 | } |
| 61 | |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 62 | bool TaskManager::addTaskBase(const sp<TaskBase>& task, const sp<TaskProcessorBase>& processor) { |
| 63 | if (mThreads.size() > 0) { |
| 64 | TaskWrapper wrapper(task, processor); |
| 65 | |
| 66 | size_t minQueueSize = INT_MAX; |
| 67 | sp<WorkerThread> thread; |
| 68 | |
| 69 | for (size_t i = 0; i < mThreads.size(); i++) { |
| 70 | if (mThreads[i]->getTaskCount() < minQueueSize) { |
| 71 | thread = mThreads[i]; |
| 72 | minQueueSize = mThreads[i]->getTaskCount(); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return thread->addTask(wrapper); |
| 77 | } |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | /////////////////////////////////////////////////////////////////////////////// |
| 82 | // Thread |
| 83 | /////////////////////////////////////////////////////////////////////////////// |
| 84 | |
John Reck | 21be43e | 2014-08-14 10:25:16 -0700 | [diff] [blame] | 85 | status_t TaskManager::WorkerThread::readyToRun() { |
John Reck | 21be43e | 2014-08-14 10:25:16 -0700 | [diff] [blame] | 86 | setpriority(PRIO_PROCESS, 0, PRIORITY_FOREGROUND); |
John Reck | 21be43e | 2014-08-14 10:25:16 -0700 | [diff] [blame] | 87 | return NO_ERROR; |
| 88 | } |
| 89 | |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 90 | bool TaskManager::WorkerThread::threadLoop() { |
| 91 | mSignal.wait(); |
John Reck | 272a685 | 2015-07-29 16:48:58 -0700 | [diff] [blame] | 92 | std::vector<TaskWrapper> tasks; |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 93 | { |
| 94 | Mutex::Autolock l(mLock); |
John Reck | 272a685 | 2015-07-29 16:48:58 -0700 | [diff] [blame] | 95 | tasks.swap(mTasks); |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | for (size_t i = 0; i < tasks.size(); i++) { |
John Reck | 272a685 | 2015-07-29 16:48:58 -0700 | [diff] [blame] | 99 | const TaskWrapper& task = tasks[i]; |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 100 | task.mProcessor->process(task.mTask); |
| 101 | } |
| 102 | |
| 103 | return true; |
| 104 | } |
| 105 | |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 106 | bool TaskManager::WorkerThread::addTask(const TaskWrapper& task) { |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 107 | if (!isRunning()) { |
| 108 | run(mName.string(), PRIORITY_DEFAULT); |
Sangkyu Lee | c3c58e0 | 2015-01-12 09:51:53 +0900 | [diff] [blame] | 109 | } else if (exitPending()) { |
| 110 | return false; |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 111 | } |
| 112 | |
John Reck | 815f2a0 | 2015-03-24 12:16:37 -0700 | [diff] [blame] | 113 | { |
| 114 | Mutex::Autolock l(mLock); |
John Reck | 272a685 | 2015-07-29 16:48:58 -0700 | [diff] [blame] | 115 | mTasks.push_back(task); |
John Reck | 815f2a0 | 2015-03-24 12:16:37 -0700 | [diff] [blame] | 116 | } |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 117 | mSignal.signal(); |
| 118 | |
John Reck | 272a685 | 2015-07-29 16:48:58 -0700 | [diff] [blame] | 119 | return true; |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | size_t TaskManager::WorkerThread::getTaskCount() const { |
| 123 | Mutex::Autolock l(mLock); |
| 124 | return mTasks.size(); |
| 125 | } |
| 126 | |
| 127 | void TaskManager::WorkerThread::exit() { |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 128 | requestExit(); |
| 129 | mSignal.signal(); |
| 130 | } |
| 131 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 132 | }; // namespace uirenderer |
| 133 | }; // namespace android |