User:Anotheridiot/Threading

From OSDev Wiki
Jump to navigation Jump to search

This serves as my writing place for my thoughts on how an OS should implement threading (though it is opinionated to my own preferences and designs obviously).

How my threading design works

A process is spawned with one thread, and a 16KiB stack, the stack size can be increased or decreased by the process as wanted, though most things should be put onto the heap. Regardless a thread can be spawned by tspawn, and exits by texit. tspawn spawns a thread taking in the pointer for its entry code and allocates a 16KiB stack, texit exits the currently running thread (cannot be thread 0, that must call exit). All threads in a process share the PID and have a different TID (thread ID). They also share a CR3 for speed. A process is ran, then it runs once, then the next task runs, and it will loop over and over again going from TID 0 to TID 1 and so on in each task. For example:

- Process 1 runs with Thread 0

- Process 2 runs with Thead 0

- Process 1 runs with Thread 1

- Process 2 runs with Thread 2

- ...

- Process 1 runs its final thread in the list

- Process 2 runs its final thread in the list

- Process 1 runs thread 0.

This is not a perfectly optimized setup though it does stop an application from creating a large amount of threads and just taking over execution.