• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #if defined(__LITEOS_M__)
30 extern void LOS_TaskLock(void);
31 extern void LOS_TaskUnlock(void);
32 #endif
33 
34 namespace OHOS {
35 namespace ACELite {
36 class TaskLockGuard {
37 public:
TaskLockGuard(void * lock)38     explicit TaskLockGuard(void* lock): lock_(lock)
39     {
40 #if (defined(__LINUX__) || defined(__LITEOS_A__))
41         pthread_mutex_lock((pthread_mutex_t*)lock_);
42 #elif defined(__LITEOS_M__)
43         LOS_TaskLock();
44 #endif
45     }
~TaskLockGuard()46     ~TaskLockGuard()
47     {
48 #if (defined(__LINUX__) || defined(__LITEOS_A__))
49         pthread_mutex_unlock((pthread_mutex_t*)lock_);
50 #elif defined(__LITEOS_M__)
51         LOS_TaskUnlock();
52 #endif
53         lock_ = nullptr;
54     }
55     TaskLockGuard(const TaskLockGuard&) = delete;
56     TaskLockGuard& operator=(const TaskLockGuard&) = delete;
57 private:
58     void* lock_ = nullptr;
59 };
60 
61 
62 using AsyncTaskHandler = void (*)(void *);
63 
64 struct AsyncTask final : public MemoryHeap {
65     uint16_t id;
66     const void *context;
67     AsyncTaskHandler handler;
68     void *data;
69     AsyncTask *next;
70     bool isRunning;
71 };
72 
73 constexpr uint16_t DISPATCH_FAILURE = 0;
74 
75 class AsyncTaskManager final : public Task {
76 public:
77     ACE_DISALLOW_COPY_AND_MOVE(AsyncTaskManager);
78 
79     static AsyncTaskManager &GetInstance();
80 
81     void Init() override;
82 
83     void Callback() override;
84 
85     uint16_t Dispatch(AsyncTaskHandler handler, void *data, const void *context = nullptr);
86 
87     void Cancel(uint16_t taskID);
88 
89     void CancelWithContext(const void *context);
90 
91     void SetFront(bool front);
92 
IsFront()93     bool IsFront() const
94     {
95         return front_;
96     }
97 
98 private:
99     AsyncTaskManager();
100 
101     ~AsyncTaskManager() = default;
102 
103     void Reset();
104 
105     AsyncTask *head_;
106     AsyncTask *tail_;
107 #if (defined(__LINUX__) || defined(__LITEOS_A__))
108     pthread_mutex_t lock_;
109 #endif
110     uint16_t uniqueTaskID_;
111     bool front_;
112     bool initialized_;
113 };
114 } // namespace ACELite
115 } // namespace OHOS
116 #endif
117