1 /*
2  * Copyright (c) 2021 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 #include "uv_queue.h"
16 #include <mutex>
17 #include "logger.h"
18 
19 namespace OHOS::ObjectStore {
UvQueue(napi_env env)20 UvQueue::UvQueue(napi_env env) : env_(env)
21 {
22     napi_get_uv_event_loop(env, &loop_);
23 }
24 
~UvQueue()25 UvQueue::~UvQueue()
26 {
27     LOG_DEBUG("no memory leak for queue-callback");
28 }
29 
ExecUvWork(uv_work_t * work,int uvstatus)30 void UvQueue::ExecUvWork(uv_work_t *work, int uvstatus)
31 {
32     UvEntry *entry = static_cast<UvEntry *>(work->data);
33     auto queue = entry->uvQueue_.lock();
34     if (queue != nullptr) {
35         std::unique_lock<std::shared_mutex> cacheLock(queue->mutex_);
36         for (auto &item : queue->args_) {
37             item.first(queue->env_, item.second);
38         }
39         queue->args_.clear();
40     }
41     delete entry;
42     work->data = nullptr;
43     delete work;
44     work = nullptr;
45 }
46 
CallFunction(Process process,void * argv)47 void UvQueue::CallFunction(Process process, void *argv)
48 {
49     if (process == nullptr || argv == nullptr) {
50         LOG_ERROR("nullptr");
51         return;
52     }
53     uv_work_t *work = new (std::nothrow) uv_work_t;
54     if (work == nullptr) {
55         LOG_ERROR("no memory for uv_work_t");
56         return;
57     }
58     work->data = new (std::nothrow)UvEntry { weak_from_this() };
59     if (work->data == nullptr) {
60         delete work;
61         LOG_ERROR("no memory for UvEntry");
62         return;
63     }
64     {
65         std::unique_lock<std::shared_mutex> cacheLock(mutex_);
66         if (args_.count(process) != 0) {
67             std::list<void *> newData = args_.at(process);
68             newData.push_back(argv);
69             args_.insert_or_assign(process, newData);
70         } else {
71             std::list<void *> data;
72             data.push_back(argv);
73             args_.insert_or_assign(process, data);
74         }
75     }
76 
77     int ret = uv_queue_work(
78         loop_, work, [](uv_work_t *work) {}, UvQueue::ExecUvWork);
79     if (ret != 0) {
80         if (work->data != nullptr) {
81             UvEntry *uvEntry = static_cast<UvEntry *>(work->data);
82             delete uvEntry;
83             uvEntry = nullptr;
84         }
85         if (work != nullptr) {
86             delete work;
87             work = nullptr;
88         }
89     }
90 }
91 } // namespace OHOS::ObjectStore
92