Orxonox  0.0.5 Codename: Arcturus
Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | Friends | List of all members
orxonox::TclThreadManager Class Reference

#include </home/jenkins/workspace/orxonox_doxygen_trunk/src/libraries/core/command/TclThreadManager.h>

Inheritance diagram for orxonox::TclThreadManager:
orxonox::Singleton< TclThreadManager >

Public Member Functions

 TclThreadManager (Tcl::interpreter *interpreter)
 Constructor. More...
 
virtual ~TclThreadManager ()
 Destructor. More...
 
std::list< unsigned int > getThreadList () const
 Returns a list with the numbers of all existing Tcl-interpreters. More...
 
void preUpdate (const Clock &time)
 The "main loop" of the TclThreadManager. More...
 

Static Public Member Functions

static unsigned int create ()
 Creates a new Tcl-interpreter. More...
 
static Tcl::interpreter * createWithId (unsigned int id)
 Creates a new Tcl-interpreter with a given id. More...
 
static void debug (const std::string &error)
 A helper function to print debug information in a thread safe manner. More...
 
static void destroy (unsigned int id)
 Stops and destroys a given Tcl-interpreter. More...
 
static void error (const std::string &error)
 A helper function to print errors in a thread safe manner. More...
 
static void execute (unsigned int target_id, const std::string &command)
 Sends a command to the queue of a given Tcl-interpreter. More...
 
static std::string query (unsigned int target_id, const std::string &command)
 Sends a query to a given Tcl-interpreter and waits for the result. More...
 
static void source (const std::string &file)
 Creates a non-interactive Tcl-interpreter which executes a file. More...
 
- Static Public Member Functions inherited from orxonox::Singleton< TclThreadManager >
static bool exists ()
 Tells whether the singleton has been created. More...
 
static TclThreadManagergetInstance ()
 Returns a reference to the singleton instance. More...
 

Private Member Functions

void _execute (unsigned int target_id, const std::string &command)
 Sends a command to the queue of a given Tcl-interpreter. More...
 
std::string _query (unsigned int source_id, unsigned int target_id, const std::string &command, bool bUseCommandExecutor=false)
 This function performs a query to any Tcl interpreter. More...
 
std::string dumpList (const std::list< unsigned int > &list)
 Returns a string containing all elements of a unsigned-integer-list separated by spaces. More...
 
TclInterpreterBundlegetInterpreterBundle (unsigned int id)
 Returns the interpreter bundle with the given id. More...
 

Static Private Member Functions

static std::string eval (TclInterpreterBundle *bundle, const std::string &command, const std::string &action)
 Evaluates a Tcl command without throwing exceptions (which may rise problems on certain machines). More...
 
static void initialize (TclInterpreterBundle *bundle)
 
static void tcl_crossexecute (int target_id, const Tcl::object &args)
 This function can be called from Tcl to send a command to the queue of any interpreter. More...
 
static std::string tcl_crossquery (int source_id, int target_id, const Tcl::object &args)
 This function can be called from Tcl to send a query to another thread. More...
 
static void tcl_execute (const Tcl::object &args)
 This function can be called from Tcl to execute a console command. More...
 
static std::string tcl_query (int source_id, const Tcl::object &args)
 This function can be called from Tcl to send a query to the main thread. More...
 
static bool tcl_running (int id)
 This function can be called from Tcl to ask if the thread is still suposed to be running. More...
 

Private Attributes

std::map< unsigned int, TclInterpreterBundle * > interpreterBundles_
 A map containing all Tcl-interpreters. More...
 
boost::shared_mutex * interpreterBundlesMutex_
 A mutex used to synchronize threads when accessing interpreterBundles_. More...
 
boost::mutex * mainInterpreterMutex_
 A mutex to synchronize queries to the main interpreter. More...
 
TclThreadList< std::string > * messageQueue_
 A queue to pass messages from Tcl-threads to the main thread. More...
 
unsigned int numInterpreterBundles_
 Number of created Tcl-interpreters (only used for auto-numbered interpreters, not affected by createWithId) More...
 

