Пример #1
0
/* Up or "V" operation on a semaphore.  Increments SEMA's value
   and wakes up one thread of those waiting for SEMA, if any.

   This function may be called from an interrupt handler. */
void
sema_up (struct semaphore *sema) 
{
  enum intr_level old_level;

  ASSERT (sema != NULL);

  old_level = intr_disable ();
  sema->value++;
  struct list_elem *next;
  if (list_empty (&sema->waiters))
  {
    next = NULL;
  }
  else
  {
    next = list_max(&sema->waiters, thread_priority_compare, NULL);
    list_remove (next);
    thread_unblock (list_entry (next, struct thread, elem));
  }
  
  if (next && thread_priority_compare(&thread_current()->elem, next, NULL))
  {
    thread_yield();
  }
  intr_set_level (old_level);
}
Пример #2
0
struct list_elem *
list_pop_max (struct list *list, list_less_func *less, void *aux)
{
  struct list_elem *m = list_max (list, less, aux);
  list_remove (m);
  return m;
}
Пример #3
0
/* Up or "V" operation on a semaphore.  Increments SEMA's value
   and wakes up one thread of those waiting for SEMA, if any.

   This function may be called from an interrupt handler. 

   The thread with the highest priority in the waiting list gets added
   to the ready_list. If it has a higher priority than the    
   running thread, then the running thread will be preempted. */
void
sema_up (struct semaphore *sema) 
{
  enum intr_level old_level;

  ASSERT (sema != NULL);

  old_level = intr_disable ();
  struct thread *t = NULL;

  if (!list_empty (&sema->waiters)) 
  {
    struct list_elem *next = list_max(&sema->waiters, list_comp_greater, 0);
    list_remove(next);
    t = list_entry (next, struct thread, elem);
    thread_unblock (t);
  }
   
  sema->value++;
  if(t != NULL && !intr_context()) 
  {
    check_preempt(t, false);
  }
  intr_set_level (old_level);
}
Пример #4
0
/* Up or "V" operation on a semaphore.  Increments SEMA's value
   and wakes up one thread of those waiting for SEMA, if any.

   This function may be called from an interrupt handler. */
void
sema_up (struct semaphore *sema) 
{
  enum intr_level old_level;
  struct thread *  tmp_t = thread_current();

  ASSERT (sema != NULL);
  old_level = intr_disable ();
  //unblock the higherst pri on the waiter list
  if (!list_empty (&sema->waiters)) 
  {
	  //printf("\nsema->waiters list size: %d\n", list_size(&sema->waiters));
	  struct list_elem *tmp_e = list_max((&sema->waiters), find_less_pri, NULL);
	  list_remove(tmp_e);
	  tmp_t = list_entry(tmp_e, struct thread, elem);
	  thread_unblock(tmp_t);
  }
                                
  sema->value++;
  intr_set_level (old_level);
  
  //if the curr thread doesnt have the highest anymore, yield
  if(thread_current() != tmp_t){
	  if (thread_get_priority() < get_pri(tmp_t)) 
	  { 
			thread_yield();
	  }
  }
}
Пример #5
0
/* Up or "V" operation on a semaphore.  Increments SEMA's value
   and wakes up one thread of those waiting for SEMA, if any.

   This function may be called from an interrupt handler. */
