00001 // synchconsole.cc 00002 // Routines providing synchronized access to the keyboard 00003 // and console display hardware devices. 00004 // 00005 // Copyright (c) 1992-1996 The Regents of the University of California. 00006 // All rights reserved. See copyright.h for copyright notice and limitation 00007 // of liability and disclaimer of warranty provisions. 00008 00009 #include "copyright.h" 00010 #include "synchconsole.h" 00011 00012 //---------------------------------------------------------------------- 00013 // SynchConsoleInput::SynchConsoleInput 00014 // Initialize synchronized access to the keyboard 00015 // 00016 // "inputFile" -- if NULL, use stdin as console device 00017 // otherwise, read from this file 00018 //---------------------------------------------------------------------- 00019 00020 SynchConsoleInput::SynchConsoleInput(char *inputFile) 00021 { 00022 consoleInput = new ConsoleInput(inputFile, this); 00023 lock = new Lock("console in"); 00024 waitFor = new Semaphore("console in", 0); 00025 } 00026 00027 //---------------------------------------------------------------------- 00028 // SynchConsoleInput::~SynchConsoleInput 00029 // Deallocate data structures for synchronized access to the keyboard 00030 //---------------------------------------------------------------------- 00031 00032 SynchConsoleInput::~SynchConsoleInput() 00033 { 00034 delete consoleInput; 00035 delete lock; 00036 delete waitFor; 00037 } 00038 00039 //---------------------------------------------------------------------- 00040 // SynchConsoleInput::GetChar 00041 // Read a character typed at the keyboard, waiting if necessary. 00042 //---------------------------------------------------------------------- 00043 00044 char 00045 SynchConsoleInput::GetChar() 00046 { 00047 char ch; 00048 00049 lock->Acquire(); 00050 waitFor->P(); // wait for EOF or a char to be available. 00051 ch = consoleInput->GetChar(); 00052 lock->Release(); 00053 return ch; 00054 } 00055 00056 //---------------------------------------------------------------------- 00057 // SynchConsoleInput::CallBack 00058 // Interrupt handler called when keystroke is hit; wake up 00059 // anyone waiting. 00060 //---------------------------------------------------------------------- 00061 00062 void 00063 SynchConsoleInput::CallBack() 00064 { 00065 waitFor->V(); 00066 } 00067 00068 //---------------------------------------------------------------------- 00069 // SynchConsoleOutput::SynchConsoleOutput 00070 // Initialize synchronized access to the console display 00071 // 00072 // "outputFile" -- if NULL, use stdout as console device 00073 // otherwise, read from this file 00074 //---------------------------------------------------------------------- 00075 00076 SynchConsoleOutput::SynchConsoleOutput(char *outputFile) 00077 { 00078 consoleOutput = new ConsoleOutput(outputFile, this); 00079 lock = new Lock("console out"); 00080 waitFor = new Semaphore("console out", 0); 00081 } 00082 00083 //---------------------------------------------------------------------- 00084 // SynchConsoleOutput::~SynchConsoleOutput 00085 // Deallocate data structures for synchronized access to the keyboard 00086 //---------------------------------------------------------------------- 00087 00088 SynchConsoleOutput::~SynchConsoleOutput() 00089 { 00090 delete consoleOutput; 00091 delete lock; 00092 delete waitFor; 00093 } 00094 00095 //---------------------------------------------------------------------- 00096 // SynchConsoleOutput::PutChar 00097 // Write a character to the console display, waiting if necessary. 00098 //---------------------------------------------------------------------- 00099 00100 void 00101 SynchConsoleOutput::PutChar(char ch) 00102 { 00103 lock->Acquire(); 00104 consoleOutput->PutChar(ch); 00105 waitFor->P(); 00106 lock->Release(); 00107 } 00108 00109 //---------------------------------------------------------------------- 00110 // SynchConsoleOutput::CallBack 00111 // Interrupt handler called when it's safe to send the next 00112 // character can be sent to the display. 00113 //---------------------------------------------------------------------- 00114 00115 void 00116 SynchConsoleOutput::CallBack() 00117 { 00118 waitFor->V(); 00119 }