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 #include "server_executor/include/sync_msg_handler.h"
17
18 #include "platform/queuepool/queue_pool.h"
19 #include "platform/semaphore/include/simple_event_notifier.h"
20 #include "plugin/i_plugin.h"
21 #include "server_executor/include/engine_manager.h"
22 #include "utils/aie_guard.h"
23 #include "utils/constants/constants.h"
24 #include "utils/log/aie_log.h"
25
26 namespace OHOS {
27 namespace AI {
SyncMsgHandler(Queue<Task> & queue,IPlugin * pluginAlgorithm)28 SyncMsgHandler::SyncMsgHandler(Queue<Task> &queue, IPlugin *pluginAlgorithm)
29 : queue_(queue), pluginAlgorithm_(pluginAlgorithm)
30 {
31 }
32
Process(const Task & task)33 int SyncMsgHandler::Process(const Task &task)
34 {
35 CHK_RET(pluginAlgorithm_ == nullptr, RETCODE_PLUGIN_LOAD_FAILED);
36
37 IRequest *request = task.request;
38 if (request == nullptr) {
39 HILOGE("[SyncMsgHandler]Invalid request param");
40 return RETCODE_NULL_PARAM;
41 }
42
43 IResponse *response = nullptr;
44 int processRetCode = pluginAlgorithm_->SyncProcess(request, response);
45
46 if (response == nullptr) {
47 response = IResponse::Create(task.request);
48 CHK_RET(response == nullptr, RETCODE_OUT_OF_MEMORY);
49 }
50
51 if (processRetCode != RETCODE_SUCCESS) {
52 response->SetRetCode(RETCODE_ALGORITHM_PROCESS_ERROR);
53 } else {
54 response->SetRetCode(RETCODE_SUCCESS);
55 }
56
57 if (task.notifier != nullptr) {
58 (task.notifier)->AddToBack(response);
59 } else {
60 IResponse::Destroy(response);
61 }
62
63 return processRetCode;
64 }
65
SetPluginAlgorithm(IPlugin * pluginAlgorithm)66 void SyncMsgHandler::SetPluginAlgorithm(IPlugin *pluginAlgorithm)
67 {
68 pluginAlgorithm_ = pluginAlgorithm;
69 }
70
GetSynMsgQueueCapacity()71 size_t GetSynMsgQueueCapacity()
72 {
73 return MAX_SYNC_MSG_NUM;
74 }
75
SendRequest(IRequest * request,SimpleEventNotifier<IResponse> & notifier)76 int SyncMsgHandler::SendRequest(IRequest *request, SimpleEventNotifier<IResponse> ¬ifier)
77 {
78 if (request == nullptr) {
79 HILOGE("[SyncMsgHandler]Invalid request param");
80 return RETCODE_NULL_PARAM;
81 }
82
83 if (queue_.Count() >= GetSynMsgQueueCapacity()) {
84 HILOGE("[SyncMsgHandler]Queue overload");
85 return RETCODE_QUEUE_FULL;
86 }
87
88 Task task(this, request, ¬ifier);
89 int retCode = queue_.PushBack(task);
90 if (retCode != RETCODE_SUCCESS) {
91 HILOGI("[SyncMsgHandler]Push sync msg result is %d.", retCode);
92 }
93 return retCode;
94 }
95
ReceiveResponse(int timeOut,SimpleEventNotifier<IResponse> & notifier,IResponse * & response)96 int SyncMsgHandler::ReceiveResponse(int timeOut, SimpleEventNotifier<IResponse> ¬ifier,
97 IResponse *&response)
98 {
99 bool notifierCode = notifier.GetFromFront(timeOut, response);
100 if (!notifierCode) {
101 HILOGI("[SyncMsgHandler]Sync ReceiveResponse timeout.");
102 return RETCODE_SYNC_MSG_TIMEOUT;
103 }
104 return RETCODE_SUCCESS;
105 }
106 } // namespace AI
107 } // namespace OHOS