John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 1 | /* |
| 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 Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 19 | #include <utils/Trace.h> |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 20 | |
| 21 | #include <array> |
| 22 | |
| 23 | namespace android { |
| 24 | namespace uirenderer { |
| 25 | |
Jerome Gaillard | 9b2b762 | 2024-03-14 18:55:30 +0000 | [diff] [blame] | 26 | CommonPool::CommonPool() : CommonPoolBase() { |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 27 | ATRACE_CALL(); |
| 28 | |
| 29 | CommonPool* pool = this; |
Bo Liu | 027b218 | 2021-03-18 16:50:38 -0400 | [diff] [blame] | 30 | std::mutex mLock; |
| 31 | std::vector<int> tids(THREAD_COUNT); |
| 32 | std::vector<std::condition_variable> tidConditionVars(THREAD_COUNT); |
| 33 | |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 34 | // Create 2 workers |
| 35 | for (int i = 0; i < THREAD_COUNT; i++) { |
Bo Liu | 027b218 | 2021-03-18 16:50:38 -0400 | [diff] [blame] | 36 | std::thread worker([pool, i, &mLock, &tids, &tidConditionVars] { |
Jerome Gaillard | 9b2b762 | 2024-03-14 18:55:30 +0000 | [diff] [blame] | 37 | pool->setupThread(i, mLock, tids, tidConditionVars); |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 38 | pool->workerLoop(); |
| 39 | }); |
| 40 | worker.detach(); |
| 41 | } |
Bo Liu | 027b218 | 2021-03-18 16:50:38 -0400 | [diff] [blame] | 42 | { |
| 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 Gaillard | 9b2b762 | 2024-03-14 18:55:30 +0000 | [diff] [blame] | 50 | if (pool->supportsTid()) { |
| 51 | mWorkerThreadIds = std::move(tids); |
| 52 | } |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 53 | } |
| 54 | |
John Reck | cfd929d | 2019-04-08 11:28:15 -0700 | [diff] [blame] | 55 | CommonPool& CommonPool::instance() { |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 56 | static CommonPool pool; |
John Reck | cfd929d | 2019-04-08 11:28:15 -0700 | [diff] [blame] | 57 | return pool; |
| 58 | } |
| 59 | |
| 60 | void CommonPool::post(Task&& task) { |
| 61 | instance().enqueue(std::move(task)); |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Bo Liu | 027b218 | 2021-03-18 16:50:38 -0400 | [diff] [blame] | 64 | std::vector<int> CommonPool::getThreadIds() { |
| 65 | return instance().mWorkerThreadIds; |
| 66 | } |
| 67 | |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 68 | void 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 | |
| 81 | void CommonPool::workerLoop() { |
| 82 | std::unique_lock lock(mLock); |
Jerome Gaillard | 9b2b762 | 2024-03-14 18:55:30 +0000 | [diff] [blame] | 83 | while (!mIsStopping) { |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 84 | 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 Reck | cfd929d | 2019-04-08 11:28:15 -0700 | [diff] [blame] | 100 | void CommonPool::waitForIdle() { |
| 101 | instance().doWaitForIdle(); |
| 102 | } |
| 103 | |
| 104 | void 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 Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 113 | } // namespace uirenderer |
Bo Liu | 027b218 | 2021-03-18 16:50:38 -0400 | [diff] [blame] | 114 | } // namespace android |