3f63db8d00bac93e4f4b9d14fa0727d7cc6aa20f
[pintos-anon] / src / tests / threads / priority-donate-lower.c
1 /* The main thread acquires a lock.  Then it creates a
2    higher-priority thread that blocks acquiring the lock, causing
3    it to donate their priorities to the main thread.  The main
4    thread attempts to lower its priority, which should not take
5    effect until the donation is released. */
6
7 #include <stdio.h>
8 #include "tests/threads/tests.h"
9 #include "threads/init.h"
10 #include "threads/synch.h"
11 #include "threads/thread.h"
12
13 static thread_func acquire_thread_func;
14
15 void
16 test_priority_donate_lower (void) 
17 {
18   struct lock lock;
19
20   /* This test does not work with the MLFQS. */
21   ASSERT (!enable_mlfqs);
22
23   /* Make sure our priority is the default. */
24   ASSERT (thread_get_priority () == PRI_DEFAULT);
25
26   lock_init (&lock);
27   lock_acquire (&lock);
28   thread_create ("acquire", PRI_DEFAULT + 10, acquire_thread_func, &lock);
29   msg ("Main thread should have priority %d.  Actual priority: %d.",
30        PRI_DEFAULT + 10, thread_get_priority ());
31
32   msg ("Lowering base priority...");
33   thread_set_priority (PRI_DEFAULT - 10);
34   msg ("Main thread should have priority %d.  Actual priority: %d.",
35        PRI_DEFAULT + 10, thread_get_priority ());
36   lock_release (&lock);
37   msg ("acquire must already have finished.");
38   msg ("Main thread should have priority %d.  Actual priority: %d.",
39        PRI_DEFAULT - 10, thread_get_priority ());
40 }
41
42 static void
43 acquire_thread_func (void *lock_) 
44 {
45   struct lock *lock = lock_;
46
47   lock_acquire (lock);
48   msg ("acquire: got the lock");
49   lock_release (lock);
50   msg ("acquire: done");
51 }