Fix two bugs in the base Pintos code:
[pintos-anon] / src / tests / threads / priority-sema.c
1 /* Tests that the highest-priority thread waiting on a semaphore
2    is the first to wake up. */
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 priority_sema_thread;
13 static struct semaphore sema;
14
15 void
16 test_priority_sema (void) 
17 {
18   int i;
19   
20   /* This test does not work with the MLFQS. */
21   ASSERT (!thread_mlfqs);
22
23   sema_init (&sema, 0);
24   thread_set_priority (PRI_MIN);
25   for (i = 0; i < 10; i++) 
26     {
27       int priority = PRI_DEFAULT - (i + 3) % 10 - 1;
28       char name[16];
29       snprintf (name, sizeof name, "priority %d", priority);
30       thread_create (name, priority, priority_sema_thread, NULL);
31     }
32
33   for (i = 0; i < 10; i++) 
34     {
35       sema_up (&sema);
36       msg ("Back in main thread."); 
37     }
38 }
39
40 static void
41 priority_sema_thread (void *aux UNUSED) 
42 {
43   sema_down (&sema);
44   msg ("Thread %s woke up.", thread_name ());
45 }