X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fdevices%2Ftimer.c;h=df387367e72b3b531dc094ad585b263729c31807;hb=fd24baa6eaa3c98cece07189ed34a0943e03d520;hp=4b8dd2a47865b8828b9a6f326d4a7f03d970e222;hpb=5a3263611cb5b67450f3dc1306359de9f76e0d47;p=pintos-anon diff --git a/src/devices/timer.c b/src/devices/timer.c index 4b8dd2a..df38736 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) @@ -33,25 +33,40 @@ timer_init (void) intr_register (0x20, 0, IF_OFF, irq20_timer, "8254 Timer"); } -uint64_t +int64_t timer_ticks (void) { enum if_level old_level = intr_disable (); - uint64_t t = ticks; + 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); }