Boot framework rework (#45)
[lunaix-os.git] / lunaix-os / includes / lunaix / kpreempt.h
1 #ifndef __LUNAIX_KPREEMPT_H
2 #define __LUNAIX_KPREEMPT_H
3
4 #include <sys/abi.h>
5 #include <sys/cpu.h>
6 #include <lunaix/process.h>
7
8 static inline void
9 set_preemption() 
10 {
11     cpu_enable_interrupt();
12 }
13
14 static inline void
15 no_preemption() 
16 {
17     cpu_disable_interrupt();
18 }
19
20 static inline void
21 __schedule_away()
22 {
23     current_thread->stats.last_reentry = clock_systime();
24     
25     cpu_trap_sched();
26     set_preemption();
27 }
28
29 /**
30  * @brief preempt the current thread, and yield the remaining
31  *        time slice to other threads.
32  * 
33  *        The current thread is marked as if it is being
34  *        preempted involuntarily by kernel.
35  * 
36  */
37 static inline void
38 preempt_current()
39 {
40     no_preemption();
41     thread_stats_update_kpreempt();
42     __schedule_away();
43 }
44
45 /**
46  * @brief yield the remaining time slice to other threads.
47  * 
48  *        The current thread is marked as if it is being
49  *        preempted voluntarily by itself.
50  * 
51  */
52 static inline void
53 yield_current()
54 {
55     no_preemption();
56     __schedule_away();
57 }
58
59 bool
60 preempt_check_stalled(struct thread* th);
61
62 #endif /* __LUNAIX_KPREEMPT_H */