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 OHOS_ACELITE_ASYNC_TASK_MANAGER_H 17 #define OHOS_ACELITE_ASYNC_TASK_MANAGER_H 18 19 #if (defined(__LINUX__) || defined(__LITEOS_A__)) 20 #include <pthread.h> 21 #elif defined(__LITEOS_M__) 22 #include "los_task.h" 23 #endif 24 25 #include "common/task_manager.h" 26 #include "memory_heap.h" 27 #include "non_copyable.h" 28 29 namespace OHOS { 30 namespace ACELite { 31 using AsyncTaskHandler = void (*)(void *); 32 33 struct AsyncTask final : public MemoryHeap { 34 uint16_t id; 35 const void *context; 36 AsyncTaskHandler handler; 37 void *data; 38 AsyncTask *next; 39 bool isRunning; 40 }; 41 42 constexpr uint16_t DISPATCH_FAILURE = 0; 43 44 class AsyncTaskManager final : public Task { 45 public: 46 ACE_DISALLOW_COPY_AND_MOVE(AsyncTaskManager); 47 48 static AsyncTaskManager &GetInstance(); 49 50 void Init() override; 51 52 void Callback() override; 53 54 uint16_t Dispatch(AsyncTaskHandler handler, void *data, const void *context = nullptr); 55 56 void Cancel(uint16_t taskID); 57 58 void CancelWithContext(const void *context); 59 60 void SetFront(bool front); 61 IsFront()62 bool IsFront() const 63 { 64 return front_; 65 } 66 67 private: 68 AsyncTaskManager(); 69 70 ~AsyncTaskManager() = default; 71 72 void Reset(); 73 74 AsyncTask *head_; 75 AsyncTask *tail_; 76 #if (defined(__LINUX__) || defined(__LITEOS_A__)) 77 pthread_mutex_t lock_; 78 #endif 79 uint16_t uniqueTaskID_; 80 bool front_; 81 bool initialized_; 82 }; 83 } // namespace ACELite 84 } // namespace OHOS 85 #endif 86