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 #ifndef DATASHARE_UV_QUEUE_H 17 #define DATASHARE_UV_QUEUE_H 18 19 #include <functional> 20 #include <condition_variable> 21 #include <mutex> 22 23 #include "napi/native_api.h" 24 #include "napi/native_common.h" 25 #include "napi/native_node_api.h" 26 #include "uv.h" 27 28 namespace OHOS { 29 namespace DataShare { 30 class DataShareUvQueue { 31 using NapiVoidFunc = std::function<void()>; 32 using NapiBoolFunc = std::function<bool()>; 33 34 public: 35 explicit DataShareUvQueue(napi_env env); 36 virtual ~DataShareUvQueue() = default; 37 38 void SyncCall(NapiVoidFunc func = NapiVoidFunc(), NapiBoolFunc retFunc = NapiBoolFunc()); 39 40 void CheckFuncAndExec(NapiBoolFunc retFunc); 41 42 private: 43 struct UvEntry { 44 napi_env env; 45 NapiVoidFunc func; 46 bool done; 47 std::condition_variable condition; 48 std::mutex mutex; 49 std::atomic_int32_t count; 50 }; 51 52 static void LambdaForWork(uv_work_t* work, int uvstatus); 53 54 static void Purge(uv_work_t* work); 55 56 napi_env env_ = nullptr; 57 uv_loop_s* loop_ = nullptr; 58 }; 59 } // namespace DataShare 60 } // namespace OHOS 61 #endif // DATASHARE_UV_QUEUE_H