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 ENGINE_H 17 #define ENGINE_H 18 19 #include <memory> 20 21 #include "platform/queuepool/queue.h" 22 #include "plugin_manager/include/i_plugin_manager.h" 23 #include "plugin_manager/include/plugin.h" 24 #include "protocol/data_channel/include/i_request.h" 25 #include "protocol/data_channel/include/i_response.h" 26 #include "server_executor/include/engine_worker.h" 27 #include "server_executor/include/future.h" 28 #include "server_executor/include/i_handler.h" 29 30 namespace OHOS { 31 namespace AI { 32 const int SYNC_MSG_TIMEOUT = -1; 33 const char * const PLUGIN_SYNC_INFER = "SYNC"; 34 35 class Engine { 36 public: 37 Engine(std::shared_ptr<Plugin> &plugin, std::shared_ptr<Thread> &thread, std::shared_ptr<Queue<Task>> &queue); 38 ~Engine(); 39 40 /** 41 * Initialize the engine, which is called when the engine is created. 42 * 43 * @return Returns RETCODE_SUCCESS(0) if the operation is successful, returns a non-zero value otherwise. 44 */ 45 int Initialize(); 46 47 std::shared_ptr<Plugin> GetPlugin() const; 48 49 /** 50 * Get the number of references for the engine. 51 * 52 * @return Number of engine references. 53 */ 54 int GetEngineReference() const; 55 56 /** 57 * Engine reference count increased by 1, which is called when the engine starts. 58 */ 59 void AddEngineReference(); 60 61 /** 62 * The engine reference count is reduced by 1, which is called when the engine stops. 63 */ 64 void DelEngineReference(); 65 66 /** 67 * Algorithmic execution interface for synchronous tasks. 68 * 69 * @param [in] request Request information of synchronous task. 70 * @param [out] response Response of synchronous task. 71 * @return Returns 0 if the operation is successful, returns a non-zero value otherwise. 72 */ 73 int SyncExecute(IRequest *request, IResponse *&response); 74 75 /** 76 * Algorithmic execution interface for asynchronous tasks. 77 * 78 * @param [in] request Request information of asynchronous task. 79 * @return Returns 0 if the operation is successful, returns a non-zero value otherwise. 80 */ 81 int AsyncExecute(IRequest *request); 82 83 private: 84 void Uninitialize(); 85 86 private: 87 std::atomic<int> refCount_; 88 std::shared_ptr<Plugin> plugin_; 89 std::shared_ptr<Thread> thread_; 90 std::shared_ptr<Queue<Task>> queue_; 91 IHandler *msgHandler_; 92 EngineWorker worker_; 93 }; 94 } // namespace AI 95 } // namespace OHOS 96 97 #endif // ENGINE_H