void
sema_up (struct semaphore *sema)
{
    enum intr_level old_level;

    ASSERT (sema != NULL);
    old_level = intr_disable ();
    //sema->value++;
    //int y=sema_value;
    if (!list_empty (&sema->waiters))
    {
        /*   //Original Code Starts
        thread_unblock (list_entry (list_pop_front (&sema->waiters),
                                 struct thread, elem));

            //Original Code Ends
        */
        //Finds Maximum Priority Thread Waiting on the lock
        struct list_elem *a1;
        struct thread* t;
        a1 = list_max( &sema->waiters, &(max_priority), 0 );
        t = list_entry (a1, struct thread, elem);
        printf ("Loc: Sema_Up %s\n", t->name );
        list_remove( a1 );
        //while(sema_value==y)
        sema->value++;
        thread_unblock( t );
    }
    else
    {
Пример #6
0
/* Up or "V" operation on a semaphore.  Increments SEMA's value
   and wakes up one thread of those waiting for SEMA, if any.

   This function may be called from an interrupt handler. */
void
sema_up (struct semaphore *sema) 
{
  enum intr_level old_level;
  ASSERT (sema != NULL);
  old_level = intr_disable ();
  sema->value++;
  if (!list_empty (&sema->waiters))
   {
     struct list_elem *e = list_max (&sema->waiters, priority_less, NULL);
     list_remove (e);
     thread_unblock(list_entry (e, struct thread, elem));
   }
  intr_set_level (old_level);
}
Пример #7
0
link list_sort(link h) {
	link max, t, out = NULL;
	
	while (h->next != NULL) {
		max = list_max(h);
		t = max->next;
		max->next = t->next;
		t->next = out;
		out = t;
	}
	
	h->next = out;
	
	return h;
}
Пример #8
0
/* Up or "V" operation on a semaphore.  Increments SEMA's value
   and wakes up one thread of those waiting for SEMA, if any.

   This function may be called from an interrupt handler. */
void
sema_up (struct semaphore *sema) 
{
  enum intr_level old_level;

  ASSERT (sema != NULL);

  old_level = intr_disable ();
  if (!list_empty (&sema->waiters)) {
		struct list_elem *e = list_max (&sema->waiters, &compare_func, 0);
		list_remove (e);
		sema->value++;				//this allows the the first semup to work correctly 
    thread_unblock (list_entry (e, struct thread, elem));
  }
  else
		sema->value++;
  
  intr_set_level (old_level);
}
Пример #9
0
/* Up or "V" operation on a semaphore.  Increments SEMA's value
   and wakes up one thread of those waiting for SEMA, if any.

   This function may be called from an interrupt handler. */
void
sema_up (struct semaphore *sema) 
{
  enum intr_level old_level;

  ASSERT (sema != NULL);

  old_level = intr_disable ();
  if (!list_empty (&sema->waiters))
  {
    // Unblock highest priority thread first
    struct list_elem *e = list_max(&sema->waiters, comp_priority, NULL);
    list_remove(e);
    thread_unblock (list_entry(e, struct thread, elem));
  }
  sema->value++;
  // Yield if necessary after Unblock
  thread_yield_priority();
  intr_set_level (old_level);
}
Пример #10
0
/* Removes the lock from donating locks, then it finds the next priority to be assigned
 * to the releasing thread. */ 
void
lock_release (struct lock *lock) 
{
  ASSERT (lock != NULL);
  ASSERT (lock_held_by_current_thread (lock));
	
	/* Removing the lock from the donating_lists. */
	list_remove(&lock->elem);
	
	
	/* Getting the next max. priority for the lock holder, either one of the locks, or the 
	 * initial priority. */
	struct list_elem *e;
	struct thread *holder= lock->holder;
	
	if(!list_empty(&holder->donating_locks)){
		e = list_max (&holder->donating_locks, &lock_comparator, 0);
		struct lock *mx_lock= list_entry(e, struct lock, elem);
		holder->priority= mx_lock->mx_priority;
	}
Пример #11
0
/* Up or "V" operation on a semaphore.  Increments SEMA's value
   and wakes up one thread of those waiting for SEMA, if any.

   This function may be called from an interrupt handler. */
void
sema_up (struct semaphore *sema) 
{
  enum intr_level old_level;

  ASSERT (sema != NULL);

  old_level = intr_disable ();
  if (!list_empty (&sema->waiters)) {
    // P2: find out thread with highest priority.
    struct list_elem* max_elem = list_max(&sema->waiters, priority_less, NULL);
    list_remove (max_elem);
   
    thread_unblock (list_entry (max_elem, struct thread, elem));
  }
  sema->value++;
  intr_set_level (old_level);
  
  thread_yield(); // p2
}
Пример #12
0
fid_t
file_insert_in_fd (struct file *file)
{
  struct list_elem *e;
  struct file *f;
  struct thread *t = thread_current ();
  
  if(list_empty(&(t->open_file_list)))
  {
    file->fid = 3;
  }
  else
  {
    e = list_max(&(t->open_file_list), less_fid, NULL);
    f = list_entry(e, struct file, elem);
    file->fid = f->fid + 1;
  }
  list_push_back(&t->open_file_list, &file->elem);
 
  return file->fid;
}
Пример #13
0
/* Up or "V" operation on a semaphore.  Increments SEMA's value
   and wakes up one thread of those waiting for SEMA, if any.

   This function may be called from an interrupt handler. */
void
sema_up (struct semaphore *sema) 
{
  enum intr_level old_level;

  ASSERT (sema != NULL);
  struct thread *t=NULL;
  old_level = intr_disable ();
  if (!list_empty (&sema->waiters)) {
	  struct list_elem *e=list_max(&sema->waiters, thread_max_priority,NULL);
	  t= list_entry(e, struct thread, elem);
	  list_remove(e);
	  thread_unblock(t);
    //thread_unblock (list_entry (list_pop_front (&sema->waiters),struct thread, elem));
  }
  sema->value++;
  intr_set_level (old_level);
  if(t && t->priority>thread_current()->priority){
	  if(intr_context()) intr_yield_on_return();
	  else thread_yield();
  }
}
Пример #14
0
void lock_sema_up (struct semaphore *sema, struct thread* holder) {
  ASSERT (!thread_mlfqs);
  enum intr_level old_level;

  ASSERT (sema != NULL);

  old_level = intr_disable ();
  if (!list_empty (&sema->waiters)) {
    // P2: find out thread with highest priority.
    struct list_elem* max_elem = list_max(&sema->waiters, priority_less, NULL);
    list_remove (max_elem);

    // P2: calculate and assign donate priority.
    donate_priority (holder, calculate_donated_priority_up(holder));
    
    thread_unblock (list_entry (max_elem, struct thread, elem));
  }
  sema->value++;
  intr_set_level (old_level);
  
  thread_yield(); // p2
}
Пример #15
0
/* Up or "V" operation on a semaphore.  Increments SEMA's value
   and wakes up one thread of those waiting for SEMA, if any.

   When determining which thread to unblock, awaken the one
   with the highest priority.  If the priority of the thread
   that will be awoken is higher than that of the current thread
   yield the CPU to switch threads.

   This function may be called from an interrupt handler. */
void
sema_up (struct semaphore *sema)
{
  enum intr_level old_level;
  bool yield = false;

  ASSERT (sema != NULL);

  struct list_elem *thread_max_elem = list_max (&sema->waiters,
    priority_less, NULL);
  struct thread *thread_max = list_entry (thread_max_elem,
    struct thread, elem);

  /* Added minimal number of lines between disabled interrupts */
  old_level = intr_disable ();
  if (!list_empty (&sema->waiters))
    {
      list_remove (thread_max_elem);
      thread_unblock (thread_max);

      /* Check the appropriate priority, depending on which scheduler
         is being used.  This produces unwanted behavior when
         interfacing with user programs, but was left in due to
         functionality in the first project. */

      /* if ((!thread_mlfqs && thread_max->priority >
         thread_current ()->priority) ||
         (thread_mlfqs && thread_max->mlfqs_priority >
         thread_current ()->mlfqs_priority))
          yield = true; */
    }

  sema->value++;
  intr_set_level (old_level);
  if (yield)
    thread_yield ();
}
Пример #16
0
  lock_release (lock);
  sema_down (&waiter.semaphore);
  lock_acquire (lock);
}


static bool
cond_compare_priorities(const struct list_elem *left, 
                        const struct list_elem *right,
                        void *aux UNUSED)
{
  struct semaphore_elem *lSe, *rSe;
  struct list_elem      *lLe, *rLe;

  lSe = list_entry(left, struct semaphore_elem, elem);
  lLe = list_max (&lSe->semaphore.waiters, thread_priority_compare, NULL);

  rSe = list_entry(right, struct semaphore_elem, elem);
  rLe = list_max (&rSe->semaphore.waiters, thread_priority_compare, NULL);

  return thread_priority_compare(lLe, rLe, NULL);
}
/* If any threads are waiting on COND (protected by LOCK), then
   this function signals one of them to wake up from its wait.
   LOCK must be held before calling this function.

   An interrupt handler cannot acquire a lock, so it does not
   make sense to try to signal a condition variable within an
   interrupt handler. */
void
cond_signal (struct condition *cond, struct lock *lock UNUSED)