00001 #include "synchconsole.h"
00002 #include "console.h"
00003 #include "synch.h"
00004
00005 void SynchConsole::WriteDone() { outputDone->V();}
00006 void SynchConsole::ReadAvail() { inputAvailable->V();}
00007
00008
00009 static void SynchReadAvail(SynchConsole *SC) { SC->ReadAvail();}
00010 static void SynchWriteDone(SynchConsole *SC) { SC->WriteDone();}
00011
00012
00013 SynchConsole::SynchConsole(char *readFile, char *writeFile)
00014 {
00015 inputMutex = new Lock("synchronized console input mutex");
00016 ASSERT(inputMutex != NULL);
00017 inputAvailable = new Semaphore("synchronized console input semaphore",0);
00018 ASSERT(inputAvailable != NULL);
00019 outputMutex = new Lock("synchronized console output mutex");
00020 ASSERT(outputMutex != NULL);
00021 outputDone = new Semaphore("synchronized console output semaphore", 0);
00022 ASSERT(outputDone != NULL);
00023
00024 console = new Console(readFile, writeFile,
00025 (VoidFunctionPtr)SynchReadAvail, (VoidFunctionPtr)SynchWriteDone,(int) this);
00026 }
00027
00028 SynchConsole::~SynchConsole()
00029 {
00030 delete inputMutex;
00031 inputMutex = NULL;
00032 delete outputMutex;
00033 outputMutex = NULL;
00034 delete inputAvailable;
00035 inputAvailable = NULL;
00036 delete outputDone;
00037 outputDone = NULL;
00038 delete console;
00039 console = 0;
00040 }
00041
00042 void
00043 SynchConsole::WriteLine(char *line, int size)
00044 {
00045
00046 outputMutex->Acquire();
00047 while (size --) {
00048 console->PutChar(*line);
00049 line++;
00050 outputDone->P();
00051 }
00052 outputMutex->Release();
00053 }
00054
00055 int
00056 SynchConsole::ReadLine(char *line, int size)
00057 {
00058 char c = 0;
00059 int num_read = 0;
00060 inputMutex->Acquire();
00061 while (c != '\n' && num_read < size) {
00062 inputAvailable->P();
00063 c = console->GetChar();
00064 *line = c;
00065 line++; num_read++;
00066 }
00067 inputMutex->Release();
00068 DEBUG('r', "Read the %d character string %s into buffer of size %d\n",num_read, line-num_read, size);
00069 return num_read;
00070 }
00071
The University of Southern California does not screen or control the content on this website and thus does not guarantee the accuracy, integrity, or quality of such content. All content on this website is provided by and is the sole responsibility of the person from which such content originated, and such content does not necessarily reflect the opinions of the University administration or the Board of Trustees