CS318 - Pintos
Pintos source browser for JHU CS318 course
priority-donate-multiple2.c
Go to the documentation of this file.
1 /** The main thread acquires locks A and B, then it creates three
2  higher-priority threads. The first two of these threads block
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, allowing the third thread
6  to run.
7 
8  In this test, the main thread releases the locks in a different
9  order compared to priority-donate-multiple.c.
10 
11  Written by Godmar Back <gback@cs.vt.edu>.
12  Based on a test originally submitted for Stanford's CS 140 in
13  winter 1999 by Matt Franklin <startled@leland.stanford.edu>,
14  Greg Hutchins <gmh@leland.stanford.edu>, Yu Ping Hu
15  <yph@cs.stanford.edu>. Modified by arens. */
16 
17 #include <stdio.h>
18 #include "tests/threads/tests.h"
19 #include "threads/init.h"
20 #include "threads/synch.h"
21 #include "threads/thread.h"
22 
26 
27 void
29 {
30  struct lock a, b;
31 
32  /* This test does not work with the MLFQS. */
34 
35  /* Make sure our priority is the default. */
37 
38  lock_init (&a);
39  lock_init (&b);
40 
41  lock_acquire (&a);
42  lock_acquire (&b);
43 
44  thread_create ("a", PRI_DEFAULT + 3, a_thread_func, &a);
45  msg ("Main thread should have priority %d. Actual priority: %d.",
47 
49 
50  thread_create ("b", PRI_DEFAULT + 5, b_thread_func, &b);
51  msg ("Main thread should have priority %d. Actual priority: %d.",
53 
54  lock_release (&a);
55  msg ("Main thread should have priority %d. Actual priority: %d.",
57 
58  lock_release (&b);
59  msg ("Threads b, a, c should have just finished, in that order.");
60  msg ("Main thread should have priority %d. Actual priority: %d.",
62 }
63 
64 static void
65 a_thread_func (void *lock_)
66 {
67  struct lock *lock = lock_;
68 
70  msg ("Thread a acquired lock a.");
72  msg ("Thread a finished.");
73 }
74 
75 static void
76 b_thread_func (void *lock_)
77 {
78  struct lock *lock = lock_;
79 
81  msg ("Thread b acquired lock b.");
83  msg ("Thread b finished.");
84 }
85 
86 static void
87 c_thread_func (void *a_ UNUSED)
88 {
89  msg ("Thread c finished.");
90 }
lock_release
void lock_release(struct lock *lock)
Releases LOCK, which must be owned by the current thread.
Definition: synch.c:229
NULL
#define NULL
Definition: stddef.h:4
b_thread_func
static thread_func b_thread_func
Definition: priority-donate-multiple2.c:24
lock_init
void lock_init(struct lock *lock)
Initializes LOCK.
Definition: synch.c:176
UNUSED
#define UNUSED
GCC lets us add "attributes" to functions, function parameters, etc.
Definition: debug.h:7
c_thread_func
static thread_func c_thread_func
Definition: priority-donate-multiple2.c:25
thread_get_priority
int thread_get_priority(void)
Returns the current thread's priority.
Definition: thread.c:343
init.h
test_priority_donate_multiple2
void test_priority_donate_multiple2(void)
Definition: priority-donate-multiple2.c:28
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
a_thread_func
static thread_func a_thread_func
The main thread acquires locks A and B, then it creates three higher-priority threads.
Definition: priority-donate-multiple2.c:23
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