133eb0b6dSopenharmony_ci/* 233eb0b6dSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 333eb0b6dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 433eb0b6dSopenharmony_ci * you may not use this file except in compliance with the License. 533eb0b6dSopenharmony_ci * You may obtain a copy of the License at 633eb0b6dSopenharmony_ci * 733eb0b6dSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 833eb0b6dSopenharmony_ci * 933eb0b6dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1033eb0b6dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1133eb0b6dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1233eb0b6dSopenharmony_ci * See the License for the specific language governing permissions and 1333eb0b6dSopenharmony_ci * limitations under the License. 1433eb0b6dSopenharmony_ci */ 1533eb0b6dSopenharmony_ci 1633eb0b6dSopenharmony_ci#ifndef FOUNDATION_ACE_NAPI_NATIVE_ENGINE_NATIVE_SAFE_ASYNC_WORK_H 1733eb0b6dSopenharmony_ci#define FOUNDATION_ACE_NAPI_NATIVE_ENGINE_NATIVE_SAFE_ASYNC_WORK_H 1833eb0b6dSopenharmony_ci 1933eb0b6dSopenharmony_ci#include "native_value.h" 2033eb0b6dSopenharmony_ci 2133eb0b6dSopenharmony_ci#include <mutex> 2233eb0b6dSopenharmony_ci#include <queue> 2333eb0b6dSopenharmony_ci#include <uv.h> 2433eb0b6dSopenharmony_ci#ifdef LINUX_PLATFORM 2533eb0b6dSopenharmony_ci#include <condition_variable> 2633eb0b6dSopenharmony_ci#endif 2733eb0b6dSopenharmony_ci 2833eb0b6dSopenharmony_ci#include "native_async_context.h" 2933eb0b6dSopenharmony_ci 3033eb0b6dSopenharmony_ci#if defined(ENABLE_EVENT_HANDLER) 3133eb0b6dSopenharmony_cinamespace OHOS::AppExecFwk { 3233eb0b6dSopenharmony_ci class EventHandler; 3333eb0b6dSopenharmony_ci} 3433eb0b6dSopenharmony_ci#endif 3533eb0b6dSopenharmony_ci 3633eb0b6dSopenharmony_cienum class SafeAsyncCode { 3733eb0b6dSopenharmony_ci UNKNOWN = 0, 3833eb0b6dSopenharmony_ci SAFE_ASYNC_QUEUE_FULL, 3933eb0b6dSopenharmony_ci SAFE_ASYNC_INVALID_ARGS, 4033eb0b6dSopenharmony_ci SAFE_ASYNC_CLOSED, 4133eb0b6dSopenharmony_ci SAFE_ASYNC_FAILED, 4233eb0b6dSopenharmony_ci SAFE_ASYNC_OK, 4333eb0b6dSopenharmony_ci}; 4433eb0b6dSopenharmony_ci 4533eb0b6dSopenharmony_cienum class SafeAsyncStatus { 4633eb0b6dSopenharmony_ci UNKNOW = 0, 4733eb0b6dSopenharmony_ci SAFE_ASYNC_STATUS_INTE, 4833eb0b6dSopenharmony_ci SAFE_ASYNC_STATUS_CLOSING, 4933eb0b6dSopenharmony_ci SAFE_ASYNC_STATUS_CLOSED, 5033eb0b6dSopenharmony_ci}; 5133eb0b6dSopenharmony_ci 5233eb0b6dSopenharmony_citypedef struct CallbackWrapper_ { 5333eb0b6dSopenharmony_ci std::function<void()> cb; 5433eb0b6dSopenharmony_ci} CallbackWrapper; 5533eb0b6dSopenharmony_ci 5633eb0b6dSopenharmony_ciclass NativeSafeAsyncWork { 5733eb0b6dSopenharmony_cipublic: 5833eb0b6dSopenharmony_ci static void AsyncCallback(uv_async_t* asyncHandler); 5933eb0b6dSopenharmony_ci static void CallJs(NativeEngine* engine, napi_value js_call_func, void* context, void* data); 6033eb0b6dSopenharmony_ci 6133eb0b6dSopenharmony_ci NativeSafeAsyncWork(NativeEngine* engine, 6233eb0b6dSopenharmony_ci napi_value func, 6333eb0b6dSopenharmony_ci napi_value asyncResource, 6433eb0b6dSopenharmony_ci napi_value asyncResourceName, 6533eb0b6dSopenharmony_ci size_t maxQueueSize, 6633eb0b6dSopenharmony_ci size_t threadCount, 6733eb0b6dSopenharmony_ci void* finalizeData, 6833eb0b6dSopenharmony_ci NativeFinalize finalizeCallback, 6933eb0b6dSopenharmony_ci void* context, 7033eb0b6dSopenharmony_ci NativeThreadSafeFunctionCallJs callJsCallback); 7133eb0b6dSopenharmony_ci 7233eb0b6dSopenharmony_ci virtual ~NativeSafeAsyncWork(); 7333eb0b6dSopenharmony_ci virtual bool Init(); 7433eb0b6dSopenharmony_ci virtual SafeAsyncCode Send(void* data, NativeThreadSafeFunctionCallMode mode); 7533eb0b6dSopenharmony_ci virtual SafeAsyncCode Acquire(); 7633eb0b6dSopenharmony_ci virtual SafeAsyncCode Release(NativeThreadSafeFunctionReleaseMode mode); 7733eb0b6dSopenharmony_ci virtual bool Ref(); 7833eb0b6dSopenharmony_ci virtual bool Unref(); 7933eb0b6dSopenharmony_ci virtual void* GetContext(); 8033eb0b6dSopenharmony_ci virtual napi_status PostTask(void *data, int32_t priority, bool isTail); 8133eb0b6dSopenharmony_ci virtual napi_status SendEvent(const std::function<void()> &cb, napi_event_priority priority); 8233eb0b6dSopenharmony_ci 8333eb0b6dSopenharmony_ciprivate: 8433eb0b6dSopenharmony_ci void ProcessAsyncHandle(); 8533eb0b6dSopenharmony_ci SafeAsyncCode CloseHandles(); 8633eb0b6dSopenharmony_ci void CleanUp(); 8733eb0b6dSopenharmony_ci bool IsSameTid(); 8833eb0b6dSopenharmony_ci bool IsMaxQueueSize(); 8933eb0b6dSopenharmony_ci 9033eb0b6dSopenharmony_ci SafeAsyncCode ValidEngineCheck(); 9133eb0b6dSopenharmony_ci 9233eb0b6dSopenharmony_ci NativeEngine* engine_ = nullptr; 9333eb0b6dSopenharmony_ci uint64_t engineId_ = 0; 9433eb0b6dSopenharmony_ci NativeReference* ref_ = nullptr; 9533eb0b6dSopenharmony_ci size_t maxQueueSize_ = 0; 9633eb0b6dSopenharmony_ci size_t threadCount_ = 0; 9733eb0b6dSopenharmony_ci void* finalizeData_ = nullptr; 9833eb0b6dSopenharmony_ci NativeFinalize finalizeCallback_ = nullptr; 9933eb0b6dSopenharmony_ci void* context_ = nullptr; 10033eb0b6dSopenharmony_ci NativeThreadSafeFunctionCallJs callJsCallback_ = nullptr; 10133eb0b6dSopenharmony_ci NativeAsyncContext asyncContext_; 10233eb0b6dSopenharmony_ci uv_async_t asyncHandler_; 10333eb0b6dSopenharmony_ci std::mutex mutex_; 10433eb0b6dSopenharmony_ci std::queue<void*> queue_; 10533eb0b6dSopenharmony_ci std::condition_variable condition_; 10633eb0b6dSopenharmony_ci SafeAsyncStatus status_ = SafeAsyncStatus::UNKNOW; 10733eb0b6dSopenharmony_ci#if defined(ENABLE_EVENT_HANDLER) 10833eb0b6dSopenharmony_ci std::mutex eventHandlerMutex_; 10933eb0b6dSopenharmony_ci std::shared_ptr<OHOS::AppExecFwk::EventHandler> eventHandler_ = nullptr; 11033eb0b6dSopenharmony_ci#endif 11133eb0b6dSopenharmony_ci 11233eb0b6dSopenharmony_ci#ifdef ENABLE_CONTAINER_SCOPE 11333eb0b6dSopenharmony_ci int32_t containerScopeId_; 11433eb0b6dSopenharmony_ci#endif 11533eb0b6dSopenharmony_ci}; 11633eb0b6dSopenharmony_ci 11733eb0b6dSopenharmony_ci#endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_NATIVE_SAFE_ASYNC_WORK_H */ 118