X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/b60166b327a9108b07e3069fa6568a451529ffd9..084eac3d5fa7deeab2296cf20653f4f7b3f75cd0:/lunaix-os/includes/lunaix/kpreempt.h diff --git a/lunaix-os/includes/lunaix/kpreempt.h b/lunaix-os/includes/lunaix/kpreempt.h index d20deba..6fa9d70 100644 --- a/lunaix-os/includes/lunaix/kpreempt.h +++ b/lunaix-os/includes/lunaix/kpreempt.h @@ -1,18 +1,62 @@ #ifndef __LUNAIX_KPREEMPT_H #define __LUNAIX_KPREEMPT_H -#include - -#define _preemptible __attribute__((section(".kf.preempt"))) - -#define ensure_preempt_caller() \ - do { \ - extern int __kf_preempt_start[]; \ - extern int __kf_preempt_end[]; \ - ptr_t _retaddr = abi_get_retaddr(); \ - assert_msg((ptr_t)__kf_preempt_start <= _retaddr \ - && _retaddr < (ptr_t)__kf_preempt_end, \ - "caller must be kernel preemptible"); \ - } while(0) +#include +#include +#include + +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 */