From: Ben Pfaff Date: Sun, 29 Aug 2004 19:13:08 +0000 (+0000) Subject: Add timer_msleep, timer_usleep, timer_nsleep(). X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?p=pintos-anon;a=commitdiff_plain;h=fd24baa6eaa3c98cece07189ed34a0943e03d520 Add timer_msleep, timer_usleep, timer_nsleep(). --- 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); } diff --git a/src/devices/timer.h b/src/devices/timer.h index 4be2f48..33fdaec 100644 --- a/src/devices/timer.h +++ b/src/devices/timer.h @@ -6,9 +6,11 @@ #define TIMER_FREQ 100 void timer_init (void); -uint64_t timer_ticks (void); -uint64_t timer_elapsed (uint64_t); +int64_t timer_ticks (void); +int64_t timer_elapsed (int64_t); -void timer_wait_until (uint64_t); +void timer_msleep (int64_t ms); +void timer_usleep (int64_t us); +void timer_nsleep (int64_t ns); #endif /* timer.h */