Files
2026-05-19 11:49:22 +08:00

138 lines
4.1 KiB
C

#ifndef INCLUDED_OS_WAITOBJECT_H
#define INCLUDED_OS_WAITOBJECT_H
#ifndef INCLUDED_OS_TYPES_H
#include <os_types.h>
#endif /*INCLUDED_OS_TYPES_H*/
#ifndef INCLUDED_OS_LIST_H
#include <os_list.h>
#endif /*INCLUDED_OS_LIST_H*/
#ifndef INCLUDED_OS_TIMEWHEEL_H
#include <os_timewheel.h>
#endif /*INCLUDED_OS_TIMEWHEEL_H*/
#ifndef INCLUDED_OS_TASK_H
#include <os_task.h>
#endif /*INCLUDED_OS_TASK_H*/
#ifndef INCLUDED_OS_CRITICAL_H
#include <os_critical.h>
#endif /*INCLUDED_OS_CRITICAL_H*/
#ifndef INCLUDED_OS_RDY_LIST_H
#include <os_rdy_list.h>
#endif /*INCLUDED_OS_RDY_LIST_H*/
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
typedef struct {
os_list_t wait_list;
}os_waitobject_t;
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
OS_STATIC_FORCE_INLINE
void os_waitobject_init(os_waitobject_t* self){
os_list_init(&self->wait_list);
}
OS_STATIC_FORCE_INLINE
void os_waitobject_wait_infinity(os_waitobject_t* self, os_task_t* task){
os_list_remove(&task->node);
os_list_insert_before(&self->wait_list, &task->node);
task->state = kOsTaskState_Wait;
task->errors = OS_ERR_OK;
}
void os_waitobject_on_timeout(os_timer_t* timer);
os_err_t os_waitobject_timed_wait(os_waitobject_t* self, os_task_t* task, os_tick_t ticks);
os_err_t os_waitobject_wait_until(os_waitobject_t* self, os_task_t* task, os_tick_t ticks);
OS_STATIC_FORCE_INLINE
void os_waitobject_wait(os_waitobject_t* self, os_task_t* task){
os_waitobject_timed_wait(self, task, OS_WAIT_INFINITY);
}
OS_STATIC_FORCE_INLINE
void os_waitobject_notify_one(os_waitobject_t* self){
os_critical_enter();
if(os_list_is_empty(&self->wait_list)){
os_critical_leave();
return;
}
os_bool_t is_need_schedule_flag = OS_FALSE;
os_list_node_t* p = self->wait_list.next;
os_list_remove(p);
os_task_t* p_thread = os_list_member_of(p, os_task_t, node);
os_rdy_list_insert(p_thread);
p_thread->remain_ticks = p_thread->init_ticks;
// 尝试抢占当前任务
os_task_t* p_curr_thread_p = os_task_self();
if(p_curr_thread_p && p_curr_thread_p->state==kOsTaskState_Running){
if(os_priority_is_high(p_thread->curr_priority, p_curr_thread_p->curr_priority)){
os_priority_set(p_curr_thread_p->curr_priority);
os_rdy_list_insert_head(p_curr_thread_p); // 放到头部,这样下次调度可以尽快调度这个任务
p_curr_thread_p->state = kOsTaskState_Ready; // 就绪状态
// 不要改变时间窗
is_need_schedule_flag = OS_TRUE;
}
}
os_critical_leave();
if(is_need_schedule_flag) {
os_sched();
}
}
OS_STATIC_FORCE_INLINE
void os_waitobject_notify_all(os_waitobject_t * self){
os_critical_enter();
os_bool_t is_need_schedule_flag = OS_FALSE;
os_list_node_t* p = self->wait_list.next;
os_task_t* p_curr_thread_p = os_task_self();
for(;p!=&self->wait_list;){
os_task_t* p_thread = os_list_member_of(p, os_task_t, node);
p = p->next;
os_list_remove(&p_thread->node);
// 将任务加入就绪表
os_rdy_list_insert(p_thread);
// p_thread->state = OS_TASK_STATE_READY;
p_thread->remain_ticks = p_thread->init_ticks;
// 尝试抢占当前任务
if(p_curr_thread_p && p_curr_thread_p->state==kOsTaskState_Running){
if(os_priority_is_high(p_thread->curr_priority, p_curr_thread_p->curr_priority)){
os_priority_set(p_curr_thread_p->curr_priority);
os_rdy_list_insert_head(p_curr_thread_p); // 放到头部,这样下次调度可以尽快调度这个任务
p_curr_thread_p->state = kOsTaskState_Ready; // 就绪状态
is_need_schedule_flag = OS_TRUE;
}
}
}
os_critical_leave();
if(is_need_schedule_flag){
os_sched();
}
}
void os_waitobject_notify_one_in_isr(os_waitobject_t* self);
void os_waitobject_notify_all_in_isr(os_waitobject_t * self);
#endif /*INCLUDED_OS_WAITOBJECT_H*/