• 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 <mutex>
22 #include <queue>
23 #include <uv.h>
24 #ifdef LINUX_PLATFORM
25 #include <condition_variable>
26 #endif
27 
28 #include "native_async_context.h"
29 
30 enum class SafeAsyncCode {
31     UNKNOWN = 0,
32     SAFE_ASYNC_QUEUE_FULL,
33     SAFE_ASYNC_INVALID_ARGS,
34     SAFE_ASYNC_CLOSED,
35     SAFE_ASYNC_FAILED,
36     SAFE_ASYNC_OK,
37 };
38 
39 enum class SafeAsyncStatus {
40     UNKNOW = 0,
41     SAFE_ASYNC_STATUS_INTE,
42     SAFE_ASYNC_STATUS_CLOSING,
43     SAFE_ASYNC_STATUS_CLOSED,
44 };
45 
46 class NativeSafeAsyncWork {
47 public:
48     static void AsyncCallback(uv_async_t* asyncHandler);
49     static void CallJs(NativeEngine* engine, napi_value js_call_func, void* context, void* data);
50 
51     NativeSafeAsyncWork(NativeEngine* engine,
52                         napi_value func,
53                         napi_value asyncResource,
54                         napi_value asyncResourceName,
55                         size_t maxQueueSize,
56                         size_t threadCount,
57                         void* finalizeData,
58                         NativeFinalize finalizeCallback,
59                         void* context,
60                         NativeThreadSafeFunctionCallJs callJsCallback);
61 
62     virtual ~NativeSafeAsyncWork();
63     virtual bool Init();
64     virtual SafeAsyncCode Send(void* data, NativeThreadSafeFunctionCallMode mode);
65     virtual SafeAsyncCode Acquire();
66     virtual SafeAsyncCode Release(NativeThreadSafeFunctionReleaseMode mode);
67     virtual bool Ref();
68     virtual bool Unref();
69     virtual void* GetContext();
70 
71 private:
72     void ProcessAsyncHandle();
73     SafeAsyncCode CloseHandles();
74     void CleanUp();
75     bool IsSameTid();
76     bool IsMaxQueueSize();
77 
78     NativeEngine* engine_ = nullptr;
79     NativeReference* ref_ = nullptr;
80     size_t maxQueueSize_ = 0;
81     size_t threadCount_ = 0;
82     void* finalizeData_ = nullptr;
83     NativeFinalize finalizeCallback_ = nullptr;
84     void* context_ = nullptr;
85     NativeThreadSafeFunctionCallJs callJsCallback_ = nullptr;
86     NativeAsyncContext asyncContext_;
87     uv_async_t asyncHandler_;
88     std::mutex mutex_;
89     std::queue<void*> queue_;
90     std::condition_variable condition_;
91     SafeAsyncStatus status_ = SafeAsyncStatus::UNKNOW;
92 };
93 
94 #endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_NATIVE_SAFE_ASYNC_WORK_H */
95