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 #ifndef UV_QUEUE_H
16 #define UV_QUEUE_H
17 #include <functional>
18 #include <list>
19 #include <map>
20 #include <shared_mutex>
21 
22 #include "napi/native_api.h"
23 #include "napi/native_node_api.h"
24 #include "uv.h"
25 
26 namespace OHOS::ObjectStore {
27 typedef void (*Process)(napi_env env, std::list<void *> &);
28 class UvQueue : public std::enable_shared_from_this<UvQueue> {
29 public:
30     UvQueue(napi_env env);
31     virtual ~UvQueue();
32 
33     void CallFunction(Process process, void *argv);
34 
35 private:
36     struct UvEntry {
37         std::weak_ptr<UvQueue> uvQueue_;
38     };
39 
40     static void ExecUvWork(uv_work_t *work, int uvstatus);
41 
42     napi_env env_;
43     std::shared_mutex mutex_{};
44     // key is callback,value is list of args
45     std::map<Process, std::list<void *>> args_;
46     uv_loop_s *loop_ = nullptr;
47 };
48 } // namespace OHOS::ObjectStore
49 #endif
50