blob: e9dde294b2aaaec53cdc70282495317ee8db17f4 [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
John Reckc4526542015-07-07 12:17:50 -070036 // 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 Craik05f3d6e2014-06-02 16:27:04 -070039 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() {
John Reck21be43e2014-08-14 10:25:16 -070086 setpriority(PRIO_PROCESS, 0, PRIORITY_FOREGROUND);
John Reck21be43e2014-08-14 10:25:16 -070087 return NO_ERROR;
88}
89
Romain Guy5dc7fa72013-03-11 20:48:31 -070090bool TaskManager::WorkerThread::threadLoop() {
91 mSignal.wait();
92 Vector<TaskWrapper> tasks;
93 {
94 Mutex::Autolock l(mLock);
95 tasks = mTasks;
96 mTasks.clear();
97 }
98
99 for (size_t i = 0; i < tasks.size(); i++) {
100 const TaskWrapper& task = tasks.itemAt(i);
101 task.mProcessor->process(task.mTask);
102 }
103
104 return true;
105}
106
107bool TaskManager::WorkerThread::addTask(TaskWrapper task) {
108 if (!isRunning()) {
109 run(mName.string(), PRIORITY_DEFAULT);
Sangkyu Leec3c58e02015-01-12 09:51:53 +0900110 } else if (exitPending()) {
111 return false;
Romain Guy5dc7fa72013-03-11 20:48:31 -0700112 }
113
John Reck815f2a02015-03-24 12:16:37 -0700114 ssize_t index;
115 {
116 Mutex::Autolock l(mLock);
117 index = mTasks.add(task);
118 }
Romain Guy5dc7fa72013-03-11 20:48:31 -0700119 mSignal.signal();
120
121 return index >= 0;
122}
123
124size_t TaskManager::WorkerThread::getTaskCount() const {
125 Mutex::Autolock l(mLock);
126 return mTasks.size();
127}
128
129void TaskManager::WorkerThread::exit() {
Romain Guy5dc7fa72013-03-11 20:48:31 -0700130 requestExit();
131 mSignal.signal();
132}
133
134}; // namespace uirenderer
135}; // namespace android