matos/core/kthread.c

68 lines
2.0 KiB
C
Raw Normal View History

2020-04-23 00:49:09 +02:00
#include "kthread.h"
#include "list.h"
#include "alloc.h"
#include "klibc.h"
static struct kthread *currentThread;
void _kthread_exit(){
//TODO
return;
}
int kthreadSetup(vaddr_t mainStack, size_t mainStackSize){
struct kthread * current = (struct kthread *)malloc(sizeof(struct kthread));
strzcpy(current->name, "[KINIT]", KTHREAD_NAME_MAX_LENGTH);
current->stackAddr = mainStack;
current->stackSize = mainStackSize;
list_singleton(currentThread, current);
return 0;
}
struct kthread *createKthread(const char *name, cpu_kstate_function_arg1_t func,
void *args)
{
struct kthread *thread = (struct kthread *)malloc(sizeof(struct kthread));
if (!thread)
return NULL;
thread->stackAddr = (vaddr_t) malloc(KTHREAD_DEFAULT_STACK_SIZE);
thread->stackSize = KTHREAD_DEFAULT_STACK_SIZE;
if(!thread->stackAddr)
goto free_mem;
if(name)
strzcpy(thread->name, name, KTHREAD_NAME_MAX_LENGTH);
else
strzcpy(thread->name, "[UNKNOW]", KTHREAD_NAME_MAX_LENGTH);
if(cpu_kstate_init(&thread->cpuState, (cpu_kstate_function_arg1_t *)func, (vaddr_t)args,
thread->stackAddr, thread->stackSize,
(cpu_kstate_function_arg1_t *)_kthread_exit, 0))
goto free_mem;
list_add_tail(currentThread, thread);
return thread;
free_mem:
free((void *)thread->stackAddr);
free((void *)thread);
return NULL;
}
void deleteKthread(struct kthread *thread){
list_delete(currentThread, thread);
free((void *)thread->stackAddr);
free((void *)thread);
}
struct cpu_state *selectNextThread(struct cpu_state *prevCpu){
currentThread->cpuState = prevCpu;
struct kthread *nextThread = currentThread->next;
currentThread = nextThread;
return nextThread->cpuState;
}