Static Private Attributes

static TclThreadManagersingletonPtr_s = nullptr
 Singleton pointer. More...
 

Friends

class Singleton< TclThreadManager >
 
_CoreExport void sourceThread (const std::string &file)
 The main function of a non-interactive source thread. More...
 
_CoreExport int Tcl_OrxonoxAppInit (Tcl_Interp *interp)
 A tcl-init hook to inject the non-interactive Tcl-interpreter into the TclThreadManager. More...
 
class TclBind
 
_CoreExport void tclThread (TclInterpreterBundle *bundle, const std::string &command)
 The main function of the thread. More...
 

Additional Inherited Members

- Protected Member Functions inherited from orxonox::Singleton< TclThreadManager >
 Singleton ()
 Constructor sets the singleton instance pointer. More...
 
virtual ~Singleton ()
 Destructor resets the singleton instance pointer. More...
 

Constructor & Destructor Documentation

orxonox::TclThreadManager::TclThreadManager ( Tcl::interpreter *  interpreter)

Constructor.

Parameters
interpreterA pointer to the standard Tcl-interpreter (see TclBind)
orxonox::TclThreadManager::~TclThreadManager ( )
virtual

Destructor.

Member Function Documentation

void orxonox::TclThreadManager::_execute ( unsigned int  target_id,
const std::string &  command 
)
private

Sends a command to the queue of a given Tcl-interpreter.

Parameters
target_idThe id of the target interpreter
commandThe command to be sent
std::string orxonox::TclThreadManager::_query ( unsigned int  source_id,
unsigned int  target_id,
const std::string &  command,
bool  bUseCommandExecutor = false 
)
private

This function performs a query to any Tcl interpreter.

Parameters
source_idThe id of the calling thread
target_idThe id of the target thread
commandThe command to send as a query
bUseCommandExecutorOnly used if the target_id is 0 (which references the main interpreter). In this case it means if the command should be passed to the CommandExecutor (true) or to the main Tcl interpreter (false). This is true when called by tcl_query() and false when called by tcl_crossquery().
unsigned int orxonox::TclThreadManager::create ( )
static

Creates a new Tcl-interpreter.

Tcl::interpreter * orxonox::TclThreadManager::createWithId ( unsigned int  id)
static

Creates a new Tcl-interpreter with a given id.

Use with caution - if the id collides with an already existing interpreter, this call will fail. This will also be a problem, if the auto-numbered interpreters (by using create()) reach an id which was previously used in this function. Use high numbers to be safe.

void orxonox::TclThreadManager::debug ( const std::string &  error)
static

A helper function to print debug information in a thread safe manner.

void orxonox::TclThreadManager::destroy ( unsigned int  id)
static

Stops and destroys a given Tcl-interpreter.

std::string orxonox::TclThreadManager::dumpList ( const std::list< unsigned int > &  list)
private

Returns a string containing all elements of a unsigned-integer-list separated by spaces.

void orxonox::TclThreadManager::error ( const std::string &  error)
static

A helper function to print errors in a thread safe manner.

std::string orxonox::TclThreadManager::eval ( TclInterpreterBundle bundle,
const std::string &  command,
const std::string &  action 
)
staticprivate

Evaluates a Tcl command without throwing exceptions (which may rise problems on certain machines).

Returns
The Tcl return value

Errors are reported through the error function.

void orxonox::TclThreadManager::execute ( unsigned int  target_id,
const std::string &  command 
)
static

Sends a command to the queue of a given Tcl-interpreter.

Parameters
target_idThe id of the target interpreter
commandThe command to be sent
TclInterpreterBundle * orxonox::TclThreadManager::getInterpreterBundle ( unsigned int  id)
private

Returns the interpreter bundle with the given id.

Parameters
idThe id of the interpreter
Returns
The interpreter or 0 if the id doesn't exist
std::list< unsigned int > orxonox::TclThreadManager::getThreadList ( ) const

