Fast Research Interface Library  Manual and Documentation
Public Member Functions | Static Protected Member Functions | Protected Attributes
Console Class Reference

Screen or file output for real-time threads. More...

#include <Console.h>

List of all members.

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()

Detailed Description

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.

Definition at line 100 of file Console.h.


Constructor & Destructor Documentation

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.

Parameters:
PriorityThe priority of the console thread
FileHandlerA file handler, to which the output is written. The default is stdout.
Attention:
The call of the constructor does not fulfill any real-time requirements.

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.

Attention:
The call of the destructor does not fulfill any real-time requirements.

Definition at line 135 of file Console.cpp.


Member Function Documentation

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.

Parameters:
ObjectPointerA 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.

Returns:
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.

Parameters:
FormatFormats 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.
Returns:
  • The number characters sent to the file handler Console::Handler.
  • If the capacity of the double buffer is exceeded, 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.


Member Data Documentation

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.

Definition at line 359 of file Console.h.

bool Console::BufferNumber [protected]

Buffer selection variable.

  • false $ \longrightarrow $ first double buffer half
  • true $ \longrightarrow $ second double buffer half

This variable is protected by the mutex Console::Mutex.

Definition at line 296 of file Console.h.

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.

Definition at line 346 of file Console.h.

pthread_t Console::ConsoleThread [protected]

Thread ID of Console::ConsoleThreadMain()

A pthread_t object containing the thread ID of Console::ConsoleThreadMain().

Definition at line 308 of file Console.h.

This flag is set by the main thread in order to acknowledge the console thread that it can run its loop.

Definition at line 271 of file Console.h.

FILE * Console::Handler [protected]

The file handler Console::Handler.

Definition at line 215 of file Console.h.

pthread_mutex_t Console::Mutex [protected]

Mutex to protect the variables Console::BufferNumber and Console::Buffer.

This pthread_mutex_t object protects

Definition at line 324 of file Console.h.

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].

Definition at line 251 of file Console.h.

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.

Definition at line 281 of file Console.h.

bool Console::ThreadCreated [protected]

This flag is set during the thread creation in order to acknowledge the creating thread.

Definition at line 261 of file Console.h.


The documentation for this class was generated from the following files:
This document was generated with Doxygen on Thu Apr 12 2012 11:18:54. User documentation of the Fast Research Interface Library for the KUKA Lightweight Robot IV by the Stanford Robotics Research Group. Copyright 2010–2012.