CS318 - Pintos
Pintos source browser for JHU CS318 course
priority-donate-multiple.c
Go to the documentation of this file.
1 /** The main thread acquires locks A and B, then it creates two
2  higher-priority threads. Each of these threads blocks
3  acquiring one of the locks and thus donate their priority to
4  the main thread. The main thread releases the locks in turn
5  and relinquishes its donated priorities.
6 
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. */
11 
12 #include <stdio.h>
13 #include "tests/threads/tests.h"
14 #include "threads/init.h"
15 #include "threads/synch.h"
16 #include "threads/thread.h"
17 
20 
21 void
23 {
24  struct lock a, b;
25 
26  /* This test does not work with the MLFQS. */
28 
29  /* Make sure our priority is the default. */
31 
32  lock_init (&a);
33  lock_init (&b);
34 
35  lock_acquire (&a);
36  lock_acquire (&b);
37 
38  thread_create ("a", PRI_DEFAULT + 1, a_thread_func, &a);
39  msg ("Main thread should have priority %d. Actual priority: %d.",
41 
42  thread_create ("b", PRI_DEFAULT + 2, b_thread_func, &b);
43  msg ("Main thread should have priority %d. Actual priority: %d.",
45 
46  lock_release (&b);
47  msg ("Thread b should have just finished.");
48  msg ("Main thread should have priority %d. Actual priority: %d.",
50 
51  lock_release (&a);
52  msg ("Thread a should have just finished.");
53  msg ("Main thread should have priority %d. Actual priority: %d.",
55 }
56 
57 static void
58 a_thread_func (void *lock_)
59 {
60  struct lock *lock = lock_;
61 
63  msg ("Thread a acquired lock a.");
65  msg ("Thread a finished.");
66 }
67 
68 static void
69 b_thread_func (void *lock_)
70 {
71  struct lock *lock = lock_;
72 
74  msg ("Thread b acquired lock b.");
76  msg ("Thread b finished.");
77 }
lock_release
void lock_release(struct lock *lock)
Releases LOCK, which must be owned by the current thread.
Definition: synch.c:229
a_thread_func
static thread_func a_thread_func
The main thread acquires locks A and B, then it creates two higher-priority threads.
Definition: priority-donate-multiple.c:18
lock_init
void lock_init(struct lock *lock)
Initializes LOCK.
Definition: synch.c:176
b_thread_func
static thread_func b_thread_func
Definition: priority-donate-multiple.c:19
thread_get_priority
int thread_get_priority(void)
Returns the current thread's priority.
Definition: thread.c:343
init.h
test_priority_donate_multiple
void test_priority_donate_multiple(void)
Definition: priority-donate-multiple.c:22
ASSERT
#define ASSERT(CONDITION)
This is outside the header guard so that debug.h may be included multiple times with different settin...
Definition: debug.h:31
msg
void msg(const char *format,...)
Definition: lib.c:28
thread_mlfqs
bool thread_mlfqs
If false (default), use round-robin scheduler.
Definition: thread.c:60
thread_create
tid_t thread_create(const char *name, int priority, thread_func *function, void *aux)
Creates a new kernel thread named NAME with the given initial PRIORITY, which executes FUNCTION passi...
Definition: thread.c:166
tests.h
thread_func
void thread_func(void *aux)
Definition: thread.h:116
lock_acquire
void lock_acquire(struct lock *lock)
Acquires LOCK, sleeping until it becomes available if necessary.
Definition: synch.c:193
thread.h
synch.h
lock
Lock.
Definition: synch.h:21
PRI_DEFAULT
#define PRI_DEFAULT
Default priority.
Definition: thread.h:24