719b95727c692abb86f831a9fcc92048e335a643
[pintos-anon] / src / tests / threads / priority-donate-multiple.c
1 /* Problem 1-3: Priority Scheduling tests.
2
3    Based on a test originally submitted for Stanford's CS 140 in
4    winter 1999 by Matt Franklin <startled@leland.stanford.edu>,
5    Greg Hutchins <gmh@leland.stanford.edu>, Yu Ping Hu
6    <yph@cs.stanford.edu>.  Modified by arens. */
7
8 #include <stdio.h>
9 #include "tests/threads/tests.h"
10 #include "threads/init.h"
11 #include "threads/synch.h"
12 #include "threads/thread.h"
13
14 static thread_func a_thread_func;
15 static thread_func b_thread_func;
16
17 void
18 test_priority_donate_multiple (void) 
19 {
20   struct lock a, b;
21
22   /* This test does not work with the MLFQS. */
23   ASSERT (!enable_mlfqs);
24
25   /* Make sure our priority is the default. */
26   ASSERT (thread_get_priority () == PRI_DEFAULT);
27
28   lock_init (&a);
29   lock_init (&b);
30
31   lock_acquire (&a);
32   lock_acquire (&b);
33
34   thread_create ("a", PRI_DEFAULT - 1, a_thread_func, &a);
35   msg ("Main thread should have priority %d.  Actual priority: %d.",
36        PRI_DEFAULT - 1, thread_get_priority ());
37
38   thread_create ("b", PRI_DEFAULT - 2, b_thread_func, &b);
39   msg ("Main thread should have priority %d.  Actual priority: %d.",
40        PRI_DEFAULT - 2, thread_get_priority ());
41
42   lock_release (&b);
43   msg ("Thread b should have just finished.");
44   msg ("Main thread should have priority %d.  Actual priority: %d.",
45        PRI_DEFAULT - 1, thread_get_priority ());
46
47   lock_release (&a);
48   msg ("Thread a should have just finished.");
49   msg ("Main thread should have priority %d.  Actual priority: %d.",
50        PRI_DEFAULT, thread_get_priority ());
51 }
52
53 static void
54 a_thread_func (void *lock_) 
55 {
56   struct lock *lock = lock_;
57
58   lock_acquire (lock);
59   msg ("Thread a acquired lock a.");
60   lock_release (lock);
61   msg ("Thread a finished.");
62 }
63
64 static void
65 b_thread_func (void *lock_) 
66 {
67   struct lock *lock = lock_;
68
69   lock_acquire (lock);
70   msg ("Thread b acquired lock b.");
71   lock_release (lock);
72   msg ("Thread b finished.");
73 }