| 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" | ||
| 11 | #define KLOG_LVL KLOG_WARNING | ||
| 12 | #include <puppy/klog.h> | ||
| 13 | |||
| 14 | struct _thread_obj *p_sched_ready_highest(void); | ||
| 15 | |||
| 16 | 472 | int p_sched(void) | |
| 17 | { | ||
| 18 | 472 | int ret = P_EOK; | |
| 19 | struct _thread_obj *_h_thread; | ||
| 20 | 472 | p_base_t key = arch_irq_lock(); | |
| 21 | 472 | struct p_cpu *_cpu = p_cpu_self(); | |
| 22 | |||
| 23 | 
        2/2✓ Branch 0 taken 467 times. 
          ✓ Branch 1 taken 5 times. 
         | 
      472 | if (_cpu->sched_lock == 0) | 
| 24 | { | ||
| 25 | 
        2/2✓ Branch 0 taken 454 times. 
          ✓ Branch 1 taken 13 times. 
         | 
      467 | if (!_cpu->next_thread) | 
| 26 | { | ||
| 27 | /* get prio higest thread */ | ||
| 28 | 454 | _h_thread = p_sched_ready_highest(); | |
| 29 | 
        2/2✓ Branch 0 taken 86 times. 
          ✓ Branch 1 taken 368 times. 
         | 
      454 | if (!_h_thread) | 
| 30 | { | ||
| 31 | 86 | arch_irq_unlock(key); | |
| 32 | 86 | return 0; | |
| 33 | } | ||
| 34 | 
        6/6✓ Branch 0 taken 364 times. 
          ✓ Branch 1 taken 4 times. 
          ✓ Branch 2 taken 187 times. 
          ✓ Branch 3 taken 177 times. 
          ✓ Branch 4 taken 39 times. 
          ✓ Branch 5 taken 148 times. 
         | 
      368 | if (_cpu->curr_thread && _cpu->curr_thread->state == P_THREAD_STATE_RUN && _h_thread->prio >= _cpu->curr_thread->prio) | 
| 35 | { | ||
| 36 | 39 | arch_irq_unlock(key); | |
| 37 | 39 | return 0; | |
| 38 | } | ||
| 39 | 329 | _cpu->next_thread = _h_thread; | |
| 40 | 329 | p_sched_ready_remove(_h_thread); | |
| 41 | KLOG_D("next thread:%s", _cpu->next_thread->kobj.name); | ||
| 42 | 
        4/4✓ Branch 0 taken 324 times. 
          ✓ Branch 1 taken 5 times. 
          ✓ Branch 2 taken 148 times. 
          ✓ Branch 3 taken 176 times. 
         | 
      329 | if (_cpu->curr_thread && _cpu->curr_thread->state == P_THREAD_STATE_RUN) | 
| 43 | { | ||
| 44 | 148 | p_sched_ready_insert(_cpu->curr_thread); | |
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | 342 | arch_swap(key); | |
| 49 | 315 | ret = p_get_errno(); | |
| 50 | } | ||
| 51 | |||
| 52 | 320 | arch_irq_unlock(key); | |
| 53 | 320 | return -ret; | |
| 54 | } | ||
| 55 | |||
| 56 | ✗ | void p_sched_lock(void) | |
| 57 | { | ||
| 58 | ✗ | atomic_fetch_add(&p_cpu_self()->sched_lock, 1); | |
| 59 | ✗ | } | |
| 60 | 4 | void p_sched_unlock(void) | |
| 61 | { | ||
| 62 | 4 | atomic_fetch_sub(&p_cpu_self()->sched_lock, 1); | |
| 63 | 
        1/2✓ Branch 1 taken 4 times. 
          ✗ Branch 2 not taken. 
         | 
      4 | if (!p_cpu_self()->sched_lock) | 
| 64 | { | ||
| 65 | 4 | p_sched(); | |
| 66 | } | ||
| 67 | ✗ | } | |
| 68 | |||
| 69 | 325 | void p_sched_swap_out_cb(p_obj_t thread) | |
| 70 | { | ||
| 71 | P_UNUSED(thread); | ||
| 72 | 
        1/6✗ Branch 2 not taken. 
          ✓ Branch 3 taken 325 times. 
          ✗ Branch 13 not taken. 
          ✗ Branch 14 not taken. 
          ✗ Branch 16 not taken. 
          ✗ Branch 17 not taken. 
         | 
      325 | KLOG_ASSERT(p_obj_get_type(p_cpu_self()->curr_thread) == P_OBJ_TYPE_THREAD); | 
| 73 | 325 | p_cpu_self()->curr_thread = NULL; | |
| 74 | 325 | } | |
| 75 | |||
| 76 | 328 | void p_sched_swap_in_cb(p_obj_t thread) | |
| 77 | { | ||
| 78 | 328 | struct p_cpu *cpu = p_cpu_self(); | |
| 79 | P_UNUSED(thread); | ||
| 80 | 
        1/6✗ Branch 2 not taken. 
          ✓ Branch 3 taken 328 times. 
          ✗ Branch 13 not taken. 
          ✗ Branch 14 not taken. 
          ✗ Branch 16 not taken. 
          ✗ Branch 17 not taken. 
         | 
      328 | KLOG_ASSERT(p_obj_get_type(p_cpu_self()->next_thread) == P_OBJ_TYPE_THREAD); | 
| 81 | 328 | cpu->next_thread->state = P_THREAD_STATE_RUN; | |
| 82 | 328 | cpu->curr_thread = cpu->next_thread; | |
| 83 | 328 | cpu->next_thread = NULL; | |
| 84 | 328 | cpu->curr_thread->oncpu = p_cpu_self_id(); | |
| 85 | 329 | } | |
| 86 | |||
| 87 | 60 | int sched_getcpu(void) | |
| 88 | { | ||
| 89 | 60 | return p_cpu_self_id(); | |
| 90 | } | ||
| 91 |