00001 /* Tests that the highest-priority thread waiting on a semaphore 00002 is the first to wake up. */ 00003 00004 #include <stdio.h> 00005 #include "tests/threads/tests.h" 00006 #include "threads/init.h" 00007 #include "threads/malloc.h" 00008 #include "threads/synch.h" 00009 #include "threads/thread.h" 00010 #include "devices/timer.h" 00011 00012 static thread_func priority_sema_thread; 00013 static struct semaphore sema; 00014 00015 void 00016 test_priority_sema (void) 00017 { 00018 int i; 00019 00020 /* This test does not work with the MLFQS. */ 00021 ASSERT (!thread_mlfqs); 00022 00023 sema_init (&sema, 0); 00024 thread_set_priority (PRI_MIN); 00025 for (i = 0; i < 10; i++) 00026 { 00027 int priority = PRI_DEFAULT - (i + 3) % 10 - 1; 00028 char name[16]; 00029 snprintf (name, sizeof name, "priority %d", priority); 00030 thread_create (name, priority, priority_sema_thread, NULL); 00031 } 00032 00033 for (i = 0; i < 10; i++) 00034 { 00035 sema_up (&sema); 00036 msg ("Back in main thread."); 00037 } 00038 } 00039 00040 static void 00041 priority_sema_thread (void *aux UNUSED) 00042 { 00043 sema_down (&sema); 00044 msg ("Thread %s woke up.", thread_name ()); 00045 }