// threadtest.cc // Test cases for the threads assignment. #include "copyright.h" #include "system.h" #include "synch.h" // testnum is set in main.cc int testnum = 1; //---------------------------------------------------------------------- // SimpleThread // Loop 5 times, yielding the CPU to another ready thread // each iteration. // // "which" is simply a number identifying the thread, for debugging // purposes. //---------------------------------------------------------------------- void SimpleThread(int which) { int num; for (num = 0; num < 5; num++) { printf("*** thread %d looped %d times\n", which, num); currentThread->Yield(); } } //---------------------------------------------------------------------- // ThreadTest1 // Set up a ping-pong between two threads, by forking a thread // to call SimpleThread, and then calling SimpleThread ourselves. //---------------------------------------------------------------------- void ThreadTest1() { DEBUG('t', "Entering ThreadTest1"); Thread *t = new Thread("forked thread"); t->Fork(SimpleThread, 1); SimpleThread(0); } //---------------------------------------------------------------------- // LockTest1 //---------------------------------------------------------------------- Lock *locktest1 = NULL; void LockThread1(int param) { printf("L1:0\n"); locktest1->Acquire(); printf("L1:1\n"); currentThread->Yield(); printf("L1:2\n"); locktest1->Release(); printf("L1:3\n"); } void LockThread2(int param) { printf("L2:0\n"); locktest1->Acquire(); printf("L2:1\n"); currentThread->Yield(); printf("L2:2\n"); locktest1->Release(); printf("L2:3\n"); } void LockTest1() { DEBUG('t', "Entering LockTest1"); locktest1 = new Lock("LockTest1"); Thread *t = new Thread("one"); t->Fork(LockThread1, 0); t = new Thread("two"); t->Fork(LockThread2, 0); } //---------------------------------------------------------------------- // ThreadTest // Invoke a test routine. //---------------------------------------------------------------------- void ThreadTest() { switch (testnum) { case 1: ThreadTest1(); break; case 2: LockTest1(); break; default: printf("No test specified.\n"); break; } }