Add more tests.
[pintos-anon] / grading / threads / priority-donate-one.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 by Matt Franklin
5    <startled@leland.stanford.edu>, Greg Hutchins
6    <gmh@leland.stanford.edu>, Yu Ping Hu <yph@cs.stanford.edu>.
7    Modified by arens. */
8
9 #ifdef MLFQS
10 #error This test not applicable with MLFQS enabled.
11 #endif
12
13 #include "threads/test.h"
14 #include <stdio.h>
15 #include "threads/synch.h"
16 #include "threads/thread.h"
17
18 static void test_donate_return (void);
19
20 void
21 test (void) 
22 {
23   /* Make sure our priority is the default. */
24   ASSERT (thread_get_priority () == PRI_DEFAULT);
25
26   test_donate_return ();
27 }
28 \f
29 static thread_func acquire_thread_func;
30
31 static void
32 test_donate_return (void) 
33 {
34   struct lock lock;
35
36   printf ("\n"
37           "Testing priority donation.\n"
38           "If the statements printed below are all true, you pass.\n");
39
40   lock_init (&lock, "donor");
41   lock_acquire (&lock);
42   thread_create ("acquire1", PRI_DEFAULT + 1, acquire_thread_func, &lock);
43   printf ("This thread should have priority %d.  Actual priority: %d.\n",
44           PRI_DEFAULT + 1, thread_get_priority ());
45   thread_create ("acquire2", PRI_DEFAULT + 2, acquire_thread_func, &lock);
46   printf ("This thread should have priority %d.  Actual priority: %d.\n",
47           PRI_DEFAULT + 2, thread_get_priority ());
48   lock_release (&lock);
49   printf ("acquire2 and acquire1 must already have finished, in that order.\n"
50           "This should be the last line before finishing this test.\n"
51           "Priority donation test done.\n");
52 }
53
54 static void
55 acquire_thread_func (void *lock_) 
56 {
57   struct lock *lock = lock_;
58
59   lock_acquire (lock);
60   printf ("%s: got the lock\n", thread_name ());
61   lock_release (lock);
62   printf ("%s: done\n", thread_name ());
63 }