+ int64_t start = timer_ticks ();
+
+ while (timer_elapsed (start) < ticks)
+ if (intr_get_level () == INTR_ON)
+ thread_yield ();
+}
+
+/* Returns MS milliseconds in timer ticks, rounding up. */
+int64_t
+timer_ms2ticks (int64_t ms)
+{
+ /* MS / 1000 s
+ ------------------------ = MS * TIMER_FREQ / 1000 ticks.
+ (1 / TIMER_FREQ) ticks/s
+ */
+ return DIV_ROUND_UP (ms * TIMER_FREQ, 1000);
+}
+
+/* Returns US microseconds in timer ticks, rounding up. */
+int64_t
+timer_us2ticks (int64_t us)
+{
+ return DIV_ROUND_UP (us * TIMER_FREQ, 1000000);
+}
+
+/* Returns NS nanoseconds in timer ticks, rounding up. */
+int64_t
+timer_ns2ticks (int64_t ns)
+{
+ return DIV_ROUND_UP (ns * TIMER_FREQ, 1000000000);
+}
+
+/* Prints timer statistics. */
+void
+timer_print_stats (void)
+{
+ printf ("Timer: %"PRId64" ticks\n", ticks);
+}
+\f
+/* Timer interrupt handler. */
+static void
+timer_interrupt (struct intr_frame *args UNUSED)
+{
+ ticks++;
+ thread_tick ();
+ if (ticks % TIME_SLICE == 0)
+ intr_yield_on_return ();