wip: thread and processes handle

This commit is contained in:
0x35c
2025-11-05 16:19:21 +01:00
parent 56cfe9f2be
commit 374ea13173
25 changed files with 243 additions and 340 deletions

47
src/multitasking/thread.c Normal file
View File

@ -0,0 +1,47 @@
#include "thread.h"
#include "alloc.h"
#include "assert.h"
#include "interrupts.h"
#include "memory.h"
#include "string.h"
#include "thread.h"
struct tcb *create_thread(struct pcb *process, void (*routine)(void))
{
static uint32_t tid = 1;
struct tcb *new_tcb = vmalloc(sizeof(struct tcb));
if (!new_tcb)
return NULL;
new_tcb->tid = tid++;
new_tcb->esp0 = alloc_pages(STACK_SIZE, NULL);
if (!new_tcb->esp0) {
vfree(new_tcb);
return NULL;
}
// set esp to "skip" the 4 GPRs and eip later to be used as the context
// of the thread
new_tcb->esp = new_tcb->esp0 + STACK_SIZE - 5 * 4;
memcpy(new_tcb->esp + 4, routine, 4);
new_tcb->process = process;
new_tcb->next = NULL;
new_tcb->state = NEW;
struct tcb *it = process->thread_list;
while (it)
it = it->next;
it = new_tcb;
return new_tcb;
}
void delete_thread(struct tcb *thread)
{
vfree(thread->esp0);
struct tcb *it = thread->process->thread_list;
assert(it);
while (it->next != thread)
it = it->next;
it->next = it->next->next;
vfree(thread);
}