• 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 TaskEntry {env_, std::move(func), false, {}, {}, std::atomic<int>(1)};
56     {
57         std::unique_lock<std::mutex> lock(taskEntry->mutex);
58         taskEntry->count.fetch_add(1);
59         auto task = [taskEntry]() {
60             DataShareUvQueue::LambdaForWork(taskEntry);
61         };
62         if (napi_status::napi_ok != napi_send_event(env_, task, napi_eprio_immediate)) {
63             LOG_ERROR("napi_send_event task failed");
64             delete taskEntry;
65             taskEntry = nullptr;
66             return;
67         }
68         if (taskEntry->condition.wait_for(lock, std::chrono::seconds(WAIT_TIME),
69             [taskEntry] { return taskEntry->done; })) {
70             auto time = static_cast<uint64_t>(duration_cast<milliseconds>(
71                 system_clock::now().time_since_epoch()).count());
72             LOG_INFO("function ended successfully. times %{public}" PRIu64 ".", time);
73         }
74     }
75     CheckFuncAndExec(retFunc);
76     if (taskEntry->count.fetch_sub(1) == 1) {
77         delete taskEntry;
78         taskEntry = nullptr;
79     }
80 }
81 
CheckFuncAndExec(NapiBoolFunc retFunc)82 void DataShareUvQueue::CheckFuncAndExec(NapiBoolFunc retFunc)
83 {
84     if (retFunc) {
85         int tryTimes = TRY_TIMES;
86         while (retFunc() != true && tryTimes > 0) {
87             std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME));
88             tryTimes--;
89         }
90         if (tryTimes <= 0) {
91             LOG_ERROR("function execute timeout.");
92         }
93     }
94 }
95 } // namespace DataShare
96 } // namespace OHOS