X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fdevices%2Ftimer.c;fp=src%2Fdevices%2Ftimer.c;h=951c2a6d0b17d5ce375f0a7246c5da2714a92dbf;hb=6a00cbfd8df6aa6a0e398b4566db888a6023e7e6;hp=1706201f4ea8590b54a9bc58792667128bb23cf7;hpb=c2fdbe98af66ad0870d2e5466f539d655a8eeff7;p=pintos-anon diff --git a/src/devices/timer.c b/src/devices/timer.c index 1706201..951c2a6 100644 --- a/src/devices/timer.c +++ b/src/devices/timer.c @@ -1,11 +1,19 @@ #include "devices/timer.h" #include +#include #include "threads/interrupt.h" #include "threads/io.h" #if TIMER_FREQ < 19 #error 8254 timer requires TIMER_FREQ >= 19 #endif +#if TIMER_FREQ > 1000 +#error TIMER_FREQ <= 1000 recommended +#endif + +/* Number of timer ticks that a process gets before being + preempted. */ +#define TIME_SLICE 1 /* Number of timer ticks since OS booted. */ static volatile int64_t ticks; @@ -51,7 +59,7 @@ timer_elapsed (int64_t then) void timer_msleep (int64_t ms) { - int64_t ticks = (int64_t) ms * TIMER_FREQ / 1000; + int64_t ticks = (int64_t) DIV_ROUND_UP (ms * TIMER_FREQ, 1000); int64_t start = timer_ticks (); while (timer_elapsed (start) < ticks) @@ -79,5 +87,6 @@ static void timer_interrupt (struct intr_frame *args UNUSED) { ticks++; - intr_yield_on_return (); + if (ticks % TIME_SLICE == 0) + intr_yield_on_return (); }