TaskManager.cpp revision 05f3d6e5111fd08df5cd9aae2c3d28399dc0e7f5
1/*
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 "TaskManager.h"
20#include "Task.h"
21#include "TaskProcessor.h"
22#include "utils/MathUtils.h"
23
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.
33    int cpuCount = sysconf(_SC_NPROCESSORS_CONF);
34
35    int workerCount = MathUtils::max(1, cpuCount / 2);
36    for (int i = 0; i < workerCount; i++) {
37        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
53void TaskManager::stop() {
54    for (size_t i = 0; i < mThreads.size(); i++) {
55        mThreads[i]->exit();
56    }
57}
58
59bool 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
127