00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <stdio.h>
00013 #include "tests/threads/tests.h"
00014 #include "threads/init.h"
00015 #include "threads/synch.h"
00016 #include "threads/thread.h"
00017
00018 struct locks
00019 {
00020 struct lock *a;
00021 struct lock *b;
00022 };
00023
00024 static thread_func medium_thread_func;
00025 static thread_func high_thread_func;
00026
00027 void
00028 test_priority_donate_nest (void)
00029 {
00030 struct lock a, b;
00031 struct locks locks;
00032
00033
00034 ASSERT (!thread_mlfqs);
00035
00036
00037 ASSERT (thread_get_priority () == PRI_DEFAULT);
00038
00039 lock_init (&a);
00040 lock_init (&b);
00041
00042 lock_acquire (&a);
00043
00044 locks.a = &a;
00045 locks.b = &b;
00046 thread_create ("medium", PRI_DEFAULT + 1, medium_thread_func, &locks);
00047 thread_yield ();
00048 msg ("Low thread should have priority %d. Actual priority: %d.",
00049 PRI_DEFAULT + 1, thread_get_priority ());
00050
00051 thread_create ("high", PRI_DEFAULT + 2, high_thread_func, &b);
00052 thread_yield ();
00053 msg ("Low thread should have priority %d. Actual priority: %d.",
00054 PRI_DEFAULT + 2, thread_get_priority ());
00055
00056 lock_release (&a);
00057 thread_yield ();
00058 msg ("Medium thread should just have finished.");
00059 msg ("Low thread should have priority %d. Actual priority: %d.",
00060 PRI_DEFAULT, thread_get_priority ());
00061 }
00062
00063 static void
00064 medium_thread_func (void *locks_)
00065 {
00066 struct locks *locks = locks_;
00067
00068 lock_acquire (locks->b);
00069 lock_acquire (locks->a);
00070
00071 msg ("Medium thread should have priority %d. Actual priority: %d.",
00072 PRI_DEFAULT + 2, thread_get_priority ());
00073 msg ("Medium thread got the lock.");
00074
00075 lock_release (locks->a);
00076 thread_yield ();
00077
00078 lock_release (locks->b);
00079 thread_yield ();
00080
00081 msg ("High thread should have just finished.");
00082 msg ("Middle thread finished.");
00083 }
00084
00085 static void
00086 high_thread_func (void *lock_)
00087 {
00088 struct lock *lock = lock_;
00089
00090 lock_acquire (lock);
00091 msg ("High thread got the lock.");
00092 lock_release (lock);
00093 msg ("High thread finished.");
00094 }