alarm: Clean up condition variable usage
authorBarret Rhoden <brho@cs.berkeley.edu>
Thu, 11 Oct 2018 20:33:14 +0000 (16:33 -0400)
committerBarret Rhoden <brho@cs.berkeley.edu>
Thu, 11 Oct 2018 20:33:14 +0000 (16:33 -0400)
The while loop is the classic Mesa-semantic use of a CV.  For some
reason, the old version was a tortured while loop with breaks and
whatnot.  The new version is simpler, easier to read, classically
correct, and has a nicer personality.

Signed-off-by: Barret Rhoden <brho@cs.berkeley.edu>
kern/src/alarm.c
user/parlib/alarm.c

index 764fdfc..d75eb1d 100644 (file)
@@ -312,13 +312,9 @@ bool unset_alarm(struct timer_chain *tchain, struct alarm_waiter *waiter)
        spin_unlock_irqsave(&tchain->lock);
 
        cv_lock_irqsave(&waiter->done_cv, &irq_state);
        spin_unlock_irqsave(&tchain->lock);
 
        cv_lock_irqsave(&waiter->done_cv, &irq_state);
-       while (1) {
-               if (!waiter->is_running) {
-                       cv_unlock_irqsave(&waiter->done_cv, &irq_state);
-                       break;
-               }
+       while (waiter->is_running)
                cv_wait(&waiter->done_cv);
                cv_wait(&waiter->done_cv);
-       }
+       cv_unlock_irqsave(&waiter->done_cv, &irq_state);
 
        waiter->no_rearm = false;
        return false;
 
        waiter->no_rearm = false;
        return false;
index e7cb27e..3cd3cee 100644 (file)
@@ -439,13 +439,9 @@ static bool __tc_unset_alarm(struct timer_chain *tchain,
        spin_pdr_unlock(&tchain->lock);
 
        uth_cond_var_lock(&waiter->done_cv);
        spin_pdr_unlock(&tchain->lock);
 
        uth_cond_var_lock(&waiter->done_cv);
-       while (1) {
-               if (!waiter->is_running) {
-                       uth_cond_var_unlock(&waiter->done_cv);
-                       break;
-               }
+       while (waiter->is_running)
                uth_cond_var_wait(&waiter->done_cv, NULL);
                uth_cond_var_wait(&waiter->done_cv, NULL);
-       }
+       uth_cond_var_unlock(&waiter->done_cv);
 
        waiter->no_rearm = false;
        return false;
 
        waiter->no_rearm = false;
        return false;