group/pintos/src/threads/thread.c
/* Creates a new kernel thread named NAME with the given initial
   PRIORITY, which executes FUNCTION passing AUX as the argument,
   and adds it to the ready queue.  Returns the thread identifier
   for the new thread, or TID_ERROR if creation fails.

   If thread_start() has been called, then the new thread may be
   scheduled before thread_create() returns.  It could even exit
   before thread_create() returns.  Contrariwise, the original
   thread may run for any amount of time before the new thread is
   scheduled.  Use a semaphore or some other form of
   synchronization if you need to ensure ordering.

   The code provided sets the new thread's `priority' member to
   PRIORITY, but no actual priority scheduling is implemented.
   Priority scheduling is the goal of Problem 1-3. */
Creates a new kernel thread named NAME with the given initial
PRIORITY, which executes FUNCTION passing AUX as the argument,
and adds it to the ready queue.  Returns the thread identifier
for the new thread, or TID_ERROR if creation fails.

If thread_start() has been called, then the new thread may be
scheduled before thread_create() returns.  It could even exit
before thread_create() returns.  Contrariwise, the original
thread may run for any amount of time before the new thread is
scheduled.  Use a semaphore or some other form of
synchronization if you need to ensure ordering.

The code provided sets the new thread's `priority' member to
PRIORITY, but no actual priority scheduling is implemented.
Priority scheduling is the goal of Problem 1-3.
Creates a new kernel thread named NAME with the given initial PRIORITY which executes FUNCTION passing AUX as the argument and adds it to the ready queue . Returns the thread identifier for the new thread or TID_ERROR if creation fails . If thread_start () has been called then the new thread may be scheduled before thread_create () returns . It could even exit before thread_create () returns . Contrariwise the original thread may run for any amount of time before the new thread is scheduled . Use a semaphore or some other form of synchronization if you need to ensure ordering . The code provided sets the new thread ' s ` priority ' member to PRIORITY but no actual priority scheduling is implemented . Priority scheduling is the goal of Problem 1 - 3 .
tid_t
thread_create (const char *name, int priority,
               thread_func *function, void *aux)
{
  struct thread *t;
  struct kernel_thread_frame *kf;
  struct switch_entry_frame *ef;
  struct switch_threads_frame *sf;
  tid_t tid;
  struct thread* cur = thread_current();
  struct thread_wait_context *wctx;
  ASSERT (function != NULL);
  t = palloc_get_page (PAL_ZERO);
  if (t == NULL)
    return TID_ERROR;
  init_thread (t, name, priority);
  tid = t->tid = allocate_tid ();
  if (cur != NULL) {
    wctx = malloc(sizeof(struct thread_wait_context));
    wctx->tid = tid;
    sema_init(&wctx->finish_sema, 0);
    wctx->exit_status = 0;
    wctx->waited = 0;
    list_push_front(&cur->children, &wctx->children_elem);
    t->wait_ctx = wctx;
  }
  kf = alloc_frame (t, sizeof *kf);
  kf->eip = NULL;
  kf->function = function;
  kf->aux = aux;
  ef = alloc_frame (t, sizeof *ef);
  ef->eip = (void (*) (void)) kernel_thread;
  sf = alloc_frame (t, sizeof *sf);
  sf->eip = switch_entry;
  sf->ebp = 0;
  thread_unblock (t);
  thread_yield();
  return tid;
}
tid_t thread_create ( const char * name , int priority , thread_func * function , void * aux ) { struct thread * t ; struct kernel_thread_frame * kf ; struct switch_entry_frame * ef ; struct switch_threads_frame * sf ; tid_t tid ; struct thread * cur = thread_current ( ) ; struct thread_wait_context * wctx ; ASSERT ( function != NULL ) ; t = palloc_get_page ( PAL_ZERO ) ; if ( t == NULL ) return TID_ERROR ; init_thread ( t , name , priority ) ; tid = t -> tid = allocate_tid ( ) ; if ( cur != NULL ) { wctx = malloc ( sizeof ( struct thread_wait_context ) ) ; wctx -> tid = tid ; sema_init ( & wctx -> finish_sema , 0 ) ; wctx -> exit_status = 0 ; wctx -> waited = 0 ; list_push_front ( & cur -> children , & wctx -> children_elem ) ; t -> wait_ctx = wctx ; } kf = alloc_frame ( t , sizeof * kf ) ; kf -> eip = NULL ; kf -> function = function ; kf -> aux = aux ; ef = alloc_frame ( t , sizeof * ef ) ; ef -> eip = ( void ( * ) ( void ) ) kernel_thread ; sf = alloc_frame ( t , sizeof * sf ) ; sf -> eip = switch_entry ; sf -> ebp = 0 ; thread_unblock ( t ) ; thread_yield ( ) ; return tid ; }
Creates a new kernel thread named NAME with the given initial
PRIORITY, which executes FUNCTION passing AUX as the argument,
and adds it to the ready queue.
Creates a new kernel thread named NAME with the given initial PRIORITY which executes FUNCTION passing AUX as the argument and adds it to the ready queue .
/* Allocate thread. */ /* Initialize thread. */ /* Stack frame for kernel_thread(). */ /* Stack frame for switch_entry(). */ /* Stack frame for switch_threads(). */ /* Add to run queue. */