• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_SAFE_ASYNC_WORK_H
17 #define FOUNDATION_ACE_NAPI_NATIVE_ENGINE_NATIVE_SAFE_ASYNC_WORK_H
18 
19 #include "native_value.h"
20 
21 #include <atomic>
22 #include <mutex>
23 #include <deque>
24 #include <uv.h>
25 #ifdef LINUX_PLATFORM
26 #include <condition_variable>
27 #endif
28 
29 #include "native_async_context.h"
30 
31 #if defined(ENABLE_EVENT_HANDLER)
32 namespace OHOS::AppExecFwk {
33     class EventRunner;
34     class EventHandler;
35 }
36 #endif
37 
38 enum class SafeAsyncCode {
39     UNKNOWN = 0,
40     SAFE_ASYNC_QUEUE_FULL,
41     SAFE_ASYNC_INVALID_ARGS,
42     SAFE_ASYNC_CLOSED,
43     SAFE_ASYNC_FAILED,
44     SAFE_ASYNC_OK,
45 };
46 
47 enum class SafeAsyncStatus {
48     UNKNOW = 0,
49     SAFE_ASYNC_STATUS_INTE,
50     SAFE_ASYNC_STATUS_CLOSING,
51     SAFE_ASYNC_STATUS_CLOSED,
52 };
53 
54 class NativeSafeAsyncWork {
55 public:
56     static void AsyncCallback(uv_async_t* asyncHandler);
57     static void CallJs(NativeEngine* engine, napi_value js_call_func, void* context, void* data);
58 
59     NativeSafeAsyncWork(NativeEngine* engine,
60                         napi_value func,
61                         napi_value asyncResource,
62                         napi_value asyncResourceName,
63                         size_t maxQueueSize,
64                         size_t threadCount,
65                         void* finalizeData,
66                         NativeFinalize finalizeCallback,
67                         void* context,
68                         NativeThreadSafeFunctionCallJs callJsCallback);
69 
70     virtual ~NativeSafeAsyncWork();
71     virtual bool Init();
72     virtual SafeAsyncCode Send(void* data, NativeThreadSafeFunctionCallMode mode);
73     virtual SafeAsyncCode Acquire();
74     virtual SafeAsyncCode Release(NativeThreadSafeFunctionReleaseMode mode);
75     virtual bool Ref();
76     virtual bool Unref();
77     virtual void* GetContext();
78     virtual napi_status PostTask(void *data, int32_t priority, bool isTail);
79 
80 protected:
81     void ProcessAsyncHandle();
82     SafeAsyncCode CloseHandles();
83     void CleanUp();
84     bool IsSameTid();
85     bool IsMaxQueueSize();
86 
87     SafeAsyncCode ValidEngineCheck();
88 
89     NativeEngine* engine_ = nullptr;
90     uint64_t engineId_ = 0;
91     NativeReference* ref_ = nullptr;
92     size_t maxQueueSize_ = 0;
93     size_t threadCount_ = 0;
94     void* finalizeData_ = nullptr;
95     NativeFinalize finalizeCallback_ = nullptr;
96     void* context_ = nullptr;
97     NativeThreadSafeFunctionCallJs callJsCallback_ = nullptr;
98     NativeAsyncContext asyncContext_;
99     uv_async_t asyncHandler_;
100     std::mutex mutex_;
101     std::deque<void*> queue_;
102     std::condition_variable condition_;
103     SafeAsyncStatus status_ = SafeAsyncStatus::UNKNOW;
104 #if defined(ENABLE_EVENT_HANDLER)
105     std::shared_ptr<OHOS::AppExecFwk::EventRunner> runner_ = nullptr;
106     std::shared_ptr<OHOS::AppExecFwk::EventHandler> eventHandler_ = nullptr;
107     std::atomic<uint32_t> taskSize_ = 0;
108 #endif
109 
110 #ifdef ENABLE_CONTAINER_SCOPE
111     int32_t containerScopeId_;
112 #endif
113 };
114 
115 #endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_NATIVE_SAFE_ASYNC_WORK_H */
116