This repository has been archived on 2023-09-28. You can view files and clone it, but cannot push or open issues or pull requests.
homepage/content/projects/noxos/docs/codebase/proc/thread.h.md

45 lines
4.0 KiB
Markdown

---
title: "thread.h"
summary: "threading infrastructure"
---
# `thread_T` - struct
| Name | Type | Description |
|-------------|--------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------|
| state | [cpu_state_T](https://nerdcult.net/projects/noxos/docs/codebase/platform/cpu.h/#cpu_state_t---struct)* | The last saved state of the thread ( -> _context switching_(reference needed)) |
| cpu_time | uint64_t | The amount of cpu time the thread had. (currently the amount of context switches the thread had) |
| stack | void* | The bottom of the threads stack |
| stack_size | uint32_t | The size of the threads stack (in bytes) |
| process | [process_T](https://nerdcult.net/projects/noxos/docs/codebase/proc/process.h/#process_t---struct)* | The process, to which the thread belongs to |
| global_prev | [thread_T](https://nerdcult.net/projects/noxos/docs/codebase/proc/thread.h/#thread_t---struct)* | The previous thread in the scheduling queue (**should only be accessed by the scheduler!**) |
| global_next | [thread_T](https://nerdcult.net/projects/noxos/docs/codebase/proc/thread.h/#thread_t---struct)* | The next thread in the scheduling queue (**should only be accessed by the scheduler!**) |
| local_prev | [thread_T](https://nerdcult.net/projects/noxos/docs/codebase/proc/thread.h/#thread_t---struct)* | The previous thread of _process_ (**should only be accessed by the scheduler!**) |
| local_next | [thread_T](https://nerdcult.net/projects/noxos/docs/codebase/proc/thread.h/#thread_t---struct)* | The next thread of _process_ (**should only be accessed by the scheduler!**) |
| local_id | uint32_t | The threads id in its process |
# `thread_spawn(function)` - function (thread_T*)
Allocates a [thread_T](https://nerdcult.net/projects/noxos/docs/codebase/proc/thread.h/#thread_t---struct) and registers it in the scheduler.
The thread starts execution at **_function_**.
The for the thread allocated stack has a size of 16 KB (4 Pages).
The thread still needs to be started with [thread_start](https://nerdcult.net/projects/noxos/docs/codebase/proc/thread.h/#thread_startthread---function-void).
Returns a pointer to the created thread.
# `thread_spawn_from_state(state)` - function (thread_T*)
Allocates a [thread_T](https://nerdcult.net/projects/noxos/docs/codebase/proc/thread.h/#thread_t---struct) and registers it in the scheduler.
The threads' _cpu_state_ is copied from **_state_**.
This won't allocate a stack for the stack.
The thread still needs to be started with [thread_start](https://nerdcult.net/projects/noxos/docs/codebase/proc/thread.h/#thread_startthread---function-void).
Returns a pointer to the created thread.
This function should be avoided.
# `thread_start(thread)` - function (void)
Starts/unpauses **_thread_**.
# `thread_pause(thread)` - function (void)
Pauses **_thread_**.
# `thread_kill(thread)` - function (void)
Kills **_thread_**.
The threads stack and [thread_T](https://nerdcult.net/projects/noxos/docs/codebase/proc/thread.h/#thread_t---struct) structure will be freed.