blob: 6c0c30f959558ac487a91350131a6c0524d882b8 [file] [log] [blame]
John Reck322b8ab2019-03-14 13:15:28 -07001/*
2 * Copyright (C) 2019 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 "CommonPool.h"
18
John Reck322b8ab2019-03-14 13:15:28 -070019#include <utils/Trace.h>
John Reck322b8ab2019-03-14 13:15:28 -070020
21#include <array>
22
23namespace android {
24namespace uirenderer {
25
Jerome Gaillard9b2b7622024-03-14 18:55:30 +000026CommonPool::CommonPool() : CommonPoolBase() {
John Reck322b8ab2019-03-14 13:15:28 -070027 ATRACE_CALL();
28
29 CommonPool* pool = this;
Bo Liu027b2182021-03-18 16:50:38 -040030 std::mutex mLock;
31 std::vector<int> tids(THREAD_COUNT);
32 std::vector<std::condition_variable> tidConditionVars(THREAD_COUNT);
33
John Reck322b8ab2019-03-14 13:15:28 -070034 // Create 2 workers
35 for (int i = 0; i < THREAD_COUNT; i++) {
Bo Liu027b2182021-03-18 16:50:38 -040036 std::thread worker([pool, i, &mLock, &tids, &tidConditionVars] {
Jerome Gaillard9b2b7622024-03-14 18:55:30 +000037 pool->setupThread(i, mLock, tids, tidConditionVars);
John Reck322b8ab2019-03-14 13:15:28 -070038 pool->workerLoop();
39 });
40 worker.detach();
41 }
Bo Liu027b2182021-03-18 16:50:38 -040042 {
43 std::unique_lock lock(mLock);
44 for (int i = 0; i < THREAD_COUNT; i++) {
45 while (!tids[i]) {
46 tidConditionVars[i].wait(lock);
47 }
48 }
49 }
Jerome Gaillard9b2b7622024-03-14 18:55:30 +000050 if (pool->supportsTid()) {
51 mWorkerThreadIds = std::move(tids);
52 }
John Reck322b8ab2019-03-14 13:15:28 -070053}
54
John Reckcfd929d2019-04-08 11:28:15 -070055CommonPool& CommonPool::instance() {
John Reck322b8ab2019-03-14 13:15:28 -070056 static CommonPool pool;
John Reckcfd929d2019-04-08 11:28:15 -070057 return pool;
58}
59
60void CommonPool::post(Task&& task) {
61 instance().enqueue(std::move(task));
John Reck322b8ab2019-03-14 13:15:28 -070062}
63
Bo Liu027b2182021-03-18 16:50:38 -040064std::vector<int> CommonPool::getThreadIds() {
65 return instance().mWorkerThreadIds;
66}
67
John Reck322b8ab2019-03-14 13:15:28 -070068void CommonPool::enqueue(Task&& task) {
69 std::unique_lock lock(mLock);
70 while (!mWorkQueue.hasSpace()) {
71 lock.unlock();
72 usleep(100);
73 lock.lock();
74 }
75 mWorkQueue.push(std::move(task));
76 if (mWaitingThreads == THREAD_COUNT || (mWaitingThreads > 0 && mWorkQueue.size() > 1)) {
77 mCondition.notify_one();
78 }
79}
80
81void CommonPool::workerLoop() {
82 std::unique_lock lock(mLock);
Jerome Gaillard9b2b7622024-03-14 18:55:30 +000083 while (!mIsStopping) {
John Reck322b8ab2019-03-14 13:15:28 -070084 if (!mWorkQueue.hasWork()) {
85 mWaitingThreads++;
86 mCondition.wait(lock);
87 mWaitingThreads--;
88 }
89 // Need to double-check that work is still available now that we have the lock
90 // It may have already been grabbed by a different thread
91 while (mWorkQueue.hasWork()) {
92 auto work = mWorkQueue.pop();
93 lock.unlock();
94 work();
95 lock.lock();
96 }
97 }
98}
99
John Reckcfd929d2019-04-08 11:28:15 -0700100void CommonPool::waitForIdle() {
101 instance().doWaitForIdle();
102}
103
104void CommonPool::doWaitForIdle() {
105 std::unique_lock lock(mLock);
106 while (mWaitingThreads != THREAD_COUNT) {
107 lock.unlock();
108 usleep(100);
109 lock.lock();
110 }
111}
112
John Reck322b8ab2019-03-14 13:15:28 -0700113} // namespace uirenderer
Bo Liu027b2182021-03-18 16:50:38 -0400114} // namespace android