Pass around tcbs instead of thread stacks

This commit is contained in:
Thomas Lovén 2018-01-26 15:02:13 +01:00
parent 1fca5af522
commit 13946b1758
3 changed files with 17 additions and 14 deletions

View File

@ -2,7 +2,7 @@
#include <memory.h> #include <memory.h>
struct tcb struct thread
{ {
uintptr_t stack_ptr; uintptr_t stack_ptr;
uint64_t tid; uint64_t tid;
@ -10,10 +10,10 @@ struct tcb
struct thread *next; struct thread *next;
}; };
#define TCB_OFFSET (PAGE_SIZE - sizeof(struct tcb)) #define TCB_OFFSET (PAGE_SIZE - sizeof(struct thread))
#define SWTCH_STACK_SIZE (0x8*8) #define SWTCH_STACK_SIZE (0x8*8)
struct thread struct thread_stack
{ {
uint8_t stack[TCB_OFFSET-SWTCH_STACK_SIZE]; uint8_t stack[TCB_OFFSET-SWTCH_STACK_SIZE];
uint64_t RBP; uint64_t RBP;
@ -25,9 +25,11 @@ struct thread
uint64_t RBP2; uint64_t RBP2;
uint64_t ret; uint64_t ret;
struct tcb tcb; struct thread tcb;
}__attribute__((packed)); }__attribute__((packed));
#define thread_stack(th) (struct thread_stack *)((uintptr_t)th - TCB_OFFSET)
struct thread *new_thread(void (*function)(void)); struct thread *new_thread(void (*function)(void));
void yield(); void yield();
int get_tid(); int get_tid();

View File

@ -9,10 +9,10 @@ void ready(struct thread *th)
{ {
if(!readyQ.last) if(!readyQ.last)
{ {
th->tcb.next = 0; th->next = 0;
readyQ.first = readyQ.last = th; readyQ.first = readyQ.last = th;
} else { } else {
readyQ.last->tcb.next = th; readyQ.last->next = th;
readyQ.last = th; readyQ.last = th;
} }
} }
@ -20,7 +20,7 @@ void ready(struct thread *th)
struct thread *scheduler_next() struct thread *scheduler_next()
{ {
struct thread *th = readyQ.first; struct thread *th = readyQ.first;
if(!(readyQ.first = th->tcb.next)) if(!(readyQ.first = th->next))
readyQ.last = 0; readyQ.last = 0;
return th; return th;
} }

View File

@ -7,12 +7,13 @@ struct thread *_current_thread = 0;
uint64_t next_tid = 1; uint64_t next_tid = 1;
struct thread *new_thread(void (*function)(void)) struct thread *new_thread(void (*function)(void))
{ {
struct thread *th = P2V(pmm_alloc()); struct thread_stack *stk = P2V(pmm_alloc());
struct thread *th = &stk->tcb;
th->tcb.tid = next_tid++; th->tid = next_tid++;
th->RBP = (uint64_t)&th->RBP2; stk->RBP = (uint64_t)&stk->RBP2;
th->ret = (uint64_t)function; stk->ret = (uint64_t)function;
th->tcb.stack_ptr = (uint64_t)&th->RBP; th->stack_ptr = (uint64_t)&stk->RBP;
return th; return th;
} }
@ -30,7 +31,7 @@ void set_current_thread(struct thread *th)
void switch_thread(struct thread *old, struct thread *new) void switch_thread(struct thread *old, struct thread *new)
{ {
set_current_thread(new); set_current_thread(new);
swtch(&old->tcb.stack_ptr, &new->tcb.stack_ptr); swtch(&old->stack_ptr, &new->stack_ptr);
} }
void yield() void yield()
@ -49,5 +50,5 @@ void yield()
int get_tid() int get_tid()
{ {
return current_thread()->tcb.tid; return current_thread()->tid;
} }