Voxia OS v0.0.1
Hobby Project Operating System Targeting x86-64
Loading...
Searching...
No Matches
thread.c
Go to the documentation of this file.
1#include "procc/thread.h"
2#include "autoconf.h"
3#include "init/init.h"
4#include "libk/serial.h"
5#include "memory/slab.h"
6#include "scheduler.h"
7#include <hal/cpu/core.h>
8#include <str.h>
9
10static struct slab_cache* thread_cache = nullptr;
12
13INIT(Thread) {
14 vxCreateSlabCache(&thread_cache, "thread", sizeof(thread_t), 0, 0);
15}
16
18 for (thread_id i = bucket.top_free; i < VOXIA_MAX_NUMBER_THREAD; i++) {
19 if (!bucket.slot[i].used) {
20 bucket.slot[i].used = true;
21 bucket.slot[i].gen++;
22 LOG2_DEBUG("THREAD", "NEW SLOT Gen %d Id %d",
23 bucket.slot[i].gen, i);
24 return THREAD_MAKE_ID(i, bucket.slot[i].gen);
25 }
26 }
27 bucket.top_free++;
28 return nullptr;
29}
30
34
35
36static void vxUpdateThreadSlot(const thread_id id, thread_t* thr) {
37 const uint32_t idx = THREAD_GET_ID(id);
38 bucket.slot[idx].thread = thr;
39}
40
45 if (!thr)
46 return nullptr;
47 memset(thr, 0, sizeof(thread_t));
48
49 serial2_printf("created thread at 0x%x \n", thr);
50 thr->id = thrAcquireNewSlot();
51 thr->entry_addr = entry;
53 thr->priority = priority;
54 thr->flags = flags;
55 thr->stack = stack;
57 thr->page = page;
58
59 // kernel stack
60 void* kstack = kalloc(0x4000);
61 thr->kernel_stack_base = (uintptr_t)kstack;
62 thr->kernel_stack_top = (uintptr_t)kstack + 0x4000;
63 thr->kernel_rsp = (thr->kernel_stack_top & ~(uintptr_t)0xF) - 8;
64
65 vxUpdateThreadSlot(thr->id, thr);
66 LOG2_DEBUG("THREAD", "created thread %d", thr->id);
67 return thr;
68}
69
71 const uint16_t core_id = get_current_core_cpuid();
72 auto queue = vxSchedulerGetCurrentQueue(core_id);
73 queue->thread->state = THREAD_STATE_TERMINATED;
74 for (;;)
75 __asm__ volatile(
76 "hlt"); // LOG_DEBUG("THREAD", "acalled thread exit");
77}
uint8_t get_current_core_cpuid()
uint8_t priority
Definition thread.h:4
struct thread thread_t
Definition thread.h:29
uint64_t stack
Definition thread.h:13
uint16_t flags
Definition thread.h:5
#define THREAD_MAKE_ID(id, gen)
Definition thread.h:60
@ THREAD_STATE_TERMINATED
Definition thread.h:26
@ THREAD_STATE_CREATE
Definition thread.h:22
uint64_t thread_id
Definition thread.h:12
uint16_t core_affinity
Definition thread.h:2
struct thread_bucket thread_bucket_t
#define THREAD_GET_ID(thread_id)
Definition thread.h:61
#define INIT(fn)
Definition init.h:26
void serial2_printf(const char *fmt,...)
void * kalloc(size_t size)
uintptr_t page
Definition paging.c:0
scheduler_queue_t * vxSchedulerGetCurrentQueue(uint16_t core)
Definition scheduler.c:29
#define LOG2_DEBUG(mod, fmt,...)
Definition serial.h:35
void * vxSlabAlloc(struct slab_cache *cache)
Definition slab.c:93
void vxCreateSlabCache(struct slab_cache **cache, const char *name, const size_t obj_size, size_t alignment, const uintptr_t virt_addr)
Definition slab.c:44
void memset(void *ptr, int value, size_t num)
uint16_t core_affinity
Definition thread.h:33
uintptr_t kernel_rsp
Definition thread.h:48
uint16_t flags
Definition thread.h:36
uint64_t stack
Definition thread.h:44
volatile uintptr_t * page
Definition thread.h:29
uint8_t priority
Definition thread.h:35
uintptr_t kernel_stack_base
Definition thread.h:49
thread_id id
Definition thread.h:32
uintptr_t kernel_stack_top
Definition thread.h:50
uint8_t state
Definition thread.h:34
uintptr_t entry_addr
Definition thread.h:43
void vxThreadExit()
Definition thread.c:70
static thread_id thrAcquireNewSlot()
Definition thread.c:17
static thread_bucket_t bucket
Definition thread.c:11
static struct slab_cache * thread_cache
Definition thread.c:10
static thread_t * thrCreateInstance()
Definition thread.c:31
static void vxUpdateThreadSlot(const thread_id id, thread_t *thr)
Definition thread.c:36
thread_t * create_thread(volatile uintptr_t *page, uintptr_t entry, uintptr_t stack, uint16_t core_affinity, uint8_t priority, uint16_t flags)
Definition thread.c:41
unsigned short uint16_t
Definition type.h:13
unsigned int uint32_t
Definition type.h:19
unsigned long uintptr_t
Definition type.h:73
unsigned char uint8_t
Definition type.h:7
uint16_t idx
Definition virtio-gpu.hpp:1
workqueue_t * queue
Definition voxmo.h:9