X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/ebb55b7e5f0b8f31328950ec383b77b208ffbb64..a136ca38d83fae60994a54f5da88120e545895e1:/lunaix-os/includes/lunaix/kpreempt.h diff --git a/lunaix-os/includes/lunaix/kpreempt.h b/lunaix-os/includes/lunaix/kpreempt.h index e622e05..400aaff 100644 --- a/lunaix-os/includes/lunaix/kpreempt.h +++ b/lunaix-os/includes/lunaix/kpreempt.h @@ -2,6 +2,8 @@ #define __LUNAIX_KPREEMPT_H #include +#include +#include #define _preemptible \ __attribute__((section(".kf.preempt"))) no_inline @@ -16,4 +18,58 @@ "caller must be kernel preemptible"); \ } while(0) +static inline void +set_preemption() +{ + cpu_enable_interrupt(); +} + +static inline void +no_preemption() +{ + cpu_disable_interrupt(); +} + +static inline void +__schedule_away() +{ + current_thread->stats.last_reentry = clock_systime(); + + cpu_trap_sched(); + set_preemption(); +} + +/** + * @brief preempt the current thread, and yield the remaining + * time slice to other threads. + * + * The current thread is marked as if it is being + * preempted involuntarily by kernel. + * + */ +static inline void +preempt_current() +{ + no_preemption(); + thread_stats_update_kpreempt(); + __schedule_away(); +} + +/** + * @brief yield the remaining time slice to other threads. + * + * The current thread is marked as if it is being + * preempted voluntarily by itself. + * + */ +static inline void +yield_current() +{ + no_preemption(); + __schedule_away(); +} + +bool +preempt_check_stalled(struct thread* th); + #endif /* __LUNAIX_KPREEMPT_H */