CS318 - Pintos
Pintos source browser for JHU CS318 course
priority-condvar.c
Go to the documentation of this file.
1 /** Tests that cond_signal() wakes up the highest-priority thread
2  waiting in cond_wait(). */
3 
4 #include <stdio.h>
5 #include "tests/threads/tests.h"
6 #include "threads/init.h"
7 #include "threads/malloc.h"
8 #include "threads/synch.h"
9 #include "threads/thread.h"
10 #include "devices/timer.h"
11 
13 static struct lock lock;
14 static struct condition condition;
15 
16 void
18 {
19  int i;
20 
21  /* This test does not work with the MLFQS. */
23 
24  lock_init (&lock);
26 
28  for (i = 0; i < 10; i++)
29  {
30  int priority = PRI_DEFAULT - (i + 7) % 10 - 1;
31  char name[16];
32  snprintf (name, sizeof name, "priority %d", priority);
34  }
35 
36  for (i = 0; i < 10; i++)
37  {
38  lock_acquire (&lock);
39  msg ("Signaling...");
41  lock_release (&lock);
42  }
43 }
44 
45 static void
47 {
48  msg ("Thread %s starting.", thread_name ());
49  lock_acquire (&lock);
51  msg ("Thread %s woke up.", thread_name ());
52  lock_release (&lock);
53 }
name
char * name[]
Definition: insult.c:47
snprintf
int snprintf(char *buffer, size_t buf_size, const char *format,...)
Like printf(), except that output is stored into BUFFER, which must have space for BUF_SIZE character...
Definition: stdio.c:62
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
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
thread_name
const char * thread_name(void)
Returns the name of the running thread.
Definition: thread.c:247
cond_init
void cond_init(struct condition *cond)
Initializes condition variable COND.
Definition: synch.c:260
priority_condvar_thread
static thread_func priority_condvar_thread
Tests that cond_signal() wakes up the highest-priority thread waiting in cond_wait().
Definition: priority-condvar.c:12
init.h
timer.h
malloc.h
cond_wait
void cond_wait(struct condition *cond, struct lock *lock)
Atomically releases LOCK and waits for COND to be signaled by some other piece of code.
Definition: synch.c:288
cond_signal
void cond_signal(struct condition *cond, struct lock *lock UNUSED)
If any threads are waiting on COND (protected by LOCK), then this function signals one of them to wak...
Definition: synch.c:312
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
PRI_MIN
#define PRI_MIN
Thread priorities.
Definition: thread.h:23
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
thread_set_priority
void thread_set_priority(int new_priority)
Sets the current thread's priority to NEW_PRIORITY.
Definition: thread.c:336
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
test_priority_condvar
void test_priority_condvar(void)
Definition: priority-condvar.c:17
condition
Condition variable.
Definition: synch.h:34
PRI_DEFAULT
#define PRI_DEFAULT
Default priority.
Definition: thread.h:24