Max OS 0.3
Loading...
Searching...
No Matches
scheduler.h
Go to the documentation of this file.
1
9#ifndef MAXOS_PROCESSES_SCHEDULER_H
10#define MAXOS_PROCESSES_SCHEDULER_H
11
12#include <common/vector.h>
13#include <system/cpu.h>
15#include <processes/process.h>
17#include <processes/ipc.h>
18
19
20namespace MaxOS::processes {
21
27
28 private:
29 inline static GlobalScheduler* s_instance = nullptr;
30 bool m_active = false;
31
32 GlobalResourceRegistry m_global_resource_registry = { };
33 ResourceRegistry<SharedMemory> m_shared_memory_registry;
34 ResourceRegistry<SharedMessageEndpoint> m_shared_messages_registry;
35
36 common::Spinlock m_lock;
37
38 uint64_t m_next_pid;
39 uint64_t m_next_tid;
40
43
44 public:
45 explicit GlobalScheduler(system::Multiboot& multiboot);
47
49 static Scheduler* core_scheduler();
50
53
54 static void activate();
55 static void deactivate();
56 static bool is_active();
57
58 void balance();
59
60 static void load_multiboot_elfs(system::Multiboot* multiboot);
61 static void print_running_header();
62
63 uint64_t add_process(Process* process);
64 uint64_t add_thread(Thread* thread);
65
66 static uint64_t remove_process(Process* process);
68
69 static Process* current_process();
70 static Process* get_process(uint64_t pid);
71 static uint64_t next_pid();
72
73 static Thread* current_thread();
74 static Thread* get_thread(uint64_t tid);
75 static uint64_t next_tid();
76 };
77
79 constexpr size_t TICKS_PER_EVENT = 20;
80
85 class Scheduler {
86
87 private:
88 common::Vector<Process*> m_processes;
90
91 uint64_t m_next_thread_index;
92 bool m_active;
93
94 uint64_t m_ticks;
95
96 static system::cpu_status_t* load_process(Process* process, Thread* thread);
97
98 public:
99 Scheduler();
100 ~Scheduler();
101
105
106 uint64_t add_process(Process* process);
107 uint64_t remove_process(Process* process);
109 uint64_t add_thread(Thread* thread);
110
112 Process* get_process(uint64_t pid);
113 uint64_t process_amount();
114
116 Thread* get_thread(uint64_t tid);
117 uint64_t thread_amount();
118
119 [[nodiscard]] uint64_t ticks() const;
120
121 void activate();
122 void deactivate();
123 };
124
125}
126
127
128#endif // MAXOS_PROCESSES_SCHEDULER_H
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
Enables a resource to be used by only one instance at a time through locking and unlocking.
Definition spinlock.h:21
Handles a certain interrupt number.
Definition interrupts.h:30
virtual void handle_interrupt()
Handles an interrupt.
Manages all the resource registries for each resource type.
Definition resource.h:121
The global scheduler that manages all processes and threads across all cores.
Definition scheduler.h:26
static uint64_t next_pid()
Gets the next usable global PID.
static Scheduler * core_scheduler()
Gets the scheduler for the currently executing core.
static Process * get_process(uint64_t pid)
Gets a process on the currently executing core by its PID.
static uint64_t remove_process(Process *process)
Removes a process from the scheduler if the process has no threads, if it does then the threads are s...
static system::cpu_status_t * yield(system::cpu_status_t *current)
Pass execution to the next thread.
Definition scheduler.cpp:79
static void print_running_header()
Print the header for the running processes in the form ({name}:t{tid}c{core})
static uint64_t next_tid()
Gets the next usable global TID.
~GlobalScheduler()
Destroys the global scheduler and frees the per core schedulers.
Definition scheduler.cpp:87
static GlobalScheduler * system_scheduler()
Gets the system scheduler.
Definition scheduler.cpp:50
static Process * current_process()
Gets the process on the currently executing core.
static Thread * get_thread(uint64_t tid)
Gets a thread on the currently executing core by its TID.
static void activate()
Activates each core's scheduler.
static void deactivate()
Deactivates each core's scheduler.
static bool is_active()
Checks if the global scheduler is active.
void balance()
Moves processes and threads between cores to balance the load so that each core has a similar amount ...
static Thread * current_thread()
Gets the thread on the currently executing core.
uint64_t add_process(Process *process)
Adds a process to the least busy core.
uint64_t add_thread(Thread *thread)
Adds a thread to the least busy core.
static void load_multiboot_elfs(system::Multiboot *multiboot)
Loads any valid ELF files from the multiboot structure.
static system::cpu_status_t * force_remove_process(Process *process)
Removes a process from the scheduler and deletes all threads, begins running the next process.
A process that can be scheduled by the Scheduler, wraps & manages threads as well as its own address ...
Definition process.h:85
A resource registry for a specific resource type.
Definition resource.h:81
Schedules processes to run on the core via their threads.
Definition scheduler.h:85
uint64_t remove_process(Process *process)
Removes a process from the scheduler if the process has no threads, if it does then the threads are s...
uint64_t ticks() const
Gets how long the system has been running for.
Scheduler()
Constructs a new Scheduler object and creates the idle process.
uint64_t thread_amount()
Gets how many threads are running on this scheduler.
Thread * current_thread()
Gets the currently executing thread.
system::cpu_status_t * yield()
Pass execution to the next thread.
system::cpu_status_t * force_remove_process(Process *process)
Removes a process from the scheduler and deletes all threads, begins running the next process.
Process * get_process(uint64_t pid)
Gets a process by its PID.
uint64_t add_process(Process *process)
Adds a process to the scheduler.
Thread * get_thread(uint64_t tid)
Gets a thread by its TID.
uint64_t add_thread(Thread *thread)
Adds a thread to the scheduler.
system::cpu_status_t * schedule(system::cpu_status_t *cpu_state)
Schedules the next thread to run.
uint64_t process_amount()
Gets how processes are running on this scheduler.
void activate()
Activates the scheduler.
Process * current_process()
Gets the current process.
system::cpu_status_t * schedule_next(system::cpu_status_t *status)
Schedules the next thread to run.
void deactivate()
Deactivates the scheduler.
The execution context of a sub-process thread.
Definition process.h:49
Parses and provides access to Multiboot 2 information.
Definition multiboot.h:518
Defines Central Processing Unit (CPU) structures and functions for managing CPU state and features.
struct PACKED MaxOS::system::CPUStatus cpu_status_t
Alias for CPUStatus struct.
Defines a InterruptManager and InterruptHandler for managing hardware and software interrupts.
Defines inter-process communication (IPC) methods such as shared memory and message endpoints.
Defines a MemoryManager class for handling memory allocation and deallocation.
Defines Process and Thread classes for managing processes and their execution contexts.
constexpr size_t TICKS_PER_EVENT
Number of times the clock has to interrupt before switching to the next process/thread.
Definition scheduler.h:79
Defines a Vector class for dynamically storing an array of elements.