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 <libcommon/vector.h>
13
14#include <system/cpu.h>
16#include <processes/process.h>
18#include <processes/ipc.h>
20
21
22namespace MaxOS::processes {
23
29
30 private:
31 inline static GlobalScheduler* s_instance = nullptr;
32 bool m_active = false;
33
34 resources::GlobalResourceRegistry m_global_resource_registry = { };
35 resources::ResourceRegistry<SharedMemory> m_shared_memory_registry;
37 resources::ThreadResourceRegistry m_thread_resource_registry;
38 resources::ProcessResourceRegistry m_process_resource_registry;
39
40 common::Spinlock m_lock;
41
42 uint64_t m_next_pid;
43 uint64_t m_next_tid;
44
45 common::Map<uint64_t, uint64_t> m_core_pids;
46 common::Map<uint64_t, uint64_t> m_core_tids;
47
48 public:
49 explicit GlobalScheduler(system::Multiboot& multiboot);
51
53 static Scheduler* core_scheduler();
54
56
57 static void activate();
58 static void deactivate();
59 static bool is_active();
60
61 void balance();
62 system::Core* least_busy_core(bool check_threads=false);
63
64 static void load_multiboot_elfs(system::Multiboot* multiboot);
65 static void prepare_initrd(multiboot_tag_module* module);
66 static void print_running_header();
67
68 uint64_t add_process(Process* process, Scheduler* scheduler = nullptr);
69 uint64_t add_thread(Thread* thread, Scheduler* scheduler = nullptr);
70
71 static uint64_t remove_process(Process* process);
73
74 static Process* current_process();
75 static Process* get_process(uint64_t pid);
76 static uint64_t next_pid();
77
78 static Thread* current_thread();
79 static Thread* get_thread(uint64_t tid);
80 static uint64_t next_tid();
81 };
82
84 constexpr size_t TICKS_PER_EVENT = 20;
85
90 class Scheduler {
91
92 private:
93 common::Vector<Process*> m_processes;
94 common::Vector<Thread*> m_threads;
95
96 uint64_t m_next_thread_index;
97 bool m_active;
98
99 uint64_t m_ticks;
100 uintptr_t m_core_id;
101
102 static system::cpu_status_t* load_process(Process* process, Thread* thread);
103
104 public:
105 Scheduler(uint64_t core_id);
106 ~Scheduler();
107
110
111 uint64_t add_process(Process* process);
112 uint64_t remove_process(Process* process);
114 uint64_t add_thread(Thread* thread);
115
117 Process* get_process(uint64_t pid);
118 uint64_t process_amount();
119
121 Thread* get_thread(uint64_t tid);
122 uint64_t thread_amount();
123
124 [[nodiscard]] uint64_t ticks() const;
125 [[nodiscard]] uint64_t core_id() const;
126
127 void activate();
128 void deactivate();
129 };
130
131}
132
133
134#endif // MAXOS_PROCESSES_SCHEDULER_H
Handles a certain interrupt number.
Definition interrupts.h:44
virtual void handle_interrupt()
Handles an interrupt.
The global scheduler that manages all processes and threads across all cores.
Definition scheduler.h:28
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...
uint64_t add_process(Process *process, Scheduler *scheduler=nullptr)
Adds a process to the least busy core.
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.
uint64_t add_thread(Thread *thread, Scheduler *scheduler=nullptr)
Adds a thread to the least busy core.
static void prepare_initrd(multiboot_tag_module *module)
Loads the initrd into the init program at 0xFFF000.
~GlobalScheduler()
Destroys the global scheduler and frees the per core schedulers.
Definition scheduler.cpp:81
system::Core * least_busy_core(bool check_threads=false)
Finds the core with the least amount of threads/processes.
static GlobalScheduler * system_scheduler()
Gets the system scheduler.
Definition scheduler.cpp:55
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.
Definition scheduler.cpp:95
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.
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:94
Schedules processes to run on the core via their threads.
Definition scheduler.h:90
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.
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 * 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.
uint64_t core_id() const
Gets the core that this scheduler handles processes for.
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:50
Manages all the resource registries for each resource type.
Definition resource.h:101
Manages the creation, retention and destruction of resources representing processes.
A resource registry for a specific resource type.
Definition resource.h:61
Manages the creation, retention and destruction of resources representing threads.
Represents a CPU core in the system.
Definition cpu.h:216
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:84
Defines scheduler resource classes for wrapping Processe and Thread objects as Resources in the Globa...
A tag containing information about a loaded module.
Definition multiboot.h:253