summaryrefslogtreecommitdiffstats
path: root/tests/threads/priority-donate-multiple2.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/threads/priority-donate-multiple2.c')
-rw-r--r--tests/threads/priority-donate-multiple2.c90
1 files changed, 90 insertions, 0 deletions
diff --git a/tests/threads/priority-donate-multiple2.c b/tests/threads/priority-donate-multiple2.c
new file mode 100644
index 0000000..7f65fef
--- /dev/null
+++ b/tests/threads/priority-donate-multiple2.c
@@ -0,0 +1,90 @@
1/* The main thread acquires locks A and B, then it creates three
2 higher-priority threads. The first two of these threads block
3 acquiring one of the locks and thus donate their priority to
4 the main thread. The main thread releases the locks in turn
5 and relinquishes its donated priorities, allowing the third thread
6 to run.
7
8 In this test, the main thread releases the locks in a different
9 order compared to priority-donate-multiple.c.
10
11 Written by Godmar Back <gback@cs.vt.edu>.
12 Based on a test originally submitted for Stanford's CS 140 in
13 winter 1999 by Matt Franklin <startled@leland.stanford.edu>,
14 Greg Hutchins <gmh@leland.stanford.edu>, Yu Ping Hu
15 <yph@cs.stanford.edu>. Modified by arens. */
16
17#include <stdio.h>
18#include "tests/threads/tests.h"
19#include "threads/init.h"
20#include "threads/synch.h"
21#include "threads/thread.h"
22
23static thread_func a_thread_func;
24static thread_func b_thread_func;
25static thread_func c_thread_func;
26
27void
28test_priority_donate_multiple2 (void)
29{
30 struct lock a, b;
31
32 /* This test does not work with the MLFQS. */
33 ASSERT (!thread_mlfqs);
34
35 /* Make sure our priority is the default. */
36 ASSERT (thread_get_priority () == PRI_DEFAULT);
37
38 lock_init (&a);
39 lock_init (&b);
40
41 lock_acquire (&a);
42 lock_acquire (&b);
43
44 thread_create ("a", PRI_DEFAULT + 3, a_thread_func, &a);
45 msg ("Main thread should have priority %d. Actual priority: %d.",
46 PRI_DEFAULT + 3, thread_get_priority ());
47
48 thread_create ("c", PRI_DEFAULT + 1, c_thread_func, NULL);
49
50 thread_create ("b", PRI_DEFAULT + 5, b_thread_func, &b);
51 msg ("Main thread should have priority %d. Actual priority: %d.",
52 PRI_DEFAULT + 5, thread_get_priority ());
53
54 lock_release (&a);
55 msg ("Main thread should have priority %d. Actual priority: %d.",
56 PRI_DEFAULT + 5, thread_get_priority ());
57
58 lock_release (&b);
59 msg ("Threads b, a, c should have just finished, in that order.");
60 msg ("Main thread should have priority %d. Actual priority: %d.",
61 PRI_DEFAULT, thread_get_priority ());
62}
63
64static void
65a_thread_func (void *lock_)
66{
67 struct lock *lock = lock_;
68
69 lock_acquire (lock);
70 msg ("Thread a acquired lock a.");
71 lock_release (lock);
72 msg ("Thread a finished.");
73}
74
75static void
76b_thread_func (void *lock_)
77{
78 struct lock *lock = lock_;
79
80 lock_acquire (lock);
81 msg ("Thread b acquired lock b.");
82 lock_release (lock);
83 msg ("Thread b finished.");
84}
85
86static void
87c_thread_func (void *a_ UNUSED)
88{
89 msg ("Thread c finished.");
90}