| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2022-2023, The Puppy RTOS Authors | ||
| 3 | * | ||
| 4 | * SPDX-License-Identifier: Apache-2.0 | ||
| 5 | */ | ||
| 6 | |||
| 7 | #include <puppy.h> | ||
| 8 | #include <puppy/kobj.h> | ||
| 9 | |||
| 10 | #define KLOG_TAG "sched_fool" | ||
| 11 | #define KLOG_LVL KLOG_WARNING | ||
| 12 | #include <puppy/klog.h> | ||
| 13 | |||
| 14 | 329 | int p_sched_ready_insert(p_obj_t thread) | |
| 15 | { | ||
| 16 | static uint8_t cpuid_last = 0; | ||
| 17 | 329 | struct _thread_obj *old_thread = NULL, *temp_thread = NULL,*_thread = thread; | |
| 18 | 329 | p_base_t key = arch_irq_lock(); | |
| 19 | 330 | arch_spin_lock(&cpu); | |
| 20 | |||
| 21 | p_node_t *node; | ||
| 22 | p_list_t *_ready_queue; | ||
| 23 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 330 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
|
330 | KLOG_ASSERT(_thread != NULL); |
| 24 |
1/6✗ Branch 1 not taken.
✓ Branch 2 taken 330 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
|
330 | KLOG_ASSERT(p_obj_get_type(_thread) == P_OBJ_TYPE_THREAD); |
| 25 | |||
| 26 | KLOG_D("p_sched_ready_insert:thread:%x,key:%x,bindcpu:%d", _thread, key, _thread->bindcpu); | ||
| 27 | |||
| 28 | #if P_CPU_NR > 1 // todo | ||
| 29 | 330 | uint8_t need_send = CPU_NA; | |
| 30 |
2/2✓ Branch 0 taken 302 times.
✓ Branch 1 taken 28 times.
|
330 | if (_thread->oncpu != CPU_NA) |
| 31 | { | ||
| 32 | 302 | cpuid_last = _thread->oncpu; | |
| 33 | } | ||
| 34 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 24 times.
|
28 | else if (_thread->bindcpu != CPU_NA) //todo support cpu_mask |
| 35 | { | ||
| 36 | 4 | cpuid_last = _thread->bindcpu; | |
| 37 | } | ||
| 38 | #endif | ||
| 39 | 330 | _ready_queue = &p_cpu_index(cpuid_last)->ready_queue; | |
| 40 | |||
| 41 |
2/2✓ Branch 0 taken 77 times.
✓ Branch 1 taken 311 times.
|
388 | p_list_for_each_node(_ready_queue, node) |
| 42 | { | ||
| 43 | 77 | temp_thread = p_list_entry(node, struct _thread_obj, tnode); | |
| 44 |
1/6✗ Branch 1 not taken.
✓ Branch 2 taken 77 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
|
77 | KLOG_ASSERT(p_obj_get_type(temp_thread) == P_OBJ_TYPE_THREAD); |
| 45 |
2/2✓ Branch 0 taken 19 times.
✓ Branch 1 taken 58 times.
|
77 | if (temp_thread->prio > _thread->prio) |
| 46 | { | ||
| 47 | /* find out insert node */ | ||
| 48 | 19 | old_thread = temp_thread; | |
| 49 | 19 | break; | |
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | 330 | _thread->state = P_THREAD_STATE_READY; | |
| 54 |
2/2✓ Branch 0 taken 19 times.
✓ Branch 1 taken 311 times.
|
330 | if (old_thread) |
| 55 | { | ||
| 56 | 19 | p_list_insert(&old_thread->tnode, &_thread->tnode); | |
| 57 | } | ||
| 58 | else | ||
| 59 | { | ||
| 60 | 311 | p_list_append(_ready_queue, &_thread->tnode); | |
| 61 | } | ||
| 62 | |||
| 63 | KLOG_D("p_sched_ready_insert done:_ready_queue->head:%x", _ready_queue->head); | ||
| 64 | |||
| 65 | #if P_CPU_NR > 1 // todo | ||
| 66 |
2/2✓ Branch 1 taken 125 times.
✓ Branch 2 taken 205 times.
|
330 | if(cpuid_last != p_cpu_self_id()) |
| 67 | { | ||
| 68 | KLOG_D("need send ipi"); | ||
| 69 | 125 | need_send = cpuid_last; | |
| 70 | } | ||
| 71 | 330 | cpuid_last = (cpuid_last + 1) % P_CPU_NR; | |
| 72 | #endif | ||
| 73 | |||
| 74 | 330 | arch_spin_unlock(&cpu); | |
| 75 | 330 | arch_irq_unlock(key); | |
| 76 | |||
| 77 | #if P_CPU_NR > 1 // todo | ||
| 78 |
2/2✓ Branch 0 taken 125 times.
✓ Branch 1 taken 205 times.
|
330 | if(need_send != CPU_NA) |
| 79 | { | ||
| 80 | void arch_ipi_send(uint8_t cpuid); | ||
| 81 | 125 | arch_ipi_send(need_send); | |
| 82 | } | ||
| 83 | #endif | ||
| 84 | |||
| 85 | 330 | return 0; | |
| 86 | } | ||
| 87 | |||
| 88 | 454 | struct _thread_obj *p_sched_ready_highest(void) | |
| 89 | { | ||
| 90 | 454 | struct _thread_obj *highest_thread = NULL; | |
| 91 | 454 | p_base_t key = arch_irq_lock(); | |
| 92 | 454 | arch_spin_lock(&cpu); | |
| 93 | |||
| 94 | 454 | p_list_t *_ready_queue = &p_cpu_self()->ready_queue; | |
| 95 | |||
| 96 |
2/2✓ Branch 1 taken 368 times.
✓ Branch 2 taken 86 times.
|
454 | if (!p_list_is_empty(_ready_queue)) |
| 97 | { | ||
| 98 | 368 | highest_thread = p_list_entry(_ready_queue->head, struct _thread_obj, tnode); | |
| 99 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 368 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
|
368 | KLOG_ASSERT(highest_thread != NULL); |
| 100 |
1/6✗ Branch 1 not taken.
✓ Branch 2 taken 368 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
|
368 | KLOG_ASSERT(p_obj_get_type(highest_thread) == P_OBJ_TYPE_THREAD); |
| 101 | } | ||
| 102 | |||
| 103 | 454 | arch_spin_unlock(&cpu); | |
| 104 | 454 | arch_irq_unlock(key); | |
| 105 | 454 | return highest_thread; | |
| 106 | } | ||
| 107 | |||
| 108 | 329 | int p_sched_ready_remove(p_obj_t thread) | |
| 109 | { | ||
| 110 | 329 | struct _thread_obj *_thread = thread; | |
| 111 | 329 | p_base_t key = arch_irq_lock(); | |
| 112 | |||
| 113 |
1/6✗ Branch 1 not taken.
✓ Branch 2 taken 329 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
|
329 | KLOG_ASSERT(p_obj_get_type(_thread) == P_OBJ_TYPE_THREAD); |
| 114 | |||
| 115 | KLOG_D("p_sched_ready_remove:tnode:%x",&_thread->tnode); | ||
| 116 | 329 | p_list_remove(&_thread->tnode); | |
| 117 | |||
| 118 | 329 | arch_irq_unlock(key); | |
| 119 | 329 | return 0; | |
| 120 | } | ||
| 121 |