// threadtest.cc // Simple test case for the threads assignment. // // Create two threads, and have them context switch // back and forth between themselves by calling Thread::Yield, // to illustratethe inner workings of the thread system. // // Copyright (c) 1992-1993 The Regents of the University of California. // All rights reserved. See copyright.h for copyright notice and limitation // of liability and disclaimer of warranty provisions. #include "copyright.h" #include "system.h" #include #include "synch.h" #include "messagebox.h" MessageBox mail("test"); int newMessage = 0; //---------------------------------------------------------------------- // 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(); // } //} //---------------------------------------------------------------------- // ThreadTest // Set up a ping-pong between two threads, by forking a thread // to call SimpleThread, and then calling SimpleThread ourselves. //---------------------------------------------------------------------- //void ThreadTest() //{ // DEBUG('t', "Entering SimpleTest"); // Thread *t = new Thread("forked thread"); // t->Fork(SimpleThread, 1); // SimpleThread(0); // //void ThreadTest() //{ // DEBUG('t', "Entering SimpleTest"); // Thread *t = new Thread("forked thread"); // t->Fork(SimpleThread, 1); // SimpleThread(0); //} void ChildFunction1(int throwaway) { //MessageBox* mail = new MessageBox("test"); int message = 0; DEBUG('l', "%s Starting execute child 1\n", currentThread->getName()); message = newMessage; newMessage++; mail.send(message); DEBUG('l', "%s Sent message %d\n", currentThread->getName(), message); //currentThread->Yield(); } void ChildFunction2(int throwaway){ DEBUG('l', "%s Starting execute child 2\n", currentThread->getName()); int reception; mail.receive(&reception); DEBUG('l', "%s Received message %d\n", currentThread->getName(), reception); } void SelfTest2() { DEBUG('t', "Entering SelfTest2\n\n"); Thread* t1 = new Thread("child 1"); Thread* t2 = new Thread("child 2"); Thread* t3 = new Thread("child 3"); Thread* t4 = new Thread("child 4"); t1->Fork(&ChildFunction1,(int)t1); t2->Fork(&ChildFunction1,(int)t2); t3->Fork(&ChildFunction2,(int)t3); t2->Join(); t3->Join(); t4->Fork(&ChildFunction2,(int)t4); currentThread->Yield(); DEBUG('t', "Leaving SelfTest2\n\n"); } void timerCallBack(int garbage) { //this in invoked on every timer interrupt. } void waitUntil(int time) { Timer* timer2 = new Timer(timerCallBack,0,false); //Do Something to count timer interrupts timer2->setDisabled(true); }