• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "datashare_uv_queue.h"
17 #include <thread>
18 #include <chrono>
19 #include "datashare_log.h"
20 
21 namespace OHOS {
22 namespace DataShare {
23 using namespace std::chrono;
24 constexpr int WAIT_TIME = 3;
25 constexpr int SLEEP_TIME = 1;
26 constexpr int TRY_TIMES = 2000;
DataShareUvQueue(napi_env env)27 DataShareUvQueue::DataShareUvQueue(napi_env env)
28     : env_(env)
29 {
30     napi_get_uv_event_loop(env, &loop_);
31 }
32 
LambdaForWork(TaskEntry * taskEntry)33 void DataShareUvQueue::LambdaForWork(TaskEntry* taskEntry)
34 {
35     if (taskEntry == nullptr) {
36         LOG_ERROR("invalid taskEntry.");
37         return;
38     }
39     {
40         std::unique_lock<std::mutex> lock(taskEntry->mutex);
41         if (taskEntry->func) {
42             taskEntry->func();
43         }
44         taskEntry->done = true;
45         taskEntry->condition.notify_all();
46     }
47     if (taskEntry->count.fetch_sub(1) == 1) {
48         delete taskEntry;
49         taskEntry = nullptr;
50     }
51 }
52 
SyncCall(NapiVoidFunc func,NapiBoolFunc retFunc)53 void DataShareUvQueue::SyncCall(NapiVoidFunc func, NapiBoolFunc retFunc)
54 {
55     auto *taskEntry = new (std::nothrow)TaskEntry {env_, std::move(func), false, {}, {}, std::atomic<int>(1)};
56     if (taskEntry == nullptr) {
57         LOG_ERROR("invalid taskEntry.");
58         return;
59     }
60     {
61         std::unique_lock<std::mutex> lock(taskEntry->mutex);
62         taskEntry->count.fetch_add(1);
63         auto task = [taskEntry]() {
64             DataShareUvQueue::LambdaForWork(taskEntry);
65         };
66         if (napi_status::napi_ok != napi_send_event(env_, task, napi_eprio_immediate)) {
67             LOG_ERROR("napi_send_event task failed");
68             delete taskEntry;
69             taskEntry = nullptr;
70             return;
71         }
72         if (taskEntry->condition.wait_for(lock, std::chrono::seconds(WAIT_TIME),
73             [taskEntry] { return taskEntry->done; })) {
74             auto time = static_cast<uint64_t>(duration_cast<milliseconds>(
75                 system_clock::now().time_since_epoch()).count());
76             LOG_WARN("function ended successfully. times %{public}" PRIu64 ".", time);
77         }
78     }
79     CheckFuncAndExec(retFunc);
80     if (taskEntry->count.fetch_sub(1) == 1) {
81         delete taskEntry;
82         taskEntry = nullptr;
83     }
84 }
85 
CheckFuncAndExec(NapiBoolFunc retFunc)86 void DataShareUvQueue::CheckFuncAndExec(NapiBoolFunc retFunc)
87 {
88     if (retFunc) {
89         int tryTimes = TRY_TIMES;
90         while (retFunc() != true && tryTimes > 0) {
91             std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME));
92             tryTimes--;
93         }
94         if (tryTimes <= 0) {
95             LOG_ERROR("function execute timeout.");
96         }
97     }
98 }
99 } // namespace DataShare
100 } // namespace OHOS