1 /*
2 * Copyright (c) 2024-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "camera_napi_worker_queue_keeper.h"
17
18 #include <algorithm>
19 #include <chrono>
20 #include <condition_variable>
21 #include <cstdint>
22 #include <memory>
23 #include <mutex>
24 #include <new>
25
26 #include "camera_log.h"
27 #include "camera_napi_const.h"
28 #include "camera_napi_utils.h"
29 namespace OHOS {
30 namespace CameraStandard {
31 constexpr uint64_t WORKER_TIMEOUT = 2000L;
32 constexpr int32_t WORKER_TASK_WAIT_COUNT_MAX = 1;
33 static std::mutex g_WorkerQueueKeeperMutex;
34 static std::shared_ptr<CameraNapiWorkerQueueKeeper> g_WorkerQueueKeeper = nullptr;
35
WorkerQueueTasksResetCreateTimeNoLock(NapiWorkerQueueTaskTimePoint timePoint)36 void CameraNapiWorkerQueueKeeper::WorkerQueueTasksResetCreateTimeNoLock(NapiWorkerQueueTaskTimePoint timePoint)
37 {
38 for (auto& task : workerQueueTasks_) {
39 task->createTimePoint = timePoint;
40 task->waitCount = 0;
41 }
42 }
43
GetInstance()44 std::shared_ptr<CameraNapiWorkerQueueKeeper> CameraNapiWorkerQueueKeeper::GetInstance()
45 {
46 CHECK_RETURN_RET(g_WorkerQueueKeeper != nullptr, g_WorkerQueueKeeper);
47 std::lock_guard<std::mutex> lock(g_WorkerQueueKeeperMutex);
48 CHECK_RETURN_RET(g_WorkerQueueKeeper != nullptr, g_WorkerQueueKeeper);
49
50 g_WorkerQueueKeeper = std::make_shared<CameraNapiWorkerQueueKeeper>();
51 return g_WorkerQueueKeeper;
52 }
53
AcquireWorkerQueueTask(const std::string & taskName)54 std::shared_ptr<NapiWorkerQueueTask> CameraNapiWorkerQueueKeeper::AcquireWorkerQueueTask(const std::string& taskName)
55 {
56 auto queueTask = std::make_shared<NapiWorkerQueueTask>(taskName);
57 std::lock_guard<std::mutex> lock(workerQueueTaskMutex_);
58 workerQueueTasks_.push_back(queueTask);
59 return queueTask;
60 }
61
WorkerLockCondition(std::shared_ptr<NapiWorkerQueueTask> queueTask,bool & isError)62 bool CameraNapiWorkerQueueKeeper::WorkerLockCondition(std::shared_ptr<NapiWorkerQueueTask> queueTask, bool& isError)
63 {
64 std::lock_guard<std::mutex> lock(workerQueueTaskMutex_);
65 if (std::find(workerQueueTasks_.begin(), workerQueueTasks_.end(), queueTask) == workerQueueTasks_.end()) {
66 MEDIA_ERR_LOG("CameraNapiWorkerQueueKeeper::WorkerLockCondition current task %{public}s not in queue",
67 queueTask->taskName.c_str());
68 isError = true;
69 return true;
70 }
71 auto firstTask = workerQueueTasks_.front();
72 CHECK_RETURN_RET(firstTask == queueTask, true);
73 CHECK_RETURN_RET(firstTask->queueStatus == RUNNING, false);
74 auto now = std::chrono::steady_clock::now();
75 auto diffTime = std::chrono::duration_cast<std::chrono::milliseconds>(now - firstTask->createTimePoint);
76 CHECK_RETURN_RET(diffTime < std::chrono::milliseconds(WORKER_TIMEOUT), false);
77 if (queueTask->waitCount < WORKER_TASK_WAIT_COUNT_MAX) {
78 queueTask->waitCount++;
79 return false;
80 }
81 MEDIA_ERR_LOG("CameraNapiWorkerQueueKeeper::WorkerLockCondition current task %{public}s wait queue task %{public}s "
82 "timeout, waitTime:%{public}lld",
83 queueTask->taskName.c_str(), firstTask->taskName.c_str(), diffTime.count());
84 workerQueueTasks_.pop_front();
85 auto frontTask = workerQueueTasks_.front();
86 CHECK_RETURN_RET(frontTask == queueTask, true);
87 MEDIA_INFO_LOG("CameraNapiWorkerQueueKeeper::WorkerLockCondition current task not equal front task,%{public}s "
88 "vs %{public}s, continue wait.",
89 queueTask->taskName.c_str(), frontTask->taskName.c_str());
90 WorkerQueueTasksResetCreateTimeNoLock(now);
91 workerCond_.notify_all();
92 return false;
93 }
94
ConsumeWorkerQueueTask(std::shared_ptr<NapiWorkerQueueTask> queueTask,std::function<void (void)> func)95 bool CameraNapiWorkerQueueKeeper::ConsumeWorkerQueueTask(
96 std::shared_ptr<NapiWorkerQueueTask> queueTask, std::function<void(void)> func)
97 {
98 CHECK_RETURN_RET(queueTask == nullptr, false);
99 std::unique_lock<std::mutex> lock(workerQueueMutex_);
100 {
101 std::lock_guard<std::mutex> lock(workerQueueTaskMutex_);
102 CHECK_RETURN_RET(workerQueueTasks_.empty(), false);
103 CHECK_RETURN_RET(
104 std::find(workerQueueTasks_.begin(), workerQueueTasks_.end(), queueTask) == workerQueueTasks_.end(), false);
105 }
106 bool isMatchCondition = false;
107 bool isError = false;
108 while (!isMatchCondition) {
109 isMatchCondition = workerCond_.wait_for(lock, std::chrono::milliseconds(WORKER_TIMEOUT),
110 [this, &queueTask, &isError]() { return WorkerLockCondition(queueTask, isError); });
111 }
112 if (isError) {
113 MEDIA_ERR_LOG("CameraNapiWorkerQueueKeeper::ConsumeWorkerQueueTask wait task %{public}s occur error",
114 queueTask->taskName.c_str());
115 workerCond_.notify_all();
116 return false;
117 }
118 queueTask->queueStatus = RUNNING;
119 func();
120 queueTask->queueStatus = DONE;
121 {
122 auto now = std::chrono::steady_clock::now();
123 std::lock_guard<std::mutex> lock(workerQueueTaskMutex_);
124 workerQueueTasks_.pop_front();
125 WorkerQueueTasksResetCreateTimeNoLock(now);
126 }
127
128 workerCond_.notify_all();
129 return true;
130 }
131
RemoveWorkerTask(std::shared_ptr<NapiWorkerQueueTask> task)132 void CameraNapiWorkerQueueKeeper::RemoveWorkerTask(std::shared_ptr<NapiWorkerQueueTask> task)
133 {
134 CHECK_RETURN_ELOG(task == nullptr, "RemoveWorkerTask task is null");
135 std::lock_guard<std::mutex> lock(workerQueueTaskMutex_);
136 auto it = std::find(workerQueueTasks_.begin(), workerQueueTasks_.end(), task);
137 CHECK_RETURN_ELOG(it == workerQueueTasks_.end(), "RemoveWorkerTask not found task");
138 workerQueueTasks_.erase(it);
139 workerCond_.notify_all();
140 }
141 } // namespace CameraStandard
142 } // namespace OHOS