Advertisement
CSenshi

OS - thread_create

Apr 8th, 2020
476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.31 KB | None | 0 0
  1. tid_t thread_create(const char *name, int priority,
  2.                     thread_func *function, void *aux)
  3. {
  4.   struct thread *t;
  5.   struct kernel_thread_frame *kf;
  6.   struct switch_entry_frame *ef;
  7.   struct switch_threads_frame *sf;
  8.   tid_t tid;
  9.  
  10.   ASSERT(function != NULL);
  11.  
  12.   /* Allocate thread. */
  13.   t = palloc_get_page(PAL_ZERO);
  14.   if (t == NULL)
  15.     return TID_ERROR;
  16.  
  17.   /* Initialize thread. */
  18.  
  19.   init_thread(t, name, priority);
  20.   tid = t->tid = allocate_tid();
  21.   t->parent = thread_current();
  22.   enum intr_level old_level = intr_disable();
  23.  
  24.   child_str *cs = palloc_get_page(0);
  25.   cs->wait_stat = 0;
  26.   cs->t = t;
  27.   cs->tid = tid;
  28.   cs->waited = 0;
  29.   list_push_back(&thread_current()->child_list, &cs->child_elem);
  30.  
  31.   intr_set_level(old_level);
  32.  
  33.   t->cwd = NULL;
  34.   if (thread_current()->cwd)
  35.     t->cwd = dir_reopen(thread_current()->cwd);
  36.  
  37.   /* Stack frame for kernel_thread(). */
  38.   kf = alloc_frame(t, sizeof *kf);
  39.   kf->eip = NULL;
  40.   kf->function = function;
  41.   kf->aux = aux;
  42.  
  43.   /* Stack frame for switch_entry(). */
  44.   ef = alloc_frame(t, sizeof *ef);
  45.   ef->eip = (void (*)(void))kernel_thread;
  46.  
  47.   /* Stack frame for switch_threads(). */
  48.   sf = alloc_frame(t, sizeof *sf);
  49.   sf->eip = switch_entry;
  50.   sf->ebp = 0;
  51.  
  52.   /* Add to run queue. */
  53.   thread_unblock(t);
  54.  
  55.   return tid;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement