From 7fd56df9056d6442d52f23d3fb356010b988668d Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Thu, 7 Apr 2005 18:00:06 +0000 Subject: [PATCH] In idle(), "sti; hlt" must be atomic or we can waste time waiting for the next clock tick. Fix inspired by Linus Torvalds in message . --- src/threads/thread.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/threads/thread.c b/src/threads/thread.c index 6eda280..02f3c7b 100644 --- a/src/threads/thread.c +++ b/src/threads/thread.c @@ -299,11 +299,19 @@ idle (void *aux UNUSED) /* Let someone else run. */ intr_disable (); thread_block (); - intr_enable (); - /* Use CPU `hlt' instruction to wait for interrupt. - See [IA32-v2a] "HLT" and [IA32-v3] 7.7. */ - asm ("hlt"); + /* Re-enable interrupts and wait for the next one. + + The `sti' instruction disables interrupts until the + completion of the next instruction, so these two + instructions are executed atomically. This atomicity is + important; otherwise, an interrupt could be handled + between re-enabling interrupts and waiting for the next + one to occur, wasting as much as one clock tick worth of + time. + + See [IA32-v2a] "HLT", [IA32-v2b] "STI", and [IA32-v3] 7.7. */ + asm ("sti; hlt"); } } -- 2.30.2