00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include <stdio.h>
00014 #include "tests/threads/tests.h"
00015 #include "threads/init.h"
00016 #include "threads/synch.h"
00017 #include "threads/thread.h"
00018
00019 struct lock_and_sema
00020 {
00021 struct lock lock;
00022 struct semaphore sema;
00023 };
00024
00025 static thread_func l_thread_func;
00026 static thread_func m_thread_func;
00027 static thread_func h_thread_func;
00028
00029 void
00030 test_priority_donate_sema (void)
00031 {
00032 struct lock_and_sema ls;
00033
00034
00035 ASSERT (!thread_mlfqs);
00036
00037
00038 ASSERT (thread_get_priority () == PRI_DEFAULT);
00039
00040 lock_init (&ls.lock);
00041 sema_init (&ls.sema, 0);
00042 thread_create ("low", PRI_DEFAULT + 1, l_thread_func, &ls);
00043 thread_create ("med", PRI_DEFAULT + 3, m_thread_func, &ls);
00044 thread_create ("high", PRI_DEFAULT + 5, h_thread_func, &ls);
00045 sema_up (&ls.sema);
00046 msg ("Main thread finished.");
00047 }
00048
00049 static void
00050 l_thread_func (void *ls_)
00051 {
00052 struct lock_and_sema *ls = ls_;
00053
00054 lock_acquire (&ls->lock);
00055 msg ("Thread L acquired lock.");
00056 sema_down (&ls->sema);
00057 msg ("Thread L downed semaphore.");
00058 lock_release (&ls->lock);
00059 msg ("Thread L finished.");
00060 }
00061
00062 static void
00063 m_thread_func (void *ls_)
00064 {
00065 struct lock_and_sema *ls = ls_;
00066
00067 sema_down (&ls->sema);
00068 msg ("Thread M finished.");
00069 }
00070
00071 static void
00072 h_thread_func (void *ls_)
00073 {
00074 struct lock_and_sema *ls = ls_;
00075
00076 lock_acquire (&ls->lock);
00077 msg ("Thread H acquired lock.");
00078
00079 sema_up (&ls->sema);
00080 lock_release (&ls->lock);
00081 msg ("Thread H finished.");
00082 }