+/* Down or "P" operation on a semaphore, but only if the
+ semaphore is not already 0. Returns true if the semaphore is
+ decremented, false otherwise.
+
+ This function may be called from an interrupt handler. */
+bool
+sema_try_down (struct semaphore *sema)
+{
+ enum intr_level old_level;
+ bool success;
+
+ ASSERT (sema != NULL);
+
+ old_level = intr_disable ();
+ if (sema->value > 0)
+ {
+ sema->value--;
+ success = true;
+ }
+ else
+ success = false;
+ intr_set_level (old_level);
+
+ return success;
+}
+