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/engine_worker.h" 17 18 #include "platform/time/include/time.h" 19 #include "server_executor/include/i_handler.h" 20 #include "utils/log/aie_log.h" 21 22 namespace OHOS { 23 namespace AI { 24 namespace { 25 const char * const ENGINE_WORKER_NAME = "EngineWorker"; 26 } 27 EngineWorker(Queue<Task> & queue)28EngineWorker::EngineWorker(Queue<Task> &queue) : queue_(queue) 29 { 30 } 31 ClearQueue(Queue<Task> & queue)32static void ClearQueue(Queue<Task> &queue) 33 { 34 size_t size = queue.Count(); 35 for (size_t i = 0; i < size; ++i) { 36 Task task; 37 int retCode = queue.PopFront(task); 38 if (retCode != RETCODE_SUCCESS) { 39 break; 40 } 41 IRequest::Destroy(task.request); 42 } 43 } 44 GetName() const45const char *EngineWorker::GetName() const 46 { 47 return ENGINE_WORKER_NAME; 48 } 49 OneAction()50bool EngineWorker::OneAction() 51 { 52 if (queue_.IsEmpty()) { 53 StepSleepMs(1); 54 CHK_RET(queue_.IsEmpty(), true); 55 } 56 57 Task task; 58 int retCode = queue_.PopFront(task); 59 if (retCode != RETCODE_SUCCESS) { 60 HILOGE("[EngineWorker]Fetch task from queue failed. error code is [%d].", retCode); 61 return true; 62 } 63 64 if (task.handler == nullptr) { 65 HILOGE("[EngineWorker]The handler is null."); 66 return true; 67 } 68 69 retCode = task.handler->Process(task); 70 if (retCode != RETCODE_SUCCESS) { 71 HILOGE("[EngineWorker]Failed to process task."); 72 } 73 return true; 74 } 75 Uninitialize()76void EngineWorker::Uninitialize() 77 { 78 ClearQueue(queue_); 79 } 80 } // namespace AI 81 } // namespace OHOS