1 /* Low priority thread L acquires a lock, then blocks downing a
2 semaphore. Medium priority thread M then blocks waiting on
3 the same semaphore. Next, high priority thread H attempts to
4 acquire the lock, donating its priority to L.
6 Next, the main thread ups the semaphore, waking up L. L
7 releases the lock, which wakes up H. H "up"s the semaphore,
8 waking up M. H terminates, then M, then L, and finally the
11 Written by Godmar Back <gback@cs.vt.edu>. */
14 #include "tests/threads/tests.h"
15 #include "threads/init.h"
16 #include "threads/synch.h"
17 #include "threads/thread.h"
22 struct semaphore sema;
25 static thread_func l_thread_func;
26 static thread_func m_thread_func;
27 static thread_func h_thread_func;
30 test_priority_donate_sema (void)
32 struct lock_and_sema ls;
34 /* This test does not work with the MLFQS. */
35 ASSERT (!enable_mlfqs);
37 /* Make sure our priority is the default. */
38 ASSERT (thread_get_priority () == PRI_DEFAULT);
41 sema_init (&ls.sema, 0);
42 thread_create ("low", PRI_DEFAULT + 1, l_thread_func, &ls);
43 thread_create ("med", PRI_DEFAULT + 3, m_thread_func, &ls);
44 thread_create ("high", PRI_DEFAULT + 5, h_thread_func, &ls);
46 msg ("Main thread finished.");
50 l_thread_func (void *ls_)
52 struct lock_and_sema *ls = ls_;
54 lock_acquire (&ls->lock);
55 msg ("Thread L acquired lock.");
56 sema_down (&ls->sema);
57 msg ("Thread L downed semaphore.");
58 lock_release (&ls->lock);
59 msg ("Thread L finished.");
63 m_thread_func (void *ls_)
65 struct lock_and_sema *ls = ls_;
67 sema_down (&ls->sema);
68 msg ("Thread M finished.");
72 h_thread_func (void *ls_)
74 struct lock_and_sema *ls = ls_;
76 lock_acquire (&ls->lock);
77 msg ("Thread H acquired lock.");
80 lock_release (&ls->lock);
81 msg ("Thread H finished.");