Some threading code cleanup

This commit is contained in:
Thomas Lovén 2018-01-16 19:52:38 +01:00
parent 251b5f71c9
commit c26434f814
3 changed files with 20 additions and 23 deletions

View File

@ -10,11 +10,11 @@
void thread_function() void thread_function()
{ {
int tid = get_tid(); int thread_id = get_tid();
while(1) while(1)
{ {
debug("Thread %d\n", tid); debug("Thread %d\n", thread_id);
yield(); yield();
} }
} }

View File

@ -15,25 +15,22 @@ struct tcb
struct thread struct thread
{ {
union { uint8_t stack[TCB_OFFSET-SWTCH_STACK_SIZE];
uint8_t stack[TCB_OFFSET]; uint64_t RBP;
struct { uint64_t RBX;
uint8_t _stck[TCB_OFFSET-SWTCH_STACK_SIZE]; uint64_t R12;
uint64_t RBP; uint64_t R13;
uint64_t RBX; uint64_t R14;
uint64_t R12; uint64_t R15;
uint64_t R13; uint64_t RBP2;
uint64_t R14; uint64_t ret;
uint64_t R15;
uint64_t RBP2;
uint64_t ret;
};
};
struct tcb tcb; struct tcb tcb;
}; }__attribute__((packed));
struct thread *current_thread; struct thread *current_thread;
#define CURRENT_THREAD() (current_thread) #define GET_CURRENT_THREAD() (current_thread)
#define SET_CURRENT_THREAD(thread) current_thread = (thread)
struct thread *new_thread(void (*function)(void)); struct thread *new_thread(void (*function)(void));
void yield(); void yield();

View File

@ -4,12 +4,12 @@
struct thread dummy; struct thread dummy;
struct thread *current_thread = 0; struct thread *current_thread = 0;
uint64_t 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 *th = P2V(pmm_alloc());
th->tcb.tid = tid++; th->tcb.tid = next_tid++;
th->RBP = (uint64_t)&th->RBP2; th->RBP = (uint64_t)&th->RBP2;
th->ret = (uint64_t)function; th->ret = (uint64_t)function;
th->tcb.stack_ptr = (uint64_t)&th->RBP; th->tcb.stack_ptr = (uint64_t)&th->RBP;
@ -19,7 +19,7 @@ struct thread *new_thread(void (*function)(void))
void switch_thread(struct thread *old, struct thread *new) void switch_thread(struct thread *old, struct thread *new)
{ {
current_thread = new; SET_CURRENT_THREAD(new);
swtch(&old->tcb.stack_ptr, &new->tcb.stack_ptr); swtch(&old->tcb.stack_ptr, &new->tcb.stack_ptr);
} }
@ -27,7 +27,7 @@ void yield()
{ {
struct thread *old, *new; struct thread *old, *new;
old = current_thread; old = GET_CURRENT_THREAD();
if(old) if(old)
ready(old); ready(old);
else else
@ -39,5 +39,5 @@ void yield()
int get_tid() int get_tid()
{ {
return CURRENT_THREAD()->tcb.tid; return GET_CURRENT_THREAD()->tcb.tid;
} }