| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2022-2023, The Puppy RTOS Authors | ||
| 3 | * | ||
| 4 | * SPDX-License-Identifier: Apache-2.0 | ||
| 5 | */ | ||
| 6 | #include <puppy.h> | ||
| 7 | #include <puppy/kobj.h> | ||
| 8 | |||
| 9 | #define KLOG_TAG "sem" | ||
| 10 | #define KLOG_LVL KLOG_WARNING | ||
| 11 | #include <puppy/klog.h> | ||
| 12 | |||
| 13 | 27 | void p_sem_init(p_obj_t obj, const char *name, | |
| 14 | uint32_t init_value, | ||
| 15 | uint32_t max_value) | ||
| 16 | { | ||
| 17 | 27 | struct _sem_obj *sem = obj; | |
| 18 | |||
| 19 | 27 | p_obj_init(obj, name, P_OBJ_TYPE_IPC | P_OBJ_TYPE_STATIC, P_OBJ_TYPE_IPC_SEM); | |
| 20 | |||
| 21 | 27 | sem->kobj.name = name; | |
| 22 | 27 | sem->value = init_value; | |
| 23 | 27 | sem->max_value = max_value; | |
| 24 | 27 | p_list_init(&sem->blocking_list); | |
| 25 | 27 | } | |
| 26 | |||
| 27 | ✗ | p_obj_t p_sem_create(const char *name, | |
| 28 | uint32_t init_value, | ||
| 29 | uint32_t max_value) | ||
| 30 | { | ||
| 31 | ✗ | return NULL; | |
| 32 | } | ||
| 33 | |||
| 34 | 121 | void _block_thread(p_list_t *list, struct _thread_obj *thread) | |
| 35 | { | ||
| 36 | struct _thread_obj *temp_thread; | ||
| 37 |
1/2✓ Branch 1 taken 121 times.
✗ Branch 2 not taken.
|
121 | if (p_list_is_empty(list)) |
| 38 | { | ||
| 39 | 121 | p_list_append(list, &thread->tnode); | |
| 40 | } | ||
| 41 | else | ||
| 42 | { | ||
| 43 | ✗ | p_node_t *pos = list->head; | |
| 44 | ✗ | while(pos != list) | |
| 45 | { | ||
| 46 | ✗ | temp_thread = p_list_entry(pos, struct _thread_obj, tnode); | |
| 47 | ✗ | KLOG_ASSERT(p_obj_get_type(temp_thread) == P_OBJ_TYPE_THREAD); | |
| 48 | ✗ | if (temp_thread->prio > thread->prio) | |
| 49 | { | ||
| 50 | ✗ | break; | |
| 51 | } | ||
| 52 | ✗ | pos = pos->next; | |
| 53 | } | ||
| 54 | ✗ | if (pos != list) | |
| 55 | { | ||
| 56 | ✗ | p_list_insert(pos, &thread->tnode); | |
| 57 | } | ||
| 58 | else | ||
| 59 | { | ||
| 60 | ✗ | p_list_append(list, &thread->tnode); | |
| 61 | } | ||
| 62 | } | ||
| 63 | KLOG_D("_block_thread:%s", thread->kobj.name); | ||
| 64 | 121 | p_thread_block(thread); | |
| 65 | 121 | } | |
| 66 | |||
| 67 | 121 | void _wakeup_block_thread(p_list_t *list) | |
| 68 | { | ||
| 69 | struct _thread_obj *_thread; | ||
| 70 | 121 | _thread = p_list_entry(list->head, | |
| 71 | struct _thread_obj, tnode); | ||
| 72 | 121 | p_list_remove(&_thread->tnode); | |
| 73 | KLOG_D("_wakeup_block_thread:%s", _thread->kobj.name); | ||
| 74 | 121 | p_sched_ready_insert(_thread); | |
| 75 | 121 | } | |
| 76 | |||
| 77 | 273 | int p_sem_post(p_obj_t obj) | |
| 78 | { | ||
| 79 | 273 | struct _sem_obj *sem = obj; | |
| 80 | 273 | p_base_t key = arch_irq_lock(); | |
| 81 |
1/6✗ Branch 1 not taken.
✓ Branch 2 taken 273 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
|
273 | KLOG_ASSERT(p_obj_get_type(sem) == P_OBJ_TYPE_IPC); |
| 82 |
1/6✗ Branch 1 not taken.
✓ Branch 2 taken 273 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
|
273 | KLOG_ASSERT(p_obj_get_extype(sem) == P_OBJ_TYPE_IPC_SEM); |
| 83 | |||
| 84 |
1/2✓ Branch 0 taken 273 times.
✗ Branch 1 not taken.
|
273 | if (sem->value < sem->max_value) |
| 85 | { | ||
| 86 | 273 | sem->value ++; | |
| 87 | } | ||
| 88 |
2/2✓ Branch 1 taken 152 times.
✓ Branch 2 taken 121 times.
|
273 | if (p_list_is_empty(&sem->blocking_list)) |
| 89 | { | ||
| 90 | 152 | goto _exit; | |
| 91 | } | ||
| 92 | 121 | _wakeup_block_thread(&sem->blocking_list); | |
| 93 | 121 | p_sched(); | |
| 94 | 273 | _exit: | |
| 95 | 273 | arch_irq_unlock(key); | |
| 96 | 273 | return -P_ENOSYS; | |
| 97 | } | ||
| 98 | |||
| 99 | ✗ | int p_sem_timewait(p_obj_t obj, p_tick_t tick) | |
| 100 | { | ||
| 101 | ✗ | return -P_ENOSYS; | |
| 102 | } | ||
| 103 | |||
| 104 | 272 | int p_sem_wait(p_obj_t obj) | |
| 105 | { | ||
| 106 | 272 | struct _sem_obj *sem = obj; | |
| 107 | 272 | int ret = P_EOK; | |
| 108 | 272 | p_base_t key = arch_irq_lock(); | |
| 109 |
2/2✓ Branch 0 taken 151 times.
✓ Branch 1 taken 121 times.
|
272 | if (sem->value > 0) |
| 110 | { | ||
| 111 | 151 | sem->value --; | |
| 112 | 151 | goto _exit; | |
| 113 | } | ||
| 114 | 121 | _block_thread(&sem->blocking_list, p_thread_self()); | |
| 115 | 121 | ret = p_sched(); | |
| 116 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 121 times.
|
121 | if (ret == P_EOK) |
| 117 | { | ||
| 118 | 121 | sem->value --; | |
| 119 | } | ||
| 120 | ✗ | _exit: | |
| 121 | 272 | arch_irq_unlock(key); | |
| 122 | 272 | return ret; | |
| 123 | } | ||
| 124 | |||
| 125 | ✗ | int p_sem_control(p_obj_t obj, int cmd, void *argv) | |
| 126 | { | ||
| 127 | ✗ | return -P_ENOSYS; | |
| 128 | } | ||
| 129 | ✗ | int p_sem_delete(p_obj_t obj) | |
| 130 | { | ||
| 131 | ✗ | return -P_ENOSYS; | |
| 132 | } | ||
| 133 | |||
| 134 |