X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fdevices%2Ftimer.c;h=4271c7354a86b6bf153045cad2666c5e2855faa5;hb=bd1397c2e890c82bd58bb2e6eefa223172b3d632;hp=4b8dd2a47865b8828b9a6f326d4a7f03d970e222;hpb=44d0fa6a2b24a84e5eb0d54959ed91c1d4f15343;p=pintos-anon diff --git a/src/devices/timer.c b/src/devices/timer.c index 4b8dd2a..4271c73 100644 --- a/src/devices/timer.c +++ b/src/devices/timer.c @@ -7,7 +7,7 @@ #error 8254 timer requires TIMER_FREQ >= 19 #endif -static volatile uint64_t ticks; +static volatile int64_t ticks; static void irq20_timer (struct intr_frame *args UNUSED) @@ -30,28 +30,43 @@ timer_init (void) outb (0x40, count & 0xff); outb (0x40, count >> 8); - intr_register (0x20, 0, IF_OFF, irq20_timer, "8254 Timer"); + intr_register (0x20, 0, INTR_OFF, irq20_timer, "8254 Timer"); } -uint64_t +int64_t timer_ticks (void) { - enum if_level old_level = intr_disable (); - uint64_t t = ticks; + enum intr_level old_level = intr_disable (); + int64_t t = ticks; intr_set_level (old_level); return t; } -uint64_t -timer_elapsed (uint64_t then) +int64_t +timer_elapsed (int64_t then) { - uint64_t now = timer_ticks (); - return now > then ? now - then : ((uint64_t) -1 - then) + now + 1; + return timer_ticks () - then; } -/* Suspends execution for at least DURATION ticks. */ +/* Suspends execution for approximately DURATION milliseconds. */ void -timer_wait_until (uint64_t duration) +timer_msleep (int64_t ms) { - /* FIXME */ + int64_t ticks = (int64_t) ms * TIMER_FREQ / 1000; + int64_t start = timer_ticks (); + + while (timer_elapsed (start) < ticks) + continue; +} + +void +timer_usleep (int64_t us) +{ + timer_msleep (us / 1000 + 1); +} + +void +timer_nsleep (int64_t ns) +{ + timer_msleep (ns / 1000000 + 1); }