• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #ifndef FOUNDATION_ACE_NAPI_NATIVE_ENGINE_NATIVE_EVENT_H
16 #define FOUNDATION_ACE_NAPI_NATIVE_ENGINE_NATIVE_EVENT_H
17 #include <atomic>
18 #include <cstdint>
19 #include <shared_mutex>
20 #include <string>
21 #include "native_safe_async_work.h"
22 
23 typedef struct CallbackWrapper_ {
24     std::function<void()> cb;
25     std::atomic<uint64_t> handleId;
26 } CallbackWrapper;
27 
28 class NativeEvent : public NativeSafeAsyncWork {
29 public:
30     NativeEvent(NativeEngine* engine,
31                 napi_value func,
32                 napi_value asyncResource,
33                 napi_value asyncResourceName,
34                 size_t maxQueueSize,
35                 size_t threadCount,
36                 void* finalizeData,
37                 NativeFinalize finalizeCallback,
38                 void* context,
39                 NativeThreadSafeFunctionCallJs callJsCallback);
40     bool Init() override;
41     virtual napi_status SendCancelableEvent(const std::function<void(void*)> &callback,
42                                             void* data,
43                                             int32_t priority,
44                                             const char* name,
45                                             uint64_t* handleId);
46     virtual napi_status CancelEvent(const char* name, uint64_t handleId);
47     virtual SafeAsyncCode UvCancelEvent(uint64_t handleId);
48     static void CreateDefaultFunction(NativeEngine* eng, napi_threadsafe_function &defaultFunc,
49                                       std::shared_mutex &eventMutex);
50     static void DestoryDefaultFunction(bool release, napi_threadsafe_function &defaultFunc,
51                                        std::shared_mutex &eventMutex);
52 protected:
53     //unique key for event
54     std::atomic<uint64_t> sequence_;
55     uint64_t GenerateUniqueID();
56     napi_status SendEventByEventHandler(const std::function<void()> &task, uint64_t eventId,
57                                         int32_t priority, const char* name, uint64_t* handleId);
58     napi_status SendEventByUv(const std::function<void()> &task, uint64_t eventId,
59                               const char* name, uint64_t* handleId);
60     napi_status SendConvertStatus2NapiStatus(void* data, NativeThreadSafeFunctionCallMode mode);
61 };
62 #endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_NATIVE_EVENT_H */