CS318 - Pintos
Pintos source browser for JHU CS318 course
priority-donate-sema.c
Go to the documentation of this file.
1 /** Low priority thread L acquires a lock, then blocks downing a
2  semaphore. Medium priority thread M then blocks waiting on
3  the same semaphore. Next, high priority thread H attempts to
4  acquire the lock, donating its priority to L.
5 
6  Next, the main thread ups the semaphore, waking up L. L
7  releases the lock, which wakes up H. H "up"s the semaphore,
8  waking up M. H terminates, then M, then L, and finally the
9  main thread.
10 
11  Written by Godmar Back <gback@cs.vt.edu>. */
12 
13 #include <stdio.h>
14 #include "tests/threads/tests.h"
15 #include "threads/init.h"
16 #include "threads/synch.h"
17 #include "threads/thread.h"
18 
20  {
21  struct lock lock;
22  struct semaphore sema;
23  };
24 
28 
29 void
31 {
32  struct lock_and_sema ls;
33 
34  /* This test does not work with the MLFQS. */
36 
37  /* Make sure our priority is the default. */
39 
40  lock_init (&ls.lock);
41  sema_init (&ls.sema, 0);
42  thread_create ("low", PRI_DEFAULT + 1, l_thread_func, &ls);
43  thread_create ("med", PRI_DEFAULT + 3, m_thread_func, &ls);
44  thread_create ("high", PRI_DEFAULT + 5, h_thread_func, &ls);
45  sema_up (&ls.sema);
46  msg ("Main thread finished.");
47 }
48 
49 static void
50 l_thread_func (void *ls_)
51 {
52  struct lock_and_sema *ls = ls_;
53 
54  lock_acquire (&ls->lock);
55  msg ("Thread L acquired lock.");
56  sema_down (&ls->sema);
57  msg ("Thread L downed semaphore.");
58  lock_release (&ls->lock);
59  msg ("Thread L finished.");
60 }
61 
62 static void
63 m_thread_func (void *ls_)
64 {
65  struct lock_and_sema *ls = ls_;
66 
67  sema_down (&ls->sema);
68  msg ("Thread M finished.");
69 }
70 
71 static void
72 h_thread_func (void *ls_)
73 {
74  struct lock_and_sema *ls = ls_;
75 
76  lock_acquire (&ls->lock);
77  msg ("Thread H acquired lock.");
78 
79  sema_up (&ls->sema);
80  lock_release (&ls->lock);
81  msg ("Thread H finished.");
82 }
lock_release
void lock_release(struct lock *lock)
Releases LOCK, which must be owned by the current thread.
Definition: synch.c:229
lock_init
void lock_init(struct lock *lock)
Initializes LOCK.
Definition: synch.c:176
semaphore
A counting semaphore.
Definition: synch.h:8
lock_and_sema::sema
struct semaphore sema
Definition: priority-donate-sema.c:22
lock_and_sema
Low priority thread L acquires a lock, then blocks downing a semaphore.
Definition: priority-donate-sema.c:19
m_thread_func
static thread_func m_thread_func
Definition: priority-donate-sema.c:26
sema_up
void sema_up(struct semaphore *sema)
Up or "V" operation on a semaphore.
Definition: synch.c:109
thread_get_priority
int thread_get_priority(void)
Returns the current thread's priority.
Definition: thread.c:343
init.h
sema_down
void sema_down(struct semaphore *sema)
Down or "P" operation on a semaphore.
Definition: synch.c:61
h_thread_func
static thread_func h_thread_func
Definition: priority-donate-sema.c:27
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
sema_init
void sema_init(struct semaphore *sema, unsigned value)
This file is derived from source code for the Nachos instructional operating system.
Definition: synch.c:45
lock_and_sema::lock
struct lock lock
Definition: priority-donate-sema.c:21
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
l_thread_func
static thread_func l_thread_func
Definition: priority-donate-sema.c:25
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
test_priority_donate_sema
void test_priority_donate_sema(void)
Definition: priority-donate-sema.c:30
PRI_DEFAULT
#define PRI_DEFAULT
Default priority.
Definition: thread.h:24