Make tests public. Rewrite most tests. Add tests.
[pintos-anon] / src / tests / threads / alarm-priority.c
1 #include <stdio.h>
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"
8
9 static thread_func alarm_priority_thread;
10 static int64_t wake_time;
11 static struct semaphore wait_sema;
12
13 void
14 test_alarm_priority (void) 
15 {
16   int i;
17   
18   /* This test does not work with the MLFQS. */
19   ASSERT (!enable_mlfqs);
20
21   wake_time = timer_ticks () + 5 * TIMER_FREQ;
22   sema_init (&wait_sema, 0);
23   
24   for (i = 0; i < 10; i++) 
25     {
26       int priority = (i + 5) % 10 + PRI_DEFAULT + 1;
27       char name[16];
28       snprintf (name, sizeof name, "priority %d", priority);
29       thread_create (name, priority, alarm_priority_thread, NULL);
30     }
31
32   thread_set_priority (PRI_MAX);
33
34   for (i = 0; i < 10; i++)
35     sema_down (&wait_sema);
36 }
37
38 static void
39 alarm_priority_thread (void *aux UNUSED) 
40 {
41   /* Busy-wait until the current time changes. */
42   int64_t start_time;
43   while (timer_elapsed (start_time) == 0)
44     continue;
45
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 ());
50
51   /* Print a message on wake-up. */
52   msg ("Thread %s woke up.", thread_name ());
53
54   sema_up (&wait_sema);
55 }