1 /* Checks that when the alarm clock wakes up threads, the
2 higher-priority threads run first. */
5 #include "tests/threads/tests.h"
6 #include "threads/init.h"
7 #include "threads/malloc.h"
8 #include "threads/synch.h"
9 #include "threads/thread.h"
10 #include "devices/timer.h"
12 static thread_func alarm_priority_thread;
13 static int64_t wake_time;
14 static struct semaphore wait_sema;
17 test_alarm_priority (void)
21 /* This test does not work with the MLFQS. */
22 ASSERT (!thread_mlfqs);
24 wake_time = timer_ticks () + 5 * TIMER_FREQ;
25 sema_init (&wait_sema, 0);
27 for (i = 0; i < 10; i++)
29 int priority = PRI_DEFAULT - (i + 5) % 10 - 1;
31 snprintf (name, sizeof name, "priority %d", priority);
32 thread_create (name, priority, alarm_priority_thread, NULL);
35 thread_set_priority (PRI_MIN);
37 for (i = 0; i < 10; i++)
38 sema_down (&wait_sema);
42 alarm_priority_thread (void *aux UNUSED)
44 /* Busy-wait until the current time changes. */
45 int64_t start_time = timer_ticks ();
46 while (timer_elapsed (start_time) == 0)
49 /* Now we know we're at the very beginning of a timer tick, so
50 we can call timer_sleep() without worrying about races
51 between checking the time and a timer interrupt. */
52 timer_sleep (wake_time - timer_ticks ());
54 /* Print a message on wake-up. */
55 msg ("Thread %s woke up.", thread_name ());