Index: kernel/generic/src/synch/condvar.c
===================================================================
--- kernel/generic/src/synch/condvar.c	(revision 597fa246c9481b40ae4d512256c4ceea4506b215)
+++ kernel/generic/src/synch/condvar.c	(revision 23d4e6aeb10c07b0fc1436805639ab2e50cf7514)
@@ -79,5 +79,5 @@
  * @return		See comment for waitq_sleep_timeout().
  */
-errno_t condvar_wait_timeout(condvar_t *cv, mutex_t *mtx, uint32_t usec)
+errno_t __condvar_wait_timeout_mutex(condvar_t *cv, mutex_t *mtx, uint32_t usec)
 {
 	wait_guard_t guard = waitq_sleep_prepare(&cv->wq);
@@ -92,5 +92,5 @@
 }
 
-errno_t condvar_wait(condvar_t *cv, mutex_t *mtx)
+errno_t __condvar_wait_mutex(condvar_t *cv, mutex_t *mtx)
 {
 	wait_guard_t guard = waitq_sleep_prepare(&cv->wq);
@@ -105,24 +105,7 @@
 }
 
-/** Wait for the condition to become true with a locked spinlock.
- *
- * The function is not aware of irq_spinlock. Therefore do not even
- * try passing irq_spinlock_t to it. Use _condvar_wait_timeout_irq_spinlock()
- * instead.
- *
- * @param cv		Condition variable.
- * @param lock		Locked spinlock.
- * @param usec		Timeout value in microseconds.
- * @param flags		Select mode of operation.
- *
- * For exact description of meaning of possible combinations of usec and flags,
- * see comment for waitq_sleep_timeout().  Note that when
- * SYNCH_FLAGS_NON_BLOCKING is specified here, EAGAIN is always
- * returned.
- *
- * @return See comment for waitq_sleep_timeout().
- */
-errno_t _condvar_wait_timeout_spinlock_impl(condvar_t *cv, spinlock_t *lock,
-    uint32_t usec, int flags)
+/** Same as __condvar_wait_timeout_mutex(), except for spinlock_t. */
+errno_t __condvar_wait_timeout_spinlock(condvar_t *cv, spinlock_t *lock,
+    uint32_t usec)
 {
 	wait_guard_t guard = waitq_sleep_prepare(&cv->wq);
@@ -131,5 +114,6 @@
 	spinlock_unlock(lock);
 
-	errno_t rc = waitq_sleep_timeout_unsafe(&cv->wq, usec, flags, guard);
+	errno_t rc = waitq_sleep_timeout_unsafe(&cv->wq, usec,
+	    SYNCH_FLAGS_NON_BLOCKING, guard);
 
 	spinlock_lock(lock);
@@ -137,20 +121,20 @@
 }
 
-/** Wait for the condition to become true with a locked irq spinlock.
- *
- * @param cv		Condition variable.
- * @param lock		Locked irq spinlock.
- * @param usec		Timeout value in microseconds.
- * @param flags		Select mode of operation.
- *
- * For exact description of meaning of possible combinations of usec and flags,
- * see comment for waitq_sleep_timeout().  Note that when
- * SYNCH_FLAGS_NON_BLOCKING is specified here, EAGAIN is always
- * returned.
- *
- * @return See comment for waitq_sleep_timeout().
- */
-errno_t _condvar_wait_timeout_irq_spinlock(condvar_t *cv, irq_spinlock_t *irq_lock,
-    uint32_t usec, int flags)
+errno_t __condvar_wait_spinlock(condvar_t *cv, spinlock_t *mtx)
+{
+	wait_guard_t guard = waitq_sleep_prepare(&cv->wq);
+
+	/* Unlock only after the waitq is locked so we don't miss a wakeup. */
+	spinlock_unlock(mtx);
+
+	errno_t rc = waitq_sleep_unsafe(&cv->wq, guard);
+
+	spinlock_lock(mtx);
+	return rc;
+}
+
+/** Same as __condvar_wait_timeout_mutex(), except for irq_spinlock_t. */
+errno_t __condvar_wait_timeout_irq_spinlock(condvar_t *cv,
+    irq_spinlock_t *irq_lock, uint32_t usec)
 {
 	errno_t rc;
@@ -171,5 +155,23 @@
 	 * running) and there is no danger of a deadlock.
 	 */
-	rc = _condvar_wait_timeout_spinlock(cv, &irq_lock->lock, usec, flags);
+	rc = __condvar_wait_timeout_spinlock(cv, &irq_lock->lock, usec);
+
+	irq_lock->guard = guard;
+	irq_lock->ipl = ipl;
+
+	return rc;
+}
+
+/** Same as __condvar_wait_mutex(), except for irq_spinlock_t. */
+errno_t __condvar_wait_irq_spinlock(condvar_t *cv, irq_spinlock_t *irq_lock)
+{
+	errno_t rc;
+	/* Save spinlock's state so we can restore it correctly later on. */
+	ipl_t ipl = irq_lock->ipl;
+	bool guard = irq_lock->guard;
+
+	irq_lock->guard = false;
+
+	rc = __condvar_wait_spinlock(cv, &irq_lock->lock);
 
 	irq_lock->guard = guard;
