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 #ifndef CAMERA_NAPI_WORKER_QUEUE_KEEPER
17 #define CAMERA_NAPI_WORKER_QUEUE_KEEPER
18 
19 #include <cstdint>
20 #include <functional>
21 #include <list>
22 #include <memory>
23 #include <mutex>
24 
25 #include "camera_napi_const.h"
26 #include "js_native_api.h"
27 #include "js_native_api_types.h"
28 #include "napi/native_api.h"
29 
30 namespace OHOS {
31 namespace CameraStandard {
32 class CameraNapiWorkerQueueKeeper {
33 public:
34     explicit CameraNapiWorkerQueueKeeper() = default;
35     virtual ~CameraNapiWorkerQueueKeeper() = default;
36     static std::shared_ptr<CameraNapiWorkerQueueKeeper> GetInstance();
37 
38     std::shared_ptr<NapiWorkerQueueTask> AcquireWorkerQueueTask(const std::string& taskName);
39     bool ConsumeWorkerQueueTask(std::shared_ptr<NapiWorkerQueueTask> queueTask, std::function<void(void)> func);
40 
41 private:
42     void WorkerQueueTasksResetCreateTimeNoLock(NapiWorkerQueueTaskTimePoint timePoint);
43     bool WorkerLockCondition(std::shared_ptr<NapiWorkerQueueTask> queueTask, bool& isError);
44 
45     std::mutex workerQueueMutex_;
46     std::condition_variable workerCond_;
47 
48     std::mutex workerQueueTaskMutex_;
49     std::list<std::shared_ptr<NapiWorkerQueueTask>> workerQueueTasks_;
50 };
51 } // namespace CameraStandard
52 } // namespace OHOS
53 
54 #endif