aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/threads/priority-sema.c
diff options
context:
space:
mode:
authorklaar36 <klas.arvidsson@liu.se>2015-03-20 17:30:24 +0100
committerklaar36 <klas.arvidsson@liu.se>2015-03-20 17:30:24 +0100
commite7bc50ca8ffcaa6ed68ebd2315f78b0f5a7d10ad (patch)
tree4de97af7207676b69cb6a9aba8cb443cc134855d /src/tests/threads/priority-sema.c
parentb0418a24e709f0632d2ede5b0f327c422931939b (diff)
downloadpintos-rs-e7bc50ca8ffcaa6ed68ebd2315f78b0f5a7d10ad.tar.gz
Initial Pintos
Diffstat (limited to 'src/tests/threads/priority-sema.c')
-rw-r--r--src/tests/threads/priority-sema.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/tests/threads/priority-sema.c b/src/tests/threads/priority-sema.c
new file mode 100644
index 0000000..2834a88
--- /dev/null
+++ b/src/tests/threads/priority-sema.c
@@ -0,0 +1,45 @@
+/* Tests that the highest-priority thread waiting on a semaphore
+ is the first to wake up. */
+
+#include <stdio.h>
+#include "tests/threads/tests.h"
+#include "threads/init.h"
+#include "threads/malloc.h"
+#include "threads/synch.h"
+#include "threads/thread.h"
+#include "devices/timer.h"
+
+static thread_func priority_sema_thread;
+static struct semaphore sema;
+
+void
+test_priority_sema (void)
+{
+ int i;
+
+ /* This test does not work with the MLFQS. */
+ ASSERT (!thread_mlfqs);
+
+ sema_init (&sema, 0);
+ thread_set_priority (PRI_MIN);
+ for (i = 0; i < 10; i++)
+ {
+ int priority = PRI_DEFAULT - (i + 3) % 10 - 1;
+ char name[16];
+ snprintf (name, sizeof name, "priority %d", priority);
+ thread_create (name, priority, priority_sema_thread, NULL);
+ }
+
+ for (i = 0; i < 10; i++)
+ {
+ sema_up (&sema);
+ msg ("Back in main thread.");
+ }
+}
+
+static void
+priority_sema_thread (void *aux UNUSED)
+{
+ sema_down (&sema);
+ msg ("Thread %s woke up.", thread_name ());
+}