summaryrefslogtreecommitdiffstats
path: root/tests/threads/priority-donate-condvar.c
blob: 321a58a1e6ef8e5df0c6554c5d4a671cde839dd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* Basically that's the same test as priority-donate-sema but using
   conditions instead of semaphores.

   Low priority thread L acquires a lock, then blocks waiting for
   the condition.  Medium priority thread M then blocks waiting for
   the same condition.  Next, high priority thread H attempts to
   acquire the lock, donating its priority to L.

   Next, the main thread signals the condition, waking up L.  L
   releases the lock, which wakes up H.  H signals the condition,
   waking up M.  H terminates, then M, then L, and finally the
   main thread.

   Written by Manuel Mausz <manuel-tuwien@mausz.at>. */

#include <stdio.h>
#include "tests/threads/tests.h"
#include "threads/init.h"
#include "threads/synch.h"
#include "threads/thread.h"

struct lock_and_cond
  {
    struct lock lock;
    struct lock lockc;
    struct condition cond;
  };

static thread_func l_thread_func;
static thread_func m_thread_func;
static thread_func h_thread_func;

void
test_priority_donate_condvar (void)
{
  struct lock_and_cond ls;

  /* This test does not work with the MLFQS. */
  ASSERT (!thread_mlfqs);

  /* Make sure our priority is the default. */
  ASSERT (thread_get_priority () == PRI_DEFAULT);

  lock_init (&ls.lock);
  lock_init (&ls.lockc);
  cond_init (&ls.cond);
  thread_create ("low", PRI_DEFAULT + 1, l_thread_func, &ls);
  thread_create ("med", PRI_DEFAULT + 3, m_thread_func, &ls);
  thread_create ("high", PRI_DEFAULT + 5, h_thread_func, &ls);
  lock_acquire (&ls.lockc);
  msg ("Signaling...");
  cond_signal (&ls.cond, &ls.lockc);
  lock_release (&ls.lockc);
  msg ("Main thread finished.");
}

static void
l_thread_func (void *ls_)
{
  struct lock_and_cond *ls = ls_;

  lock_acquire (&ls->lock);
  msg ("Thread L acquired lock.");
  lock_acquire (&ls->lockc);
  cond_wait (&ls->cond, &ls->lockc);
  lock_release (&ls->lockc);
  msg ("Thread L woke up.");
  lock_release (&ls->lock);
  msg ("Thread L finished.");
}

static void
m_thread_func (void *ls_)
{
  struct lock_and_cond *ls = ls_;

  lock_acquire (&ls->lockc);
  cond_wait (&ls->cond, &ls->lockc);
  lock_release (&ls->lockc);
  msg ("Thread M finished.");
}

static void
h_thread_func (void *ls_)
{
  struct lock_and_cond *ls = ls_;

  lock_acquire (&ls->lock);
  msg ("Thread H acquired lock.");

  lock_acquire (&ls->lockc);
  msg ("Signaling...");
  cond_signal (&ls->cond, &ls->lockc);
  lock_release (&ls->lockc);
  lock_release (&ls->lock);
  msg ("Thread H finished.");
}