My Kernel v0.1.0
sched.h
1#pragma once
2
56#include <kernel/process.h>
57
58#include <utils/compiler.h>
59
60// TODO: Remove this once we implemented another timer for the scheduler
61extern bool scheduler_initialized;
62
70void schedule(void);
71
73void schedule_preempt(void);
74
79
83void scheduler_preempt_enable(bool old_if_flag);
84
89
94static ALWAYS_INLINE void
95sched_new_thread_create(thread_entry_t entrypoint, void *data, u32 flags)
96{
97 struct thread *thread = thread_spawn(current->process, entrypoint, data,
98 NULL, flags);
99 if (!IS_ERR(thread))
101}
102
108void sched_block_thread(struct thread *);
109
116
118void sched_block_waiting_until(struct thread *, clock_t until);
119
121void sched_unblock_waiting_before(clock_t deadline);
122
123typedef struct {
124 const bool old_if;
125 bool done;
126} sched_scope_t;
127
128static inline sched_scope_t sched_scope_constructor(void)
129{
130 return (sched_scope_t){
131 .old_if = scheduler_preempt_disable(),
132 .done = false,
133 };
134}
135
136static inline void sched_scope_destructor(sched_scope_t *scope)
137{
138 scheduler_preempt_enable(scope->old_if);
139}
140
147#define no_preemption_scope() \
148 for (sched_scope_t scope CLEANUP(sched_scope_destructor) = \
149 sched_scope_constructor(); \
150 !scope.done; scope.done = true)
#define IS_ERR(_x)
Check if an integer can be interpreted as an error.
Definition: error.h:83
thread_t * thread_spawn(struct process *, thread_entry_t, void *, void *, u32)
Create and initialize a new thread.
Definition: process.c:339
void(* thread_entry_t)(void *data)
A function used as an entry point when creating a new thread.
Definition: process.h:64
thread_t * current
The currently running thread.
Definition: process.c:41
void sched_unblock_thread(thread_t *)
Unblock a currently blocked thread.
Definition: sched.c:150
void sched_block_waiting_until(struct thread *, clock_t until)
Block thread and wait until a given deadline.
Definition: sched.c:175
void sched_new_thread(thread_t *)
Add a new thread to be scheduled.
Definition: sched.c:126
void sched_unblock_waiting_before(clock_t deadline)
Unblock all waiting threads whose deadline is inferior to deadline.
Definition: sched.c:182
void scheduler_preempt_enable(bool old_if_flag)
Re-allow the current thread to be pre-empted.
Definition: sched.c:110
void schedule_preempt(void)
Forcibly reschedule the current thread.
Definition: sched.c:96
void sched_block_thread(struct thread *)
Mark the thread as blocked.
Definition: sched.c:135
static ALWAYS_INLINE void sched_new_thread_create(thread_entry_t entrypoint, void *data, u32 flags)
Create a new thread and instantly schedule it.
Definition: sched.h:95
void schedule(void)
Reschedule the current thread.
Definition: sched.c:89
bool scheduler_preempt_disable(void)
Prevent the current thread from being pree-empted by the scheduler.
Definition: sched.c:103
Process management.
A single thread.
Definition: process.h:121
struct process * process
Definition: process.h:132
u32 flags
Definition: process.h:139