blob: 3d2b0d948c1314a783225c6425c3ad1b785de40f [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>
18
Chris Craik05f3d6e2014-06-02 16:27:04 -070019#include "TaskManager.h"
Romain Guy5dc7fa72013-03-11 20:48:31 -070020#include "Task.h"
21#include "TaskProcessor.h"
Chris Craik05f3d6e2014-06-02 16:27:04 -070022#include "utils/MathUtils.h"
Romain Guy5dc7fa72013-03-11 20:48:31 -070023
24namespace android {
25namespace uirenderer {
26
27///////////////////////////////////////////////////////////////////////////////
28// Manager
29///////////////////////////////////////////////////////////////////////////////
30
31TaskManager::TaskManager() {
32 // Get the number of available CPUs. This value does not change over time.
Romain Guy0a8c51b2013-08-21 17:35:38 -070033 int cpuCount = sysconf(_SC_NPROCESSORS_CONF);
Romain Guy5dc7fa72013-03-11 20:48:31 -070034
Chris Craik05f3d6e2014-06-02 16:27:04 -070035 int workerCount = MathUtils::max(1, cpuCount / 2);
36 for (int i = 0; i < workerCount; i++) {
Romain Guy5dc7fa72013-03-11 20:48:31 -070037 String8 name;
38 name.appendFormat("hwuiTask%d", i + 1);
39 mThreads.add(new WorkerThread(name));
40 }
41}
42
43TaskManager::~TaskManager() {
44 for (size_t i = 0; i < mThreads.size(); i++) {
45 mThreads[i]->exit();
46 }
47}
48
49bool TaskManager::canRunTasks() const {
50 return mThreads.size() > 0;
51}
52
Romain Guyc5cbee72013-03-20 19:15:02 -070053void TaskManager::stop() {
54 for (size_t i = 0; i < mThreads.size(); i++) {
55 mThreads[i]->exit();
56 }
57}
58
Romain Guy5dc7fa72013-03-11 20:48:31 -070059bool TaskManager::addTaskBase(const sp<TaskBase>& task, const sp<TaskProcessorBase>& processor) {
60 if (mThreads.size() > 0) {
61 TaskWrapper wrapper(task, processor);
62
63 size_t minQueueSize = INT_MAX;
64 sp<WorkerThread> thread;
65
66 for (size_t i = 0; i < mThreads.size(); i++) {
67 if (mThreads[i]->getTaskCount() < minQueueSize) {
68 thread = mThreads[i];
69 minQueueSize = mThreads[i]->getTaskCount();
70 }
71 }
72
73 return thread->addTask(wrapper);
74 }
75 return false;
76}
77
78///////////////////////////////////////////////////////////////////////////////
79// Thread
80///////////////////////////////////////////////////////////////////////////////
81
82bool TaskManager::WorkerThread::threadLoop() {
83 mSignal.wait();
84 Vector<TaskWrapper> tasks;
85 {
86 Mutex::Autolock l(mLock);
87 tasks = mTasks;
88 mTasks.clear();
89 }
90
91 for (size_t i = 0; i < tasks.size(); i++) {
92 const TaskWrapper& task = tasks.itemAt(i);
93 task.mProcessor->process(task.mTask);
94 }
95
96 return true;
97}
98
99bool TaskManager::WorkerThread::addTask(TaskWrapper task) {
100 if (!isRunning()) {
101 run(mName.string(), PRIORITY_DEFAULT);
102 }
103
104 Mutex::Autolock l(mLock);
105 ssize_t index = mTasks.add(task);
106 mSignal.signal();
107
108 return index >= 0;
109}
110
111size_t TaskManager::WorkerThread::getTaskCount() const {
112 Mutex::Autolock l(mLock);
113 return mTasks.size();
114}
115
116void TaskManager::WorkerThread::exit() {
117 {
118 Mutex::Autolock l(mLock);
119 mTasks.clear();
120 }
121 requestExit();
122 mSignal.signal();
123}
124
125}; // namespace uirenderer
126}; // namespace android