1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2023 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef _LOS_SCHED_PRI_H
33 #define _LOS_SCHED_PRI_H
34
35 #include "los_sortlink_pri.h"
36 #include "los_sys_pri.h"
37 #include "los_hwi.h"
38 #include "hal_timer.h"
39 #ifdef LOSCFG_SCHED_DEBUG
40 #include "los_statistics_pri.h"
41 #endif
42 #include "los_stackinfo_pri.h"
43 #include "los_futex_pri.h"
44 #ifdef LOSCFG_KERNEL_PM
45 #include "los_pm_pri.h"
46 #endif
47 #include "los_signal.h"
48 #ifdef LOSCFG_KERNEL_CPUP
49 #include "los_cpup_pri.h"
50 #endif
51 #ifdef LOSCFG_KERNEL_LITEIPC
52 #include "hm_liteipc.h"
53 #endif
54 #include "los_mp.h"
55 #ifdef LOSCFG_KERNEL_CONTAINER
56 #include "los_container_pri.h"
57 #endif
58
59 #ifdef __cplusplus
60 #if __cplusplus
61 extern "C" {
62 #endif /* __cplusplus */
63 #endif /* __cplusplus */
64
65 #define OS_SCHED_MINI_PERIOD (OS_SYS_CLOCK / LOSCFG_BASE_CORE_TICK_PER_SECOND_MINI)
66 #define OS_TICK_RESPONSE_PRECISION (UINT32)((OS_SCHED_MINI_PERIOD * 75) / 100)
67 #define OS_SCHED_MAX_RESPONSE_TIME OS_SORT_LINK_INVALID_TIME
68 #define OS_SCHED_TICK_TO_CYCLE(ticks) ((UINT64)ticks * OS_CYCLE_PER_TICK)
69 #define AFFI_MASK_TO_CPUID(mask) ((UINT16)((mask) - 1))
70
71 #define OS_SCHED_EDF_MIN_RUNTIME 100 /* 100 us */
72 #define OS_SCHED_EDF_MIN_DEADLINE 400 /* 400 us */
73 #define OS_SCHED_EDF_MAX_DEADLINE 5000000 /* 5 s */
74
75 extern UINT32 g_taskScheduled;
76 #define OS_SCHEDULER_ACTIVE (g_taskScheduled & (1U << ArchCurrCpuid()))
77 #define OS_SCHEDULER_ALL_ACTIVE (g_taskScheduled == LOSCFG_KERNEL_CPU_MASK)
78
79 typedef struct TagTaskCB LosTaskCB;
80 typedef BOOL (*SCHED_TL_FIND_FUNC)(UINTPTR, UINTPTR);
81
OsGetCurrSchedTimeCycle(VOID)82 STATIC INLINE UINT64 OsGetCurrSchedTimeCycle(VOID)
83 {
84 return HalClockGetCycles();
85 }
86
87 typedef enum {
88 INT_NO_RESCH = 0x0, /* no needs to schedule */
89 INT_PEND_RESCH = 0x1, /* pending schedule flag */
90 INT_PEND_TICK = 0x2, /* pending tick */
91 } SchedFlag;
92
93 #define OS_PRIORITY_QUEUE_NUM 32
94 typedef struct {
95 LOS_DL_LIST priQueList[OS_PRIORITY_QUEUE_NUM];
96 UINT32 readyTasks[OS_PRIORITY_QUEUE_NUM];
97 UINT32 queueBitmap;
98 } HPFQueue;
99
100 typedef struct {
101 HPFQueue queueList[OS_PRIORITY_QUEUE_NUM];
102 UINT32 queueBitmap;
103 } HPFRunqueue;
104
105 typedef struct {
106 LOS_DL_LIST root;
107 LOS_DL_LIST waitList;
108 UINT64 period;
109 } EDFRunqueue;
110
111 typedef struct {
112 SortLinkAttribute timeoutQueue; /* task timeout queue */
113 HPFRunqueue *hpfRunqueue;
114 EDFRunqueue *edfRunqueue;
115 UINT64 responseTime; /* Response time for current CPU tick interrupts */
116 UINT32 responseID; /* The response ID of the current CPU tick interrupt */
117 LosTaskCB *idleTask; /* idle task id */
118 UINT32 taskLockCnt; /* task lock flag */
119 UINT32 schedFlag; /* pending scheduler flag */
120 } SchedRunqueue;
121
122 extern SchedRunqueue g_schedRunqueue[LOSCFG_KERNEL_CORE_NUM];
123
124 VOID OsSchedExpireTimeUpdate(VOID);
125
OsSchedRunqueue(VOID)126 STATIC INLINE SchedRunqueue *OsSchedRunqueue(VOID)
127 {
128 return &g_schedRunqueue[ArchCurrCpuid()];
129 }
130
OsSchedRunqueueByID(UINT16 id)131 STATIC INLINE SchedRunqueue *OsSchedRunqueueByID(UINT16 id)
132 {
133 return &g_schedRunqueue[id];
134 }
135
OsSchedLockCountGet(VOID)136 STATIC INLINE UINT32 OsSchedLockCountGet(VOID)
137 {
138 return OsSchedRunqueue()->taskLockCnt;
139 }
140
OsSchedLockSet(UINT32 count)141 STATIC INLINE VOID OsSchedLockSet(UINT32 count)
142 {
143 OsSchedRunqueue()->taskLockCnt = count;
144 }
145
OsSchedLock(VOID)146 STATIC INLINE VOID OsSchedLock(VOID)
147 {
148 OsSchedRunqueue()->taskLockCnt++;
149 }
150
OsSchedUnlock(VOID)151 STATIC INLINE VOID OsSchedUnlock(VOID)
152 {
153 OsSchedRunqueue()->taskLockCnt--;
154 }
155
OsSchedUnlockResch(VOID)156 STATIC INLINE BOOL OsSchedUnlockResch(VOID)
157 {
158 SchedRunqueue *rq = OsSchedRunqueue();
159 if (rq->taskLockCnt > 0) {
160 rq->taskLockCnt--;
161 if ((rq->taskLockCnt == 0) && (rq->schedFlag & INT_PEND_RESCH) && OS_SCHEDULER_ACTIVE) {
162 return TRUE;
163 }
164 }
165
166 return FALSE;
167 }
168
OsSchedIsLock(VOID)169 STATIC INLINE BOOL OsSchedIsLock(VOID)
170 {
171 return (OsSchedRunqueue()->taskLockCnt != 0);
172 }
173
174 /* Check if preemptible with counter flag */
OsPreemptable(VOID)175 STATIC INLINE BOOL OsPreemptable(VOID)
176 {
177 SchedRunqueue *rq = OsSchedRunqueue();
178 /*
179 * Unlike OsPreemptableInSched, the int may be not disabled when OsPreemptable
180 * is called, needs manually disable interrupt, to prevent current task from
181 * being migrated to another core, and get the wrong preemptable status.
182 */
183 UINT32 intSave = LOS_IntLock();
184 BOOL preemptible = (rq->taskLockCnt == 0);
185 if (!preemptible) {
186 /* Set schedule flag if preemption is disabled */
187 rq->schedFlag |= INT_PEND_RESCH;
188 }
189 LOS_IntRestore(intSave);
190 return preemptible;
191 }
192
OsPreemptableInSched(VOID)193 STATIC INLINE BOOL OsPreemptableInSched(VOID)
194 {
195 BOOL preemptible = FALSE;
196 SchedRunqueue *rq = OsSchedRunqueue();
197
198 #ifdef LOSCFG_KERNEL_SMP
199 /*
200 * For smp systems, schedule must hold the task spinlock, and this counter
201 * will increase by 1 in that case.
202 */
203 preemptible = (rq->taskLockCnt == 1);
204
205 #else
206 preemptible = (rq->taskLockCnt == 0);
207 #endif
208 if (!preemptible) {
209 /* Set schedule flag if preemption is disabled */
210 rq->schedFlag |= INT_PEND_RESCH;
211 }
212
213 return preemptible;
214 }
215
OsSchedRunqueueIdleGet(VOID)216 STATIC INLINE LosTaskCB *OsSchedRunqueueIdleGet(VOID)
217 {
218 return OsSchedRunqueue()->idleTask;
219 }
220
OsSchedRunqueuePendingSet(VOID)221 STATIC INLINE VOID OsSchedRunqueuePendingSet(VOID)
222 {
223 OsSchedRunqueue()->schedFlag |= INT_PEND_RESCH;
224 }
225
226 #define LOS_SCHED_NORMAL 0U
227 #define LOS_SCHED_FIFO 1U
228 #define LOS_SCHED_RR 2U
229 #define LOS_SCHED_IDLE 3U
230 #define LOS_SCHED_DEADLINE 6U
231
232 typedef struct {
233 UINT16 policy;
234 /* HPF scheduling parameters */
235 UINT16 basePrio;
236 UINT16 priority;
237 UINT32 timeSlice;
238
239 /* EDF scheduling parameters */
240 INT32 runTimeUs;
241 UINT32 deadlineUs;
242 UINT32 periodUs;
243 } SchedParam;
244
245 typedef struct {
246 UINT16 policy; /* This field must be present for all scheduling policies and must be the first in the structure */
247 UINT16 basePrio;
248 UINT16 priority;
249 UINT32 initTimeSlice; /* cycle */
250 UINT32 priBitmap; /* Bitmap for recording the change of task priority, the priority can not be greater than 31 */
251 } SchedHPF;
252
253 #define EDF_UNUSED 0
254 #define EDF_NEXT_PERIOD 1
255 #define EDF_WAIT_FOREVER 2
256 #define EDF_INIT 3
257 typedef struct {
258 UINT16 policy;
259 UINT16 cpuid;
260 UINT32 flags;
261 INT32 runTime; /* cycle */
262 UINT64 deadline; /* deadline >> runTime */
263 UINT64 period; /* period >= deadline */
264 UINT64 finishTime; /* startTime + deadline */
265 } SchedEDF;
266
267 typedef struct {
268 union {
269 SchedEDF edf;
270 SchedHPF hpf;
271 };
272 } SchedPolicy;
273
274 typedef struct {
275 VOID (*dequeue)(SchedRunqueue *rq, LosTaskCB *taskCB);
276 VOID (*enqueue)(SchedRunqueue *rq, LosTaskCB *taskCB);
277 VOID (*start)(SchedRunqueue *rq, LosTaskCB *taskCB);
278 VOID (*exit)(LosTaskCB *taskCB);
279 UINT64 (*waitTimeGet)(LosTaskCB *taskCB);
280 UINT32 (*wait)(LosTaskCB *runTask, LOS_DL_LIST *list, UINT32 timeout);
281 VOID (*wake)(LosTaskCB *taskCB);
282 BOOL (*schedParamModify)(LosTaskCB *taskCB, const SchedParam *param);
283 UINT32 (*schedParamGet)(const LosTaskCB *taskCB, SchedParam *param);
284 UINT32 (*delay)(LosTaskCB *taskCB, UINT64 waitTime);
285 VOID (*yield)(LosTaskCB *taskCB);
286 UINT32 (*suspend)(LosTaskCB *taskCB);
287 UINT32 (*resume)(LosTaskCB *taskCB, BOOL *needSched);
288 UINT64 (*deadlineGet)(const LosTaskCB *taskCB);
289 VOID (*timeSliceUpdate)(SchedRunqueue *rq, LosTaskCB *taskCB, UINT64 currTime);
290 INT32 (*schedParamCompare)(const SchedPolicy *sp1, const SchedPolicy *sp2);
291 VOID (*priorityInheritance)(LosTaskCB *owner, const SchedParam *param);
292 VOID (*priorityRestore)(LosTaskCB *owner, const LOS_DL_LIST *list, const SchedParam *param);
293 } SchedOps;
294
295 /**
296 * @ingroup los_sched
297 * Define a usable task priority.
298 *
299 * Highest task priority.
300 */
301 #define OS_TASK_PRIORITY_HIGHEST 0
302
303 /**
304 * @ingroup los_sched
305 * Define a usable task priority.
306 *
307 * Lowest task priority.
308 */
309 #define OS_TASK_PRIORITY_LOWEST 31
310
311 /**
312 * @ingroup los_sched
313 * Flag that indicates the task or task control block status.
314 *
315 * The task is init.
316 */
317 #define OS_TASK_STATUS_INIT 0x0001U
318
319 /**
320 * @ingroup los_sched
321 * Flag that indicates the task or task control block status.
322 *
323 * The task is ready.
324 */
325 #define OS_TASK_STATUS_READY 0x0002U
326
327 /**
328 * @ingroup los_sched
329 * Flag that indicates the task or task control block status.
330 *
331 * The task is running.
332 */
333 #define OS_TASK_STATUS_RUNNING 0x0004U
334
335 /**
336 * @ingroup los_sched
337 * Flag that indicates the task or task control block status.
338 *
339 * The task is suspended.
340 */
341 #define OS_TASK_STATUS_SUSPENDED 0x0008U
342
343 /**
344 * @ingroup los_sched
345 * Flag that indicates the task or task control block status.
346 *
347 * The task is blocked.
348 */
349 #define OS_TASK_STATUS_PENDING 0x0010U
350
351 /**
352 * @ingroup los_sched
353 * Flag that indicates the task or task control block status.
354 *
355 * The task is delayed.
356 */
357 #define OS_TASK_STATUS_DELAY 0x0020U
358
359 /**
360 * @ingroup los_sched
361 * Flag that indicates the task or task control block status.
362 *
363 * The time for waiting for an event to occur expires.
364 */
365 #define OS_TASK_STATUS_TIMEOUT 0x0040U
366
367 /**
368 * @ingroup los_sched
369 * Flag that indicates the task or task control block status.
370 *
371 * The task is pend for a period of time.
372 */
373 #define OS_TASK_STATUS_PEND_TIME 0x0080U
374
375 /**
376 * @ingroup los_sched
377 * Flag that indicates the task or task control block status.
378 *
379 * The task is exit.
380 */
381 #define OS_TASK_STATUS_EXIT 0x0100U
382
383 #define OS_TASK_STATUS_BLOCKED (OS_TASK_STATUS_INIT | OS_TASK_STATUS_PENDING | \
384 OS_TASK_STATUS_DELAY | OS_TASK_STATUS_PEND_TIME)
385
386 /**
387 * @ingroup los_task
388 * Flag that indicates the task or task control block status.
389 *
390 * The delayed operation of this task is frozen.
391 */
392 #define OS_TASK_STATUS_FROZEN 0x0200U
393
394 #define OS_TCB_NAME_LEN 32
395
396 typedef struct TagTaskCB {
397 VOID *stackPointer; /**< Task stack pointer */
398 UINT16 taskStatus; /**< Task status */
399
400 UINT64 startTime; /**< The start time of each phase of task */
401 UINT64 waitTime; /**< Task delay time, tick number */
402 UINT64 irqStartTime; /**< Interrupt start time */
403 UINT32 irqUsedTime; /**< Interrupt consumption time */
404 INT32 timeSlice; /**< Task remaining time slice */
405 SortLinkList sortList; /**< Task sortlink node */
406 const SchedOps *ops;
407 SchedPolicy sp;
408
409 UINT32 stackSize; /**< Task stack size */
410 UINTPTR topOfStack; /**< Task stack top */
411 UINT32 taskID; /**< Task ID */
412 TSK_ENTRY_FUNC taskEntry; /**< Task entrance function */
413 VOID *joinRetval; /**< pthread adaption */
414 VOID *taskMux; /**< Task-held mutex */
415 VOID *taskEvent; /**< Task-held event */
416 UINTPTR args[4]; /**< Parameter, of which the maximum number is 4 */
417 CHAR taskName[OS_TCB_NAME_LEN]; /**< Task name */
418 LOS_DL_LIST pendList; /**< Task pend node */
419 LOS_DL_LIST threadList; /**< thread list */
420 UINT32 eventMask; /**< Event mask */
421 UINT32 eventMode; /**< Event mode */
422 #ifdef LOSCFG_KERNEL_CPUP
423 OsCpupBase taskCpup; /**< task cpu usage */
424 #endif
425 INT32 errorNo; /**< Error Num */
426 UINT32 signal; /**< Task signal */
427 sig_cb sig;
428 #ifdef LOSCFG_KERNEL_SMP
429 UINT16 currCpu; /**< CPU core number of this task is running on */
430 UINT16 lastCpu; /**< CPU core number of this task is running on last time */
431 UINT16 cpuAffiMask; /**< CPU affinity mask, support up to 16 cores */
432 #ifdef LOSCFG_KERNEL_SMP_TASK_SYNC
433 UINT32 syncSignal; /**< Synchronization for signal handling */
434 #endif
435 #ifdef LOSCFG_KERNEL_SMP_LOCKDEP
436 LockDep lockDep;
437 #endif
438 #endif
439 #ifdef LOSCFG_SCHED_DEBUG
440 SchedStat schedStat; /**< Schedule statistics */
441 #endif
442 #ifdef LOSCFG_KERNEL_VM
443 UINTPTR archMmu;
444 UINTPTR userArea;
445 UINTPTR userMapBase;
446 UINT32 userMapSize; /**< user thread stack size ,real size : userMapSize + USER_STACK_MIN_SIZE */
447 FutexNode futex;
448 #endif
449 UINTPTR processCB; /**< Which belong process */
450 LOS_DL_LIST joinList; /**< join list */
451 LOS_DL_LIST lockList; /**< Hold the lock list */
452 UINTPTR waitID; /**< Wait for the PID or GID of the child process */
453 UINT16 waitFlag; /**< The type of child process that is waiting, belonging to a group or parent,
454 a specific child process, or any child process */
455 #ifdef LOSCFG_KERNEL_LITEIPC
456 IpcTaskInfo *ipcTaskInfo;
457 #endif
458 #ifdef LOSCFG_KERNEL_PERF
459 UINTPTR pc;
460 UINTPTR fp;
461 #endif
462 #ifdef LOSCFG_PID_CONTAINER
463 PidContainer *pidContainer;
464 #endif
465 #ifdef LOSCFG_IPC_CONTAINER
466 BOOL cloneIpc;
467 #endif
468 } LosTaskCB;
469
OsTaskIsRunning(const LosTaskCB * taskCB)470 STATIC INLINE BOOL OsTaskIsRunning(const LosTaskCB *taskCB)
471 {
472 return ((taskCB->taskStatus & OS_TASK_STATUS_RUNNING) != 0);
473 }
474
OsTaskIsReady(const LosTaskCB * taskCB)475 STATIC INLINE BOOL OsTaskIsReady(const LosTaskCB *taskCB)
476 {
477 return ((taskCB->taskStatus & OS_TASK_STATUS_READY) != 0);
478 }
479
OsTaskIsInactive(const LosTaskCB * taskCB)480 STATIC INLINE BOOL OsTaskIsInactive(const LosTaskCB *taskCB)
481 {
482 return ((taskCB->taskStatus & (OS_TASK_STATUS_INIT | OS_TASK_STATUS_EXIT)) != 0);
483 }
484
OsTaskIsPending(const LosTaskCB * taskCB)485 STATIC INLINE BOOL OsTaskIsPending(const LosTaskCB *taskCB)
486 {
487 return ((taskCB->taskStatus & OS_TASK_STATUS_PENDING) != 0);
488 }
489
OsTaskIsSuspended(const LosTaskCB * taskCB)490 STATIC INLINE BOOL OsTaskIsSuspended(const LosTaskCB *taskCB)
491 {
492 return ((taskCB->taskStatus & OS_TASK_STATUS_SUSPENDED) != 0);
493 }
494
OsTaskIsBlocked(const LosTaskCB * taskCB)495 STATIC INLINE BOOL OsTaskIsBlocked(const LosTaskCB *taskCB)
496 {
497 return ((taskCB->taskStatus & (OS_TASK_STATUS_SUSPENDED | OS_TASK_STATUS_PENDING | OS_TASK_STATUS_DELAY)) != 0);
498 }
499
OsSchedPolicyIsEDF(const LosTaskCB * taskCB)500 STATIC INLINE BOOL OsSchedPolicyIsEDF(const LosTaskCB *taskCB)
501 {
502 const SchedEDF *sched = (const SchedEDF *)&taskCB->sp;
503 return (sched->policy == LOS_SCHED_DEADLINE);
504 }
505
OsCurrTaskGet(VOID)506 STATIC INLINE LosTaskCB *OsCurrTaskGet(VOID)
507 {
508 return (LosTaskCB *)ArchCurrTaskGet();
509 }
510
OsCurrTaskSet(LosTaskCB * task)511 STATIC INLINE VOID OsCurrTaskSet(LosTaskCB *task)
512 {
513 ArchCurrTaskSet(task);
514 }
515
OsCurrUserTaskSet(UINTPTR thread)516 STATIC INLINE VOID OsCurrUserTaskSet(UINTPTR thread)
517 {
518 ArchCurrUserTaskSet(thread);
519 }
520
OsSchedIrqUsedTimeUpdate(VOID)521 STATIC INLINE VOID OsSchedIrqUsedTimeUpdate(VOID)
522 {
523 LosTaskCB *runTask = OsCurrTaskGet();
524 runTask->irqUsedTime = OsGetCurrSchedTimeCycle() - runTask->irqStartTime;
525 }
526
OsSchedIrqStartTime(VOID)527 STATIC INLINE VOID OsSchedIrqStartTime(VOID)
528 {
529 LosTaskCB *runTask = OsCurrTaskGet();
530 runTask->irqStartTime = OsGetCurrSchedTimeCycle();
531 }
532
533 #ifdef LOSCFG_KERNEL_SMP
IdleRunqueueFind(UINT16 * idleCpuid)534 STATIC INLINE VOID IdleRunqueueFind(UINT16 *idleCpuid)
535 {
536 SchedRunqueue *idleRq = OsSchedRunqueueByID(0);
537 UINT32 nodeNum = OsGetSortLinkNodeNum(&idleRq->timeoutQueue);
538 UINT16 cpuid = 1;
539 do {
540 SchedRunqueue *rq = OsSchedRunqueueByID(cpuid);
541 UINT32 temp = OsGetSortLinkNodeNum(&rq->timeoutQueue);
542 if (nodeNum > temp) {
543 *idleCpuid = cpuid;
544 nodeNum = temp;
545 }
546 cpuid++;
547 } while (cpuid < LOSCFG_KERNEL_CORE_NUM);
548 }
549 #endif
550
OsSchedTimeoutQueueAdd(LosTaskCB * taskCB,UINT64 responseTime)551 STATIC INLINE VOID OsSchedTimeoutQueueAdd(LosTaskCB *taskCB, UINT64 responseTime)
552 {
553 #ifdef LOSCFG_KERNEL_SMP
554 UINT16 cpuid = AFFI_MASK_TO_CPUID(taskCB->cpuAffiMask);
555 if (cpuid >= LOSCFG_KERNEL_CORE_NUM) {
556 cpuid = 0;
557 IdleRunqueueFind(&cpuid);
558 }
559 #else
560 UINT16 cpuid = 0;
561 #endif
562
563 SchedRunqueue *rq = OsSchedRunqueueByID(cpuid);
564 OsAdd2SortLink(&rq->timeoutQueue, &taskCB->sortList, responseTime, cpuid);
565 #ifdef LOSCFG_KERNEL_SMP
566 if ((cpuid != ArchCurrCpuid()) && (responseTime < rq->responseTime)) {
567 rq->schedFlag |= INT_PEND_TICK;
568 LOS_MpSchedule(CPUID_TO_AFFI_MASK(cpuid));
569 }
570 #endif
571 }
572
OsSchedTimeoutQueueDelete(LosTaskCB * taskCB)573 STATIC INLINE VOID OsSchedTimeoutQueueDelete(LosTaskCB *taskCB)
574 {
575 SortLinkList *node = &taskCB->sortList;
576 #ifdef LOSCFG_KERNEL_SMP
577 SchedRunqueue *rq = OsSchedRunqueueByID(node->cpuid);
578 #else
579 SchedRunqueue *rq = OsSchedRunqueueByID(0);
580 #endif
581 UINT64 oldResponseTime = GET_SORTLIST_VALUE(node);
582 OsDeleteFromSortLink(&rq->timeoutQueue, node);
583 if (oldResponseTime <= rq->responseTime) {
584 rq->responseTime = OS_SCHED_MAX_RESPONSE_TIME;
585 }
586 }
587
OsSchedTimeoutQueueAdjust(LosTaskCB * taskCB,UINT64 responseTime)588 STATIC INLINE UINT32 OsSchedTimeoutQueueAdjust(LosTaskCB *taskCB, UINT64 responseTime)
589 {
590 UINT32 ret;
591 SortLinkList *node = &taskCB->sortList;
592 #ifdef LOSCFG_KERNEL_SMP
593 UINT16 cpuid = node->cpuid;
594 #else
595 UINT16 cpuid = 0;
596 #endif
597 SchedRunqueue *rq = OsSchedRunqueueByID(cpuid);
598 ret = OsSortLinkAdjustNodeResponseTime(&rq->timeoutQueue, node, responseTime);
599 if (ret == LOS_OK) {
600 rq->schedFlag |= INT_PEND_TICK;
601 }
602 return ret;
603 }
604
SchedTaskFreeze(LosTaskCB * taskCB)605 STATIC INLINE VOID SchedTaskFreeze(LosTaskCB *taskCB)
606 {
607 UINT64 responseTime;
608
609 #ifdef LOSCFG_KERNEL_PM
610 if (!OsIsPmMode()) {
611 return;
612 }
613 #endif
614
615 if (!(taskCB->taskStatus & (OS_TASK_STATUS_PEND_TIME | OS_TASK_STATUS_DELAY))) {
616 return;
617 }
618
619 responseTime = GET_SORTLIST_VALUE(&taskCB->sortList);
620 OsSchedTimeoutQueueDelete(taskCB);
621 SET_SORTLIST_VALUE(&taskCB->sortList, responseTime);
622 taskCB->taskStatus |= OS_TASK_STATUS_FROZEN;
623 return;
624 }
625
SchedTaskUnfreeze(LosTaskCB * taskCB)626 STATIC INLINE VOID SchedTaskUnfreeze(LosTaskCB *taskCB)
627 {
628 UINT64 currTime, responseTime;
629
630 if (!(taskCB->taskStatus & OS_TASK_STATUS_FROZEN)) {
631 return;
632 }
633
634 taskCB->taskStatus &= ~OS_TASK_STATUS_FROZEN;
635 currTime = OsGetCurrSchedTimeCycle();
636 responseTime = GET_SORTLIST_VALUE(&taskCB->sortList);
637 if (responseTime > currTime) {
638 OsSchedTimeoutQueueAdd(taskCB, responseTime);
639 return;
640 }
641
642 SET_SORTLIST_VALUE(&taskCB->sortList, OS_SORT_LINK_INVALID_TIME);
643 if (taskCB->taskStatus & OS_TASK_STATUS_PENDING) {
644 LOS_ListDelete(&taskCB->pendList);
645 }
646 taskCB->taskStatus &= ~OS_TASK_STATUS_BLOCKED;
647 return;
648 }
649
650 /*
651 * Schedule flag, one bit represents one core.
652 * This flag is used to prevent kernel scheduling before OSStartToRun.
653 */
654 #define OS_SCHEDULER_SET(cpuid) do { \
655 g_taskScheduled |= (1U << (cpuid)); \
656 } while (0);
657
658 #define OS_SCHEDULER_CLR(cpuid) do { \
659 g_taskScheduled &= ~(1U << (cpuid)); \
660 } while (0);
661
662 #ifdef LOSCFG_KERNEL_SCHED_PLIMIT
663 BOOL OsSchedLimitCheckTime(LosTaskCB *task);
664 #endif
665
EDFRunqueueTopTaskGet(EDFRunqueue * rq)666 STATIC INLINE LosTaskCB *EDFRunqueueTopTaskGet(EDFRunqueue *rq)
667 {
668 LOS_DL_LIST *root = &rq->root;
669 if (LOS_ListEmpty(root)) {
670 return NULL;
671 }
672
673 return LOS_DL_LIST_ENTRY(LOS_DL_LIST_FIRST(root), LosTaskCB, pendList);
674 }
675
HPFRunqueueTopTaskGet(HPFRunqueue * rq)676 STATIC INLINE LosTaskCB *HPFRunqueueTopTaskGet(HPFRunqueue *rq)
677 {
678 LosTaskCB *newTask = NULL;
679 UINT32 baseBitmap = rq->queueBitmap;
680 #ifdef LOSCFG_KERNEL_SMP
681 UINT32 cpuid = ArchCurrCpuid();
682 #endif
683
684 while (baseBitmap) {
685 UINT32 basePrio = CLZ(baseBitmap);
686 HPFQueue *queueList = &rq->queueList[basePrio];
687 UINT32 bitmap = queueList->queueBitmap;
688 while (bitmap) {
689 UINT32 priority = CLZ(bitmap);
690 LOS_DL_LIST_FOR_EACH_ENTRY(newTask, &queueList->priQueList[priority], LosTaskCB, pendList) {
691 #ifdef LOSCFG_KERNEL_SCHED_PLIMIT
692 if (!OsSchedLimitCheckTime(newTask)) {
693 bitmap &= ~(1U << (OS_PRIORITY_QUEUE_NUM - priority - 1));
694 continue;
695 }
696 #endif
697 #ifdef LOSCFG_KERNEL_SMP
698 if (newTask->cpuAffiMask & (1U << cpuid)) {
699 #endif
700 return newTask;
701 #ifdef LOSCFG_KERNEL_SMP
702 }
703 #endif
704 }
705 bitmap &= ~(1U << (OS_PRIORITY_QUEUE_NUM - priority - 1));
706 }
707 baseBitmap &= ~(1U << (OS_PRIORITY_QUEUE_NUM - basePrio - 1));
708 }
709
710 return NULL;
711 }
712
713 VOID EDFProcessDefaultSchedParamGet(SchedParam *param);
714 VOID EDFSchedPolicyInit(SchedRunqueue *rq);
715 UINT32 EDFTaskSchedParamInit(LosTaskCB *taskCB, UINT16 policy,
716 const SchedParam *parentParam,
717 const LosSchedParam *param);
718
719 VOID HPFSchedPolicyInit(SchedRunqueue *rq);
720 VOID HPFTaskSchedParamInit(LosTaskCB *taskCB, UINT16 policy,
721 const SchedParam *parentParam,
722 const LosSchedParam *param);
723 VOID HPFProcessDefaultSchedParamGet(SchedParam *param);
724
725 VOID IdleTaskSchedParamInit(LosTaskCB *taskCB);
726
727 INT32 OsSchedParamCompare(const LosTaskCB *task1, const LosTaskCB *task2);
728 VOID OsSchedPriorityInheritance(LosTaskCB *owner, const SchedParam *param);
729 UINT32 OsSchedParamInit(LosTaskCB *taskCB, UINT16 policy,
730 const SchedParam *parentParam,
731 const LosSchedParam *param);
732 VOID OsSchedProcessDefaultSchedParamGet(UINT16 policy, SchedParam *param);
733
734 VOID OsSchedResponseTimeReset(UINT64 responseTime);
735 VOID OsSchedToUserReleaseLock(VOID);
736 VOID OsSchedTick(VOID);
737 UINT32 OsSchedInit(VOID);
738 VOID OsSchedStart(VOID);
739
740 VOID OsSchedRunqueueIdleInit(LosTaskCB *idleTask);
741 VOID OsSchedRunqueueInit(VOID);
742
743 /*
744 * This function simply picks the next task and switches to it.
745 * Current task needs to already be in the right state or the right
746 * queues it needs to be in.
747 */
748 VOID OsSchedResched(VOID);
749 VOID OsSchedIrqEndCheckNeedSched(VOID);
750
751 /*
752 * This function inserts the runTask to the lock pending list based on the
753 * task priority.
754 */
755 LOS_DL_LIST *OsSchedLockPendFindPos(const LosTaskCB *runTask, LOS_DL_LIST *lockList);
756
757 #ifdef __cplusplus
758 #if __cplusplus
759 }
760 #endif /* __cplusplus */
761 #endif /* __cplusplus */
762
763 #endif /* _LOS_SCHED_PRI_H */
764