1 /* Low-priority main thread L acquires lock A. Medium-priority
2 thread M then acquires lock B then blocks on acquiring lock A.
3 High-priority thread H then blocks on acquiring lock B. Thus,
4 thread H donates its priority to M, which in turn donates it
7 Based on a test originally submitted for Stanford's CS 140 in
8 winter 1999 by Matt Franklin <startled@leland.stanford.edu>,
9 Greg Hutchins <gmh@leland.stanford.edu>, Yu Ping Hu
10 <yph@cs.stanford.edu>. Modified by arens. */
13 #include "tests/threads/tests.h"
14 #include "threads/init.h"
15 #include "threads/synch.h"
16 #include "threads/thread.h"
24 static thread_func medium_thread_func;
25 static thread_func high_thread_func;
28 test_priority_donate_nest (void)
33 /* This test does not work with the MLFQS. */
34 ASSERT (!enable_mlfqs);
36 /* Make sure our priority is the default. */
37 ASSERT (thread_get_priority () == PRI_DEFAULT);
46 thread_create ("medium", PRI_DEFAULT + 1, medium_thread_func, &locks);
48 msg ("Low thread should have priority %d. Actual priority: %d.",
49 PRI_DEFAULT + 1, thread_get_priority ());
51 thread_create ("high", PRI_DEFAULT + 2, high_thread_func, &b);
53 msg ("Low thread should have priority %d. Actual priority: %d.",
54 PRI_DEFAULT + 2, thread_get_priority ());
58 msg ("Medium thread should just have finished.");
59 msg ("Low thread should have priority %d. Actual priority: %d.",
60 PRI_DEFAULT, thread_get_priority ());
64 medium_thread_func (void *locks_)
66 struct locks *locks = locks_;
68 lock_acquire (locks->b);
69 lock_acquire (locks->a);
71 msg ("Medium thread should have priority %d. Actual priority: %d.",
72 PRI_DEFAULT + 2, thread_get_priority ());
73 msg ("Medium thread got the lock.");
75 lock_release (locks->a);
78 lock_release (locks->b);
81 msg ("High thread should have just finished.");
82 msg ("Middle thread finished.");
86 high_thread_func (void *lock_)
88 struct lock *lock = lock_;
91 msg ("High thread got the lock.");
93 msg ("High thread finished.");