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