next up previous contents
Next: Kernel Manual Up: Scheduler API Previous: FIFO Scheduler   Contents

EDF Scheduler

/*
 The deadline of the new thread is relative & hence jiffies is added to 
 make it absolute. Then it is inserted into the heap ordered by deadline   
*/

oskit_u32_t init(struct tcb *main_tcb)
{
  int i;
  for(i=0;i<MAX_THREADS;++i)
    {
      tcb_heap[i]=NULL;
    }
  heap_count=0;
  main_tcb->thread_params.deadline+=jiffies;
  if (insert_to_heap(main_tcb)<0)
    return -1;
  main_tcb->status=THREAD_ON_CPU;
  return 1;
}


oskit_u32_t new_thread(struct tcb *thid)
{
  thid->thread_params.deadline+=jiffies;
  if (insert_to_heap(thid)<0)
     return -1;
  thid->status=THREAD_READY;
  return 1;
}

oskit_u32_t delete_thread(struct tcb *arg)
{
  delete_from_heap(arg);
  arg->status=THREAD_EXITED;
  return 1;
}
/*
  First delete the thread- Note not imp since only exited threads 
  can be deleted & hence they have been already deleted earlier (when it
  exited.
*/

oskit_u32_t reset_thread(struct tcb *arg)
{
  delete_from_heap(arg);
  arg->thread_params.deadline+=jiffies;
  if (insert_to_heap(arg)<0)
     return -1;
  arg->status=THREAD_READY;
  return 1;
}
/* 
   The semantics does not require the scheduler to keep track of NON-READY
   threads. So we can safely delete those threads from heap
*/
oskit_u32_t set_mode(struct tcb* arg,oskit_u32_t mode)
{
  switch (mode)
    {
     case THREAD_BLOCKED:
       delete_from_heap(arg);
       arg->status=THREAD_BLOCKED;
       break;
     case THREAD_SUSPENDED:
       delete_from_heap(arg);
       arg->status=THREAD_SUSPENDED;
       break;
     case THREAD_READY:
       insert_to_heap(arg);
       arg->status=THREAD_READY;
       break;
    case THREAD_EXITED:
      delete_from_heap(arg);
      arg->status=THREAD_EXITED;
      break;
     default:
       break;
    }
  return 1;
}
/*
  invariiant : The root of the heap is the heir thread 
  Note : It take care of following cases - think
  1> current thread is running & the thread with earliest deadline
     This might happen for example during timer interrupt
  2> current thread is ready but there is a new thread with earlier deadline
     This may happen as a consequence of thread_create system call
  3> current thread is not ready at all
     The current thread might have been blocked & hence deleted from heap
     In all the cases the top of the heap is heir thread.
*/

struct tcb *heir_thread(struct tcb *curr)
{
  if (heap_count==0) return NULL;
  while((heap_count>0)&&(tcb_heap[0]->thread_params.deadline)
	&&(tcb_heap[0]->thread_params.deadline < jiffies))
    {
      if (tcb_heap[0]->thread_params.recov_func)
	{
	  mark_bottom_half(tcb_heap[0]->thread_params.recov_func,tcb_heap[0]);
	}
      tcb_heap[0]->status=THREAD_MISSED_DEADLINE;
      delete_from_heap(tcb_heap[0]);
    }
  if (curr->status==THREAD_ON_CPU)
    curr->status=THREAD_READY;
  tcb_heap[0]->status=THREAD_ON_CPU;
  return tcb_heap[0];
}


Soumyadeb Mitra 2002-08-07