blob: c69b2fd5638382d6c0c9f94263a5b4c177bb6b7e [file] [log] [blame]
Romain Guy5dc7fa72013-03-11 20:48:31 -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
John Reck21be43e2014-08-14 10:25:16 -070017#include <sys/resource.h>
Yabin Cui16104862015-01-26 19:43:58 -080018#include <sys/sysinfo.h>
Romain Guy5dc7fa72013-03-11 20:48:31 -070019
Chris Craik05f3d6e2014-06-02 16:27:04 -070020#include "TaskManager.h"
Romain Guy5dc7fa72013-03-11 20:48:31 -070021#include "Task.h"
22#include "TaskProcessor.h"
Chris Craik05f3d6e2014-06-02 16:27:04 -070023#include "utils/MathUtils.h"
Romain Guy5dc7fa72013-03-11 20:48:31 -070024
25namespace android {
26namespace uirenderer {
27
28///////////////////////////////////////////////////////////////////////////////
29// Manager
30///////////////////////////////////////////////////////////////////////////////
31
32TaskManager::TaskManager() {
33 // Get the number of available CPUs. This value does not change over time.
Romain Guy0a8c51b2013-08-21 17:35:38 -070034 int cpuCount = sysconf(_SC_NPROCESSORS_CONF);
Romain Guy5dc7fa72013-03-11 20:48:31 -070035
Chris Craik05f3d6e2014-06-02 16:27:04 -070036 int workerCount = MathUtils::max(1, cpuCount / 2);
37 for (int i = 0; i < workerCount; i++) {
Romain Guy5dc7fa72013-03-11 20:48:31 -070038 String8 name;
39 name.appendFormat("hwuiTask%d", i + 1);
40 mThreads.add(new WorkerThread(name));
41 }
42}
43
44TaskManager::~TaskManager() {
45 for (size_t i = 0; i < mThreads.size(); i++) {
46 mThreads[i]->exit();
47 }
48}
49
50bool TaskManager::canRunTasks() const {
51 return mThreads.size() > 0;
52}
53
Romain Guyc5cbee72013-03-20 19:15:02 -070054void TaskManager::stop() {
55 for (size_t i = 0; i < mThreads.size(); i++) {
56 mThreads[i]->exit();
57 }
58}
59
Romain Guy5dc7fa72013-03-11 20:48:31 -070060bool TaskManager::addTaskBase(const sp<TaskBase>& task, const sp<TaskProcessorBase>& processor) {
61 if (mThreads.size() > 0) {
62 TaskWrapper wrapper(task, processor);
63
64 size_t minQueueSize = INT_MAX;
65 sp<WorkerThread> thread;
66
67 for (size_t i = 0; i < mThreads.size(); i++) {
68 if (mThreads[i]->getTaskCount() < minQueueSize) {
69 thread = mThreads[i];
70 minQueueSize = mThreads[i]->getTaskCount();
71 }
72 }
73
74 return thread->addTask(wrapper);
75 }
76 return false;
77}
78
79///////////////////////////////////////////////////////////////////////////////
80// Thread
81///////////////////////////////////////////////////////////////////////////////
82
John Reck21be43e2014-08-14 10:25:16 -070083status_t TaskManager::WorkerThread::readyToRun() {
John Reck21be43e2014-08-14 10:25:16 -070084 setpriority(PRIO_PROCESS, 0, PRIORITY_FOREGROUND);
John Reck21be43e2014-08-14 10:25:16 -070085 return NO_ERROR;
86}
87
Romain Guy5dc7fa72013-03-11 20:48:31 -070088bool TaskManager::WorkerThread::threadLoop() {
89 mSignal.wait();
90 Vector<TaskWrapper> tasks;
91 {
92 Mutex::Autolock l(mLock);
93 tasks = mTasks;
94 mTasks.clear();
95 }
96
97 for (size_t i = 0; i < tasks.size(); i++) {
98 const TaskWrapper& task = tasks.itemAt(i);
99 task.mProcessor->process(task.mTask);
100 }
101
102 return true;
103}
104
105bool TaskManager::WorkerThread::addTask(TaskWrapper task) {
106 if (!isRunning()) {
107 run(mName.string(), PRIORITY_DEFAULT);
Sangkyu Leec3c58e02015-01-12 09:51:53 +0900108 } else if (exitPending()) {
109 return false;
Romain Guy5dc7fa72013-03-11 20:48:31 -0700110 }
111
112 Mutex::Autolock l(mLock);
113 ssize_t index = mTasks.add(task);
114 mSignal.signal();
115
116 return index >= 0;
117}
118
119size_t TaskManager::WorkerThread::getTaskCount() const {
120 Mutex::Autolock l(mLock);
121 return mTasks.size();
122}
123
124void TaskManager::WorkerThread::exit() {
Romain Guy5dc7fa72013-03-11 20:48:31 -0700125 requestExit();
126 mSignal.signal();
127}
128
129}; // namespace uirenderer
130}; // namespace android