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