5a30bd47e298ead7bf1fdb3143a9b23547d76ec5
[pintos-anon] / src / tests / threads / priority-donate-one.c
1 /* Problem 1-3: Priority Scheduling tests.
2
3    Based on a test originally submitted for Stanford's CS 140 in
4    winter 1999 by Matt Franklin <startled@leland.stanford.edu>,
5    Greg Hutchins <gmh@leland.stanford.edu>, Yu Ping Hu
6    <yph@cs.stanford.edu>.  Modified by arens. */
7
8 #include <stdio.h>
9 #include "tests/threads/tests.h"
10 #include "threads/init.h"
11 #include "threads/synch.h"
12 #include "threads/thread.h"
13
14 static thread_func acquire1_thread_func;
15 static thread_func acquire2_thread_func;
16
17 void
18 test_priority_donate_one (void) 
19 {
20   struct lock lock;
21
22   /* This test does not work with the MLFQS. */
23   ASSERT (!enable_mlfqs);
24
25   /* Make sure our priority is the default. */
26   ASSERT (thread_get_priority () == PRI_DEFAULT);
27
28   lock_init (&lock);
29   lock_acquire (&lock);
30   thread_create ("acquire1", PRI_DEFAULT - 1, acquire1_thread_func, &lock);
31   msg ("This thread should have priority %d.  Actual priority: %d.",
32        PRI_DEFAULT - 1, thread_get_priority ());
33   thread_create ("acquire2", PRI_DEFAULT - 2, acquire2_thread_func, &lock);
34   msg ("This thread should have priority %d.  Actual priority: %d.",
35        PRI_DEFAULT - 2, thread_get_priority ());
36   lock_release (&lock);
37   msg ("acquire2, acquire1 must already have finished, in that order.");
38   msg ("This should be the last line before finishing this test.");
39 }
40
41 static void
42 acquire1_thread_func (void *lock_) 
43 {
44   struct lock *lock = lock_;
45
46   lock_acquire (lock);
47   msg ("acquire1: got the lock");
48   lock_release (lock);
49   msg ("acquire1: done");
50 }
51
52 static void
53 acquire2_thread_func (void *lock_) 
54 {
55   struct lock *lock = lock_;
56
57   lock_acquire (lock);
58   msg ("acquire2: got the lock");
59   lock_release (lock);
60   msg ("acquire2: done");
61 }