--- title: "scheduler.h" summary: "the scheduler is responsible for switching between threads and controlling processes" --- #### `scheduler_processes_chunk_T` - struct These chunks are a combination of static array and linked list. They store the [process_T](https://nerdcult.net/projects/noxos/docs/codebase/proc/process.h/#process_t---struct) pointer for each valid `pid_t`. [scheduler_processes_chunk_T](https://nerdcult.net/projects/noxos/docs/codebase/proc/scheduler.h/#scheduler_processes_chunk_t---struct) | Name | Type | Description | |------------------|------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------| | processes | [process_T](https://nerdcult.net/projects/noxos/docs/codebase/proc/process.h/#process_t---struct)** | The array of process pointers | | processes_bitmap | bitmap_T(reference needed) | If a bit in this bitmap is set, the _processes_ entry with the same index is valid | | num_free_pids | uint32_t | The amount of free slots in this chunk | | prev | [scheduler_processes_chunk_T](https://nerdcult.net/projects/noxos/docs/codebase/proc/scheduler.h/#scheduler_processes_chunk_t---struct)* | The previous chunk | | next | [scheduler_processes_chunk_T](https://nerdcult.net/projects/noxos/docs/codebase/proc/scheduler.h/#scheduler_processes_chunk_t---struct)* | The next chunk | #### `scheduler_T` - struct | Name | Type | Description | |----------------|------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------| | num_threads | uint32_t | Total amount of currently spawned threads | | num_processes | uint32_t | Total amount of currently spawned processes | | running_thread | [thread_T](https://nerdcult.net/projects/noxos/docs/codebase/proc/thread.h/#thread_t---struct)* | A pointer to the currently running thread. | | processes | [scheduler_processes_chunk_T](https://nerdcult.net/projects/noxos/docs/codebase/proc/scheduler.h/#scheduler_processes_chunk_t---struct)* | The first processes store chunk | | blocked | bool | Set to true, while switching the context. Thread safety mechanism. | | initialized | bool | Set to true, if the scheduler is initialized and started. | #### `scheduler_init(boot_info)` - function (void) Initializes the scheduler and performs a `nx_scheduler_start` kernel syscall. **_boot_info_** is needed in to spawn the kernels' main process. After this function, the whole kernel is in scheduling mode. #### `scheduler_start(state)` - function (cpu_state_T*) Creates and starts a thread from **_state_**. It returns the result of a context switch, I forgot, why I did it like that. This is basically the backend for the `nx_scheduler_start` kernel syscall. #### `scheduler_is_initialized()` - function (bool) Returns if the scheduler is initialized (and running) or not. #### `scheduler_dump_info(process, indent)` - function (void) This recursively lists information(pid, name, threads) for all child processes of **_process_**. **_indent_** is used intern for the recursive calls and should be set to 0 initially. #### `scheduler_register_thread(thread)` - function (thread_T*) Registers **_thread_** in the scheduler. #### `scheduler_pause_thread(thread)` - function (void) Pauses **_thread_**, by removing it from the scheduling queue. **Potential Bug:** if **_thread_** was the currently running thread, this could cause issues, because it's _prev_ and _next_ values are nulled. #### `scheduler_start_thread(thread)` - function (void) Starts **_thread_**, by linking it into the scheduling queue. #### `scheduler_kill_thread(thread)` - function (void) Pauses and unregisters **_thread_**. #### `scheduler_register_process(process)` - function (pid_t) Reqisters **_process_** and returns its pid. #### `scheduler_kill_process(process)` - function (void) Kills **_process_** and its threads and childs. #### `scheduler_get_process(pid)` - function (process_T*) Returns the `process_T` pointer that is associated with **_pid_**. #### `scheduler_get_current_thread()` - function (thread_T*) Returns a pointer to the currently running thread. #### `scheduler_get_current_process()` - function (process_T*) Returns a pointer to the currently running threads process. #### `scheduler_switch_context(state)` - function (cpu_state_T*) Saves **_state_** in the running threads _state_ value and increments their _cpu_time_ value. Then it sets the next thread as the running thread and returns its _state_. This needs to be called from an interrupt handler, for the returned state to be loaded.