2 #include "tests/threads/tests.h"
3 #include "threads/init.h"
4 #include "threads/malloc.h"
5 #include "threads/synch.h"
6 #include "threads/thread.h"
7 #include "devices/timer.h"
9 static thread_func alarm_priority_thread;
10 static int64_t wake_time;
11 static struct semaphore wait_sema;
14 test_alarm_priority (void)
18 /* This test does not work with the MLFQS. */
19 ASSERT (!enable_mlfqs);
21 wake_time = timer_ticks () + 5 * TIMER_FREQ;
22 sema_init (&wait_sema, 0);
24 for (i = 0; i < 10; i++)
26 int priority = (i + 5) % 10 + PRI_DEFAULT + 1;
28 snprintf (name, sizeof name, "priority %d", priority);
29 thread_create (name, priority, alarm_priority_thread, NULL);
32 thread_set_priority (PRI_MAX);
34 for (i = 0; i < 10; i++)
35 sema_down (&wait_sema);
39 alarm_priority_thread (void *aux UNUSED)
41 /* Busy-wait until the current time changes. */
43 while (timer_elapsed (start_time) == 0)
46 /* Now we know we're at the very beginning of a timer tick, so
47 we can call timer_sleep() without worrying about races
48 between checking the time and a timer interrupt. */
49 timer_sleep (wake_time - timer_ticks ());
51 /* Print a message on wake-up. */
52 msg ("Thread %s woke up.", thread_name ());