• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 FOUNDATION_ACE_NAPI_NATIVE_ENGINE_NATIVE_ASYNC_WORK_H
17 #define FOUNDATION_ACE_NAPI_NATIVE_ENGINE_NATIVE_ASYNC_WORK_H
18 
19 #include "interfaces/kits/napi/common.h"
20 #include "native_value.h"
21 
22 #include <mutex>
23 #include <queue>
24 #include <uv.h>
25 
26 struct NativeAsyncWorkDataPointer {
NativeAsyncWorkDataPointerNativeAsyncWorkDataPointer27     NativeAsyncWorkDataPointer()
28     {
29         data_ = nullptr;
30     }
31 
NativeAsyncWorkDataPointerNativeAsyncWorkDataPointer32     explicit NativeAsyncWorkDataPointer(void* data)
33     {
34         data_ = data;
35     }
36     void* data_ { nullptr };
37 };
38 
39 #ifdef ENABLE_HITRACE
40 namespace OHOS {
41 namespace HiviewDFX {
42 class HiTraceId;
43 }
44 } // namespace OHOS
45 #endif
46 
47 class NativeAsyncWork {
48 public:
49     NativeAsyncWork(NativeEngine* engine,
50                     NativeAsyncExecuteCallback execute,
51                     NativeAsyncCompleteCallback complete,
52                     const std::string &asyncResourceName,
53                     void* data);
54 
55     virtual ~NativeAsyncWork();
56     virtual bool Queue();
57     virtual bool QueueWithQos(napi_qos_t qos);
58     virtual bool Cancel();
59     virtual std::string GetTraceDescription();
60     template<typename Inner, typename Outer>
DereferenceOf(const Inner Outer::* field,const Inner * pointer)61     static Outer* DereferenceOf(const Inner Outer::*field, const Inner* pointer)
62     {
63         if (field != nullptr && pointer != nullptr) {
64             auto fieldOffset = reinterpret_cast<uintptr_t>(&(static_cast<Outer*>(0)->*field));
65             auto outPointer = reinterpret_cast<Outer*>(reinterpret_cast<uintptr_t>(pointer) - fieldOffset);
66             return outPointer;
67         }
68         return nullptr;
69     }
70 
71 private:
72     static void AsyncWorkCallback(uv_work_t* req);
73     static void AsyncAfterWorkCallback(uv_work_t* req, int status);
74 
75     uv_work_t work_;
76     NativeEngine* engine_;
77     NativeAsyncExecuteCallback execute_;
78     NativeAsyncCompleteCallback complete_;
79     void* data_;
80     std::mutex workAsyncMutex_;
81     std::queue<NativeAsyncWorkDataPointer> asyncWorkRecvData_;
82     std::string traceDescription_;
83 #ifdef ENABLE_HITRACE
84     std::unique_ptr<OHOS::HiviewDFX::HiTraceId> traceId_;
85 #endif
86 };
87 
88 #endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_NATIVE_ASYNC_WORK_H */
89