Returns a list with the numbers of all existing Tcl-interpreters.

This function is used by the auto completion function.

void orxonox::TclThreadManager::initialize ( TclInterpreterBundle bundle)
staticprivate
void orxonox::TclThreadManager::preUpdate ( const Clock time)

The "main loop" of the TclThreadManager.

Creates new threads if needed and handles queries and queued commands for the main interpreter.

std::string orxonox::TclThreadManager::query ( unsigned int  target_id,
const std::string &  command 
)
static

Sends a query to a given Tcl-interpreter and waits for the result.

Parameters
target_idThe id of the target interpreter
commandThe command to be sent
Returns
The result of the command
void orxonox::TclThreadManager::source ( const std::string &  file)
static

Creates a non-interactive Tcl-interpreter which executes a file.

void orxonox::TclThreadManager::tcl_crossexecute ( int  target_id,
const Tcl::object &  args 
)
staticprivate

This function can be called from Tcl to send a command to the queue of any interpreter.

Parameters
target_idThe id of the target thread
argsContains the content of the command
std::string orxonox::TclThreadManager::tcl_crossquery ( int  source_id,
int  target_id,
const Tcl::object &  args 
)
staticprivate

This function can be called from Tcl to send a query to another thread.

Parameters
source_idThe id of the calling thread
target_idThe id of the target thread
argsContains the content of the query
void orxonox::TclThreadManager::tcl_execute ( const Tcl::object &  args)
staticprivate

This function can be called from Tcl to execute a console command.

Commands which shall be executed are put into a queue and processed as soon as the main thread feels ready to do so. The queue may also be full which results in a temporary suspension of the calling thread until the queue gets ready again.

std::string orxonox::TclThreadManager::tcl_query ( int  source_id,
const Tcl::object &  args 
)
staticprivate

This function can be called from Tcl to send a query to the main thread.

Parameters
source_idThe id of the calling thread
argsContains the content of the query

A query waits for the result of the command. This means, the calling thread will be blocked until the main thread answers the query. In return, the main thread sends the result of the console command back to Tcl.

bool orxonox::TclThreadManager::tcl_running ( int  id)
staticprivate

This function can be called from Tcl to ask if the thread is still suposed to be running.

Parameters
idThe id of the thread in question

Friends And Related Function Documentation

friend class Singleton< TclThreadManager >
friend
_CoreExport void sourceThread ( const std::string &  file)
friend

The main function of a non-interactive source thread.

Executes the file.

Parameters
fileThe name of the file that should be executed by the non-interactive interpreter.
_CoreExport int Tcl_OrxonoxAppInit ( Tcl_Interp *  interp)
friend

A tcl-init hook to inject the non-interactive Tcl-interpreter into the TclThreadManager.

friend class TclBind
friend
_CoreExport void tclThread ( TclInterpreterBundle bundle,
const std::string &  command 
)
friend

The main function of the thread.

Executes a Tcl command.

Parameters
bundleThe interpreter bundle containing all necessary variables
commandthe Command to execute

Member Data Documentation

std::map<unsigned int, TclInterpreterBundle*> orxonox::TclThreadManager::interpreterBundles_
private

A map containing all Tcl-interpreters.

boost::shared_mutex* orxonox::TclThreadManager::interpreterBundlesMutex_
private

A mutex used to synchronize threads when accessing interpreterBundles_.

boost::mutex* orxonox::TclThreadManager::mainInterpreterMutex_
private

A mutex to synchronize queries to the main interpreter.

TclThreadList<std::string>* orxonox::TclThreadManager::messageQueue_
private

A queue to pass messages from Tcl-threads to the main thread.

unsigned int orxonox::TclThreadManager::numInterpreterBundles_
private

Number of created Tcl-interpreters (only used for auto-numbered interpreters, not affected by createWithId)

TclThreadManager * orxonox::TclThreadManager::singletonPtr_s = nullptr
staticprivate

Singleton pointer.


The documentation for this class was generated from the following files: