diff options
Diffstat (limited to 'tests/threads/priority-condvar.c')
| -rw-r--r-- | tests/threads/priority-condvar.c | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/tests/threads/priority-condvar.c b/tests/threads/priority-condvar.c new file mode 100644 index 0000000..c1efb1b --- /dev/null +++ b/tests/threads/priority-condvar.c | |||
| @@ -0,0 +1,53 @@ | |||
| 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 | |||
| 12 | static thread_func priority_condvar_thread; | ||
| 13 | static struct lock lock; | ||
| 14 | static struct condition condition; | ||
| 15 | |||
| 16 | void | ||
| 17 | test_priority_condvar (void) | ||
| 18 | { | ||
| 19 | int i; | ||
| 20 | |||
| 21 | /* This test does not work with the MLFQS. */ | ||
| 22 | ASSERT (!thread_mlfqs); | ||
| 23 | |||
| 24 | lock_init (&lock); | ||
| 25 | cond_init (&condition); | ||
| 26 | |||
| 27 | thread_set_priority (PRI_MIN); | ||
| 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); | ||
| 33 | thread_create (name, priority, priority_condvar_thread, NULL); | ||
| 34 | } | ||
| 35 | |||
| 36 | for (i = 0; i < 10; i++) | ||
| 37 | { | ||
| 38 | lock_acquire (&lock); | ||
| 39 | msg ("Signaling..."); | ||
| 40 | cond_signal (&condition, &lock); | ||
| 41 | lock_release (&lock); | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | static void | ||
| 46 | priority_condvar_thread (void *aux UNUSED) | ||
| 47 | { | ||
| 48 | msg ("Thread %s starting.", thread_name ()); | ||
| 49 | lock_acquire (&lock); | ||
| 50 | cond_wait (&condition, &lock); | ||
| 51 | msg ("Thread %s woke up.", thread_name ()); | ||
| 52 | lock_release (&lock); | ||
| 53 | } | ||
