35f135c3d9ef6c372cd0af29f92aa2750a37705e
[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 acquire1_thread_func;
30 static thread_func acquire2_thread_func;
31
32 static void
33 test_donate_return (void) 
34 {
35   struct lock lock;
36
37   printf ("\n"
38           "Testing priority donation.\n"
39           "If the statements printed below are all true, you pass.\n");
40
41   lock_init (&lock, "donor");
42   lock_acquire (&lock);
43   thread_create ("acquire1", PRI_DEFAULT + 1, acquire1_thread_func, &lock);
44   printf ("This thread should have priority %d.  Actual priority: %d.\n",
45           PRI_DEFAULT + 1, thread_get_priority ());
46   thread_create ("acquire2", PRI_DEFAULT + 2, acquire2_thread_func, &lock);
47   printf ("This thread should have priority %d.  Actual priority: %d.\n",
48           PRI_DEFAULT + 2, thread_get_priority ());
49   lock_release (&lock);
50   printf ("acquire2, acquire1 must already have finished, in that order.\n"
51           "This should be the last line before finishing this test.\n"
52           "Priority donation test done.\n");
53 }
54
55 static void
56 acquire1_thread_func (void *lock_) 
57 {
58   struct lock *lock = lock_;
59
60   lock_acquire (lock);
61   printf ("acquire1: got the lock\n");
62   lock_release (lock);
63   printf ("acquire1: done\n");
64 }
65
66 static void
67 acquire2_thread_func (void *lock_) 
68 {
69   struct lock *lock = lock_;
70
71   lock_acquire (lock);
72   printf ("acquire2: got the lock\n");
73   lock_release (lock);
74   printf ("acquire2: done\n");
75 }