Infrasctucture Tid Function Reference
Tid
Since most layers of the infrastructure are re-entrant and consequently thread safe, the only layers that have not as yet, been made thread safe are msg, io, and trc.
Posix threads (pthreads) are supported using the msg layer allowing multiple threads to send and receive messages to disparate servers and receive the appropriate response.
Threads are supported as client requestors.
This means that a server may use pthreads to perform parallel processing or to make multiple requests of other servers.
The server mainline is always thread zero and appears to the Orb as a single server.
In order to use pthreads within an application the thread code must register itself with the infrascturcture before attempting any IO (tracing included), using the tidReg() call.
The tidReg() call returns a logical thread number or ID which is unique for that thread.
The logical thread ID is an integer, starting at one (1).
If tidReg() retuns a negative number, this indicates an error.
If the thread needs to ascertain it's own logical thread ID it can call tidGet() which will return the logical ID of the current thread.
After launching all threads, the main line must call msgRcv() until all threads have terminated.
This can be done in a loop with checks for thread progress or terminations.
MsgRcv() is the primary wait call used by threadded operations.
When the thread is about to exit, it must call tidFree( tid ) to free the resources held by the logical pthread.
Code Fragment:
/*H**************************************************************
*
*****************************************************************/
#include "tid.h"
#include "inf.h"
// ==================== DEFINITIONS ================================
// ==================== PROTOTYPES ================================
static void *thd( void *tci );
// ===================== VARIABLES ================================
static int TotCnt =100;
/*F**********************************************************************
* DESC:
* RTRN:
*F**********************************************************************/
int
main( int argc, char *argv[] )
{
pthread_t pTid;
argc = infInit( argc, argv );
pthread_create( &pTid, NULL, thd, NULL );
pthread_detach( pTid ); // DETACH THE THREAD - MANUAL JOIN
pthread_create( &pTid, NULL, thd, NULL );
pthread_detach( pTid ); // DETACH THE THREAD - MANUAL JOIN
msgRcv( -1 ); // ALLOW MSG LAYER TO DO ACTUAL WAITS
return( OK );
}
/*F**********************************************************************
* DESC:
* RTRN:
*F**********************************************************************/
static void*
thd()
{
int myTid, tidIn, hndl, cnt;
myTid = tidReg(); // REGISTER THREAD ID
for( cnt =1; cnt < TotCnt ; cnt++ )
{
if( (hndl = msgMkReq( Server, 3, 3 )) <=0)
trcFatal("msgMkDst Error");
msgPutS( hndl, "Data", "how now brindle bovine" );
msgPutI( hndl, "myTid", myTid );
if( msgSnd( hndl ) != OK )
trcFatal( "msgSnd error");
if( (hndl = msgRcv( 1000 )) <= 0 )
trcFatal("bad receive");
msgGetI( hndl, "myTid", tidIn );
if( tidIn != myTid ) // COMPARE FOR MY MESSAGE RETURNING
trcFatal("Tid mismatch");
msgFree( hndl );
ioWait( 5 ); // WAIT ALLOW OTHER THREADS TO RUN, CREATE MAX CONTENTION
}
tidFree(); // RELEASE/FREE THREAD ID
pthread_exit( (void*)OK );
}
Free the current logical thread ID.
Be aware that the first available logical tid will be reused immediately if another thread is launched so be sure your I/O is all complete before exiting.
The msg layer uses these logical thread IDs to keep track of which message belongs to what thread.
Get the current thread's logical ID.
Register the current pthread and return the logical thread ID.
The first available thread ID will be associated with the current thread.
Waits the current thread.
Wakes the thread "tid".