• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
CallFunction(Process process,void * argv)30 void UvQueue::CallFunction(Process process, void *argv)
31 {
32     if (process == nullptr || argv == nullptr) {
33         LOG_ERROR("nullptr");
34         return;
35     }
36     uv_work_t *work = new (std::nothrow) uv_work_t;
37     if (work == nullptr) {
38         LOG_ERROR("no memory for uv_work_t");
39         return;
40     }
41     work->data = new (std::nothrow)UvEntry { weak_from_this() };
42     if (work->data == nullptr) {
43         LOG_ERROR("no memory for UvEntry");
44         return;
45     }
46     {
47         std::unique_lock<std::shared_mutex> cacheLock(mutex_);
48         if (args_.count(process) != 0) {
49             std::list<void *> newData = args_.at(process);
50             newData.push_back(argv);
51             args_.insert_or_assign(process, newData);
52         } else {
53             std::list<void *> data;
54             data.push_back(argv);
55             args_.insert_or_assign(process, data);
56         }
57     }
58 
59     int ret = uv_queue_work(
60         loop_, work, [](uv_work_t *work) {},
61         [](uv_work_t *work, int uvstatus) {
62             UvEntry *entry = static_cast<UvEntry *>(work->data);
63             auto queue = entry->uvQueue_.lock();
64             if (queue != nullptr) {
65                 std::unique_lock<std::shared_mutex> cacheLock(queue->mutex_);
66                 for (auto &item : queue->args_) {
67                     item.first(queue->env_, item.second);
68                 }
69                 queue->args_.clear();
70             }
71             delete entry;
72             entry = nullptr;
73             delete work;
74             work = nullptr;
75         });
76     if (ret != 0) {
77         if (work->data != nullptr) {
78             UvEntry *uvEntry = static_cast<UvEntry *>(work->data);
79             delete uvEntry;
80             uvEntry = nullptr;
81         }
82         if (work != nullptr) {
83             delete work;
84             work = nullptr;
85         }
86     }
87 }
88 } // namespace OHOS::ObjectStore
89