|
Fast Research Interface Library
Manual and Documentation
|
Screen or file output for real-time threads. More...
#include <Console.h>
Public Member Functions | |
| Console (const unsigned int &Priority, const FILE *FileHandler=stdout) | |
| Constructor. | |
| ~Console (void) | |
| Destructor. | |
| int | printf (const char *Format,...) |
| A real-time wrapper for printf. | |
| void | flush (void) const |
| Prints all pending output data. | |
| FILE * | GetFileHandler (void) const |
| Returns the file handler Console::Handler. | |
Static Protected Member Functions | |
| static void * | ConsoleThreadMain (void *ObjectPointer) |
| Thread that performs the actual output. | |
Protected Attributes | |
| FILE * | Handler |
| The file handler Console::Handler. | |
| unsigned int | NumberOfMessages [2] |
| An array of two integer values; each representing the current number of message in the double buffer Console::Buffer[2]. | |
| bool | ThreadCreated |
| This flag is set during the thread creation in order to acknowledge the creating thread. | |
| bool | ConsoleThreadReadyToRun |
| This flag is set by the main thread in order to acknowledge the console thread that it can run its loop. | |
| bool | TermintateThread |
If this flag is set by the destructor, and if the thread Console::ConsoleThreadMain() gets waked up by pthread_signal(), Console::ConsoleThreadMain() will terminate. | |
| bool | BufferNumber |
| Buffer selection variable. | |
| pthread_t | ConsoleThread |
| Thread ID of Console::ConsoleThreadMain() | |
| pthread_mutex_t | Mutex |
| Mutex to protect the variables Console::BufferNumber and Console::Buffer. | |
| pthread_cond_t | CondVar |
| Condition variable to acknowledge the thread Console::ConsoleThreadMain() that new data is waiting for output. | |
| char | Buffer [2][CONSOLE_NUMBER_OF_BUFFER_ENTRIES][CONSOLE_BUFFER_ENTRY_SIZE] |
| Double buffer for data exchange between the data-sending (real-time) threads and Console::ConsoleThreadMain() | |
Screen or file output for real-time threads.
This class is used for the entire output to the console. To do this without interfering with the real-time requirements of other processes and/or threads, a dedicated thread with a low priority is used for the actual output. This class implements the singleton pattern, since only one instance is needed in any process.
Basically, this class offers a variant of the printf() function that can be used by real-time threads of the calling process.
| Console::Console | ( | const unsigned int & | Priority, |
| const FILE * | FileHandler = stdout |
||
| ) |
Constructor.
The constructor creates a process running at a priority of Priority, and it prepares a double buffer for data exchange between real-time threads that may call the method Console::printf() and the low-priority thread Console::ConsoleThreadMain() that is responsible for the actual output to FileHandler.
| Priority | The priority of the console thread |
| FileHandler | A file handler, to which the output is written. The default is stdout. |
Definition at line 74 of file Console.cpp.
| Console::~Console | ( | void | ) |
Destructor.
By using the condition variable Console::CondVar and setting the flag Console::TermintateThread, the destructor sends a signal to the output thread Console::ConsoleThreadMain(), which will terminate it. Afterwards, the thread is joined.
Definition at line 135 of file Console.cpp.
| static void * Console::ConsoleThreadMain | ( | void * | ObjectPointer | ) | [static, protected] |
Thread that performs the actual output.
This thread runs at priority of Priority (specified by the constructor Console::Console() of the class Console. After successful creation, this thread waits for the condition variable Console::CondVar (1) to be waked up, which means that one of the (real-time) threads wrote output data into the double buffer Console::Buffer[2], (2) to lock the mutex Console::Mutex, (3) to switch the boolean buffer selection variable, (4) to unlock the mutex Console::Mutex again, (5) to empty the used half of the double buffer by writing its content to Console::Handler, and (6) wait for a message in the other half of the double buffer (or empty it if data is already waiting).
If the thread gets waked up by the destructor Console::~Console() via pthread_cond_signal(), and if Console::TermintateThread is set, the thread terminates.
| ObjectPointer | A pointer the current Console object of the calling process. |
Definition at line 183 of file Console.cpp.
| void Console::flush | ( | void | ) | const |
Prints all pending output data.
This method flushes all pending data of the file handler Console::Handler.
Definition at line 257 of file Console.cpp.
| FILE * Console::GetFileHandler | ( | void | ) | const |
Returns the file handler Console::Handler.
Definition at line 267 of file Console.cpp.
| int Console::printf | ( | const char * | Format, |
| ... | |||
| ) |
A real-time wrapper for printf.
This is the main method of the class Console; it may be called by real-time threads of the process to which this object belongs. It copies the formated character string to one part of the double buffer and uses the condition variable Console::CondVar to wake up the low-priority thread Console::ConsoleThreadMain(), which can work on the actual output as soon as it becomes scheduled.
| Format | Formats the string to be sent to Console::Handler. For details please refer for example to this page: http://www.qnx.com/developers/docs/6.3.2/neutrino/lib_ref/p/printf.html. |
| ... | A variable argument list is used here. For details, please refer to stdarg.h and http://www.qnx.com/developers/docs/6.3.2/neutrino/lib_ref/p/printf.html. |
ENOBUFS is returned, and a corresponding string is written to the output in order to acknowledge the user. Definition at line 148 of file Console.cpp.
char Console::Buffer[2][CONSOLE_NUMBER_OF_BUFFER_ENTRIES][CONSOLE_BUFFER_ENTRY_SIZE] [protected] |
Double buffer for data exchange between the data-sending (real-time) threads and Console::ConsoleThreadMain()
This double buffer variable is protected by the mutex Console::Mutex, such that only one real-time thread can write new data into one half of it.
bool Console::BufferNumber [protected] |
Buffer selection variable.
false
first double buffer halftrue
second double buffer halfThis variable is protected by the mutex Console::Mutex.
pthread_cond_t Console::CondVar [protected] |
Condition variable to acknowledge the thread Console::ConsoleThreadMain() that new data is waiting for output.
This pthread_cond_t object to
This condition variable is always used in combination with the mutex Console:Mutex.
pthread_t Console::ConsoleThread [protected] |
Thread ID of Console::ConsoleThreadMain()
A pthread_t object containing the thread ID of Console::ConsoleThreadMain().
bool Console::ConsoleThreadReadyToRun [protected] |
FILE * Console::Handler [protected] |
The file handler Console::Handler.
pthread_mutex_t Console::Mutex [protected] |
Mutex to protect the variables Console::BufferNumber and Console::Buffer.
This pthread_mutex_t object protects
unsigned int Console::NumberOfMessages[2] [protected] |
An array of two integer values; each representing the current number of message in the double buffer Console::Buffer[2].
bool Console::TermintateThread [protected] |
If this flag is set by the destructor, and if the thread Console::ConsoleThreadMain() gets waked up by pthread_signal(), Console::ConsoleThreadMain() will terminate.
bool Console::ThreadCreated [protected] |