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