Some threading code cleanup
This commit is contained in:
		
							parent
							
								
									251b5f71c9
								
							
						
					
					
						commit
						c26434f814
					
				@ -10,11 +10,11 @@
 | 
			
		||||
 | 
			
		||||
void thread_function()
 | 
			
		||||
{
 | 
			
		||||
  int tid = get_tid();
 | 
			
		||||
  int thread_id = get_tid();
 | 
			
		||||
 | 
			
		||||
  while(1)
 | 
			
		||||
  {
 | 
			
		||||
    debug("Thread %d\n", tid);
 | 
			
		||||
    debug("Thread %d\n", thread_id);
 | 
			
		||||
    yield();
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -15,25 +15,22 @@ struct tcb
 | 
			
		||||
 | 
			
		||||
struct thread
 | 
			
		||||
{
 | 
			
		||||
  union {
 | 
			
		||||
    uint8_t stack[TCB_OFFSET];
 | 
			
		||||
    struct {
 | 
			
		||||
      uint8_t _stck[TCB_OFFSET-SWTCH_STACK_SIZE];
 | 
			
		||||
      uint64_t RBP;
 | 
			
		||||
      uint64_t RBX;
 | 
			
		||||
      uint64_t R12;
 | 
			
		||||
      uint64_t R13;
 | 
			
		||||
      uint64_t R14;
 | 
			
		||||
      uint64_t R15;
 | 
			
		||||
      uint64_t RBP2;
 | 
			
		||||
      uint64_t ret;
 | 
			
		||||
    };
 | 
			
		||||
  };
 | 
			
		||||
  uint8_t stack[TCB_OFFSET-SWTCH_STACK_SIZE];
 | 
			
		||||
  uint64_t RBP;
 | 
			
		||||
  uint64_t RBX;
 | 
			
		||||
  uint64_t R12;
 | 
			
		||||
  uint64_t R13;
 | 
			
		||||
  uint64_t R14;
 | 
			
		||||
  uint64_t R15;
 | 
			
		||||
  uint64_t RBP2;
 | 
			
		||||
  uint64_t ret;
 | 
			
		||||
 | 
			
		||||
  struct tcb tcb;
 | 
			
		||||
};
 | 
			
		||||
}__attribute__((packed));
 | 
			
		||||
 | 
			
		||||
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));
 | 
			
		||||
void yield();
 | 
			
		||||
 | 
			
		||||
@ -4,12 +4,12 @@
 | 
			
		||||
struct thread dummy;
 | 
			
		||||
struct thread *current_thread = 0;
 | 
			
		||||
 | 
			
		||||
uint64_t tid = 1;
 | 
			
		||||
uint64_t next_tid = 1;
 | 
			
		||||
struct thread *new_thread(void (*function)(void))
 | 
			
		||||
{
 | 
			
		||||
  struct thread *th = P2V(pmm_alloc());
 | 
			
		||||
 | 
			
		||||
  th->tcb.tid = tid++;
 | 
			
		||||
  th->tcb.tid = next_tid++;
 | 
			
		||||
  th->RBP = (uint64_t)&th->RBP2;
 | 
			
		||||
  th->ret = (uint64_t)function;
 | 
			
		||||
  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)
 | 
			
		||||
{
 | 
			
		||||
  current_thread = new;
 | 
			
		||||
  SET_CURRENT_THREAD(new);
 | 
			
		||||
  swtch(&old->tcb.stack_ptr, &new->tcb.stack_ptr);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -27,7 +27,7 @@ void yield()
 | 
			
		||||
{
 | 
			
		||||
  struct thread *old, *new;
 | 
			
		||||
 | 
			
		||||
  old = current_thread;
 | 
			
		||||
  old = GET_CURRENT_THREAD();
 | 
			
		||||
  if(old)
 | 
			
		||||
    ready(old);
 | 
			
		||||
  else
 | 
			
		||||
@ -39,5 +39,5 @@ void yield()
 | 
			
		||||
 | 
			
		||||
int get_tid()
 | 
			
		||||
{
 | 
			
		||||
  return CURRENT_THREAD()->tcb.tid;
 | 
			
		||||
  return GET_CURRENT_THREAD()->tcb.tid;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user