• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 FFRT_TASK_SCHEDULER_HPP
17 #define FFRT_TASK_SCHEDULER_HPP
18 
19 #include <mutex>
20 
21 #include "core/entity.h"
22 #include "sched/task_runqueue.h"
23 #include "eu/worker_thread.h"
24 #include "sync/sync.h"
25 #include "sync/semaphore.h"
26 #include "dfx/trace/ffrt_trace.h"
27 
28 namespace ffrt {
29 
30 template <typename Sched>
31 class TaskScheduler {
32 public:
33     virtual ~TaskScheduler() = default;
34 
PickNextTask()35     TaskCtx* PickNextTask()
36     {
37         std::unique_lock lock(mutex);
38         return static_cast<Sched*>(this)->PickNextTaskImpl();
39     }
40 
WakeupTask(TaskCtx * task)41     bool WakeupTask(TaskCtx* task)
42     {
43         bool ret = false;
44         {
45             FFRT_READY_MARKER(task->gid);
46             std::unique_lock lock(mutex);
47             ret = static_cast<Sched*>(this)->WakeupTaskImpl(task);
48         }
49         return ret;
50     }
51 
WakeupNode(LinkedList * node)52     bool WakeupNode(LinkedList* node)
53     {
54         bool ret = false;
55         {
56             std::unique_lock lock(mutex);
57             ret = static_cast<Sched*>(this)->WakeupNodeImpl(node);
58         }
59         return ret;
60     }
61 
RemoveNode(LinkedList * node)62     bool RemoveNode(LinkedList* node)
63     {
64         bool ret = false;
65         {
66             std::unique_lock lock(mutex);
67             ret = static_cast<Sched*>(this)->RemoveNodeImpl(node);
68         }
69         return ret;
70     }
71 
RQEmpty()72     bool RQEmpty()
73     {
74         return static_cast<Sched*>(this)->RQEmptyImpl();
75     }
76 
RQSize()77     int RQSize()
78     {
79         return static_cast<Sched*>(this)->RQSizeImpl();
80     }
81 
82 private:
83     fast_mutex mutex;
84     semaphore sem;
85 };
86 
87 class FIFOScheduler : public TaskScheduler<FIFOScheduler> {
88     friend class TaskScheduler<FIFOScheduler>;
89 
90 private:
PickNextTaskImpl()91     TaskCtx* PickNextTaskImpl()
92     {
93         TaskCtx* task = que.DeQueue();
94         return task;
95     }
96 
WakeupNodeImpl(LinkedList * node)97     bool WakeupNodeImpl(LinkedList* node)
98     {
99         que.EnQueueNode(node);
100         return true;
101     }
102 
RemoveNodeImpl(LinkedList * node)103     bool RemoveNodeImpl(LinkedList* node)
104     {
105         que.RmQueueNode(node);
106         return true;
107     }
108 
WakeupTaskImpl(TaskCtx * task)109     bool WakeupTaskImpl(TaskCtx* task)
110     {
111         que.EnQueue(task);
112         return true;
113     }
114 
RQEmptyImpl()115     bool RQEmptyImpl()
116     {
117         return que.Empty();
118     }
119 
RQSizeImpl()120     int RQSizeImpl()
121     {
122         return que.Size();
123     }
124 
125     FIFOQueue que;
126 };
127 
128 } // namespace ffrt
129 
130 #endif
131