Fix two bugs in the base Pintos code:
[pintos-anon] / src / tests / threads / alarm-priority.c
1 /* Checks that when the alarm clock wakes up threads, the
2    higher-priority threads run first. */
3
4 #include <stdio.h>
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"
11
12 static thread_func alarm_priority_thread;
13 static int64_t wake_time;
14 static struct semaphore wait_sema;
15
16 void
17 test_alarm_priority (void) 
18 {
19   int i;
20   
21   /* This test does not work with the MLFQS. */
22   ASSERT (!thread_mlfqs);
23
24   wake_time = timer_ticks () + 5 * TIMER_FREQ;
25   sema_init (&wait_sema, 0);
26   
27   for (i = 0; i < 10; i++) 
28     {
29       int priority = PRI_DEFAULT - (i + 5) % 10 - 1;
30       char name[16];
31       snprintf (name, sizeof name, "priority %d", priority);
32       thread_create (name, priority, alarm_priority_thread, NULL);
33     }
34
35   thread_set_priority (PRI_MIN);
36
37   for (i = 0; i < 10; i++)
38     sema_down (&wait_sema);
39 }
40
41 static void
42 alarm_priority_thread (void *aux UNUSED) 
43 {
44   /* Busy-wait until the current time changes. */
45   int64_t start_time = timer_ticks ();
46   while (timer_elapsed (start_time) == 0)
47     continue;
48
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 ());
53
54   /* Print a message on wake-up. */
55   msg ("Thread %s woke up.", thread_name ());
56
57   sema_up (&wait_sema);
58 }