blob: cb5401cb86e8d628123d2fef999d5da3c1133b05 [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
17#include <sys/sysinfo.h>
John Reck21be43e2014-08-14 10:25:16 -070018#if defined(HAVE_PTHREADS)
19#include <sys/resource.h>
20#endif
Romain Guy5dc7fa72013-03-11 20:48:31 -070021
Chris Craik05f3d6e2014-06-02 16:27:04 -070022#include "TaskManager.h"
Romain Guy5dc7fa72013-03-11 20:48:31 -070023#include "Task.h"
24#include "TaskProcessor.h"
Chris Craik05f3d6e2014-06-02 16:27:04 -070025#include "utils/MathUtils.h"
Romain Guy5dc7fa72013-03-11 20:48:31 -070026
27namespace android {
28namespace uirenderer {
29
30///////////////////////////////////////////////////////////////////////////////
31// Manager
32///////////////////////////////////////////////////////////////////////////////
33
34TaskManager::TaskManager() {
35 // Get the number of available CPUs. This value does not change over time.
Romain Guy0a8c51b2013-08-21 17:35:38 -070036 int cpuCount = sysconf(_SC_NPROCESSORS_CONF);
Romain Guy5dc7fa72013-03-11 20:48:31 -070037
Chris Craik05f3d6e2014-06-02 16:27:04 -070038 int workerCount = MathUtils::max(1, cpuCount / 2);
39 for (int i = 0; i < workerCount; i++) {
Romain Guy5dc7fa72013-03-11 20:48:31 -070040 String8 name;
41 name.appendFormat("hwuiTask%d", i + 1);
42 mThreads.add(new WorkerThread(name));
43 }
44}
45
46TaskManager::~TaskManager() {
47 for (size_t i = 0; i < mThreads.size(); i++) {
48 mThreads[i]->exit();
49 }
50}
51
52bool TaskManager::canRunTasks() const {
53 return mThreads.size() > 0;
54}
55
Romain Guyc5cbee72013-03-20 19:15:02 -070056void TaskManager::stop() {
57 for (size_t i = 0; i < mThreads.size(); i++) {
58 mThreads[i]->exit();
59 }
60}
61
Romain Guy5dc7fa72013-03-11 20:48:31 -070062bool 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 Reck21be43e2014-08-14 10:25:16 -070085status_t TaskManager::WorkerThread::readyToRun() {
86#if defined(HAVE_PTHREADS)
87 setpriority(PRIO_PROCESS, 0, PRIORITY_FOREGROUND);
88#endif
89 return NO_ERROR;
90}
91
Romain Guy5dc7fa72013-03-11 20:48:31 -070092bool TaskManager::WorkerThread::threadLoop() {
93 mSignal.wait();
94 Vector<TaskWrapper> tasks;
95 {
96 Mutex::Autolock l(mLock);
97 tasks = mTasks;
98 mTasks.clear();
99 }
100
101 for (size_t i = 0; i < tasks.size(); i++) {
102 const TaskWrapper& task = tasks.itemAt(i);
103 task.mProcessor->process(task.mTask);
104 }
105
106 return true;
107}
108
109bool TaskManager::WorkerThread::addTask(TaskWrapper task) {
110 if (!isRunning()) {
111 run(mName.string(), PRIORITY_DEFAULT);
112 }
113
114 Mutex::Autolock l(mLock);
115 ssize_t index = mTasks.add(task);
116 mSignal.signal();
117
118 return index >= 0;
119}
120
121size_t TaskManager::WorkerThread::getTaskCount() const {
122 Mutex::Autolock l(mLock);
123 return mTasks.size();
124}
125
126void TaskManager::WorkerThread::exit() {
127 {
128 Mutex::Autolock l(mLock);
129 mTasks.clear();
130 }
131 requestExit();
132 mSignal.signal();
133}
134
135}; // namespace uirenderer
136}; // namespace android