summaryrefslogtreecommitdiffstats
path: root/tests/threads/priority-donate-condvar.c
diff options
context:
space:
mode:
authormanuel <manuel@mausz.at>2012-05-11 15:10:44 +0200
committermanuel <manuel@mausz.at>2012-05-11 15:10:44 +0200
commitac1525683728d7b3ab644e729e6658d435914d85 (patch)
treeabc1feac44b687e376d98233dc00a8cd295c32c7 /tests/threads/priority-donate-condvar.c
parent29adc36b906ae3be5afc577450136e11c12bc0a0 (diff)
downloadprogos-ac1525683728d7b3ab644e729e6658d435914d85.tar.gz
progos-ac1525683728d7b3ab644e729e6658d435914d85.tar.bz2
progos-ac1525683728d7b3ab644e729e6658d435914d85.zip
add new test for priority donation with conditions
Diffstat (limited to 'tests/threads/priority-donate-condvar.c')
-rw-r--r--tests/threads/priority-donate-condvar.c97
1 files changed, 97 insertions, 0 deletions
diff --git a/tests/threads/priority-donate-condvar.c b/tests/threads/priority-donate-condvar.c
new file mode 100644
index 0000000..321a58a
--- /dev/null
+++ b/tests/threads/priority-donate-condvar.c
@@ -0,0 +1,97 @@
1/* Basically that's the same test as priority-donate-sema but using
2 conditions instead of semaphores.
3
4 Low priority thread L acquires a lock, then blocks waiting for
5 the condition. Medium priority thread M then blocks waiting for
6 the same condition. Next, high priority thread H attempts to
7 acquire the lock, donating its priority to L.
8
9 Next, the main thread signals the condition, waking up L. L
10 releases the lock, which wakes up H. H signals the condition,
11 waking up M. H terminates, then M, then L, and finally the
12 main thread.
13
14 Written by Manuel Mausz <manuel-tuwien@mausz.at>. */
15
16#include <stdio.h>
17#include "tests/threads/tests.h"
18#include "threads/init.h"
19#include "threads/synch.h"
20#include "threads/thread.h"
21
22struct lock_and_cond
23 {
24 struct lock lock;
25 struct lock lockc;
26 struct condition cond;
27 };
28
29static thread_func l_thread_func;
30static thread_func m_thread_func;
31static thread_func h_thread_func;
32
33void
34test_priority_donate_condvar (void)
35{
36 struct lock_and_cond ls;
37
38 /* This test does not work with the MLFQS. */
39 ASSERT (!thread_mlfqs);
40
41 /* Make sure our priority is the default. */
42 ASSERT (thread_get_priority () == PRI_DEFAULT);
43
44 lock_init (&ls.lock);
45 lock_init (&ls.lockc);
46 cond_init (&ls.cond);
47 thread_create ("low", PRI_DEFAULT + 1, l_thread_func, &ls);
48 thread_create ("med", PRI_DEFAULT + 3, m_thread_func, &ls);
49 thread_create ("high", PRI_DEFAULT + 5, h_thread_func, &ls);
50 lock_acquire (&ls.lockc);
51 msg ("Signaling...");
52 cond_signal (&ls.cond, &ls.lockc);
53 lock_release (&ls.lockc);
54 msg ("Main thread finished.");
55}
56
57static void
58l_thread_func (void *ls_)
59{
60 struct lock_and_cond *ls = ls_;
61
62 lock_acquire (&ls->lock);
63 msg ("Thread L acquired lock.");
64 lock_acquire (&ls->lockc);
65 cond_wait (&ls->cond, &ls->lockc);
66 lock_release (&ls->lockc);
67 msg ("Thread L woke up.");
68 lock_release (&ls->lock);
69 msg ("Thread L finished.");
70}
71
72static void
73m_thread_func (void *ls_)
74{
75 struct lock_and_cond *ls = ls_;
76
77 lock_acquire (&ls->lockc);
78 cond_wait (&ls->cond, &ls->lockc);
79 lock_release (&ls->lockc);
80 msg ("Thread M finished.");
81}
82
83static void
84h_thread_func (void *ls_)
85{
86 struct lock_and_cond *ls = ls_;
87
88 lock_acquire (&ls->lock);
89 msg ("Thread H acquired lock.");
90
91 lock_acquire (&ls->lockc);
92 msg ("Signaling...");
93 cond_signal (&ls->cond, &ls->lockc);
94 lock_release (&ls->lockc);
95 lock_release (&ls->lock);
96 msg ("Thread H finished.");
97}