Mixe for Privacy and Anonymity in the Internet
Public Member Functions | Static Public Member Functions | Private Attributes | Static Private Attributes

This class could be used for creating a new thread. More...

#include <CAThread.hpp>

List of all members.

Public Member Functions

 CAThread ()
 Creates a CAThread object but no actual thread.
 CAThread (const UINT8 *strName)
 Creates a CAThread object but no actual thread.
 ~CAThread ()
SINT32 setMainLoop (THREAD_MAIN_TYP fnc)
 Sets the main function which will be executed within this thread.
SINT32 start (void *param, bool bDaemon=false, bool bSilent=false)
 Starts the execution of the main function of this thread.
SINT32 join ()
 Waits for the main function to finish execution.
UINT8getName () const
UINT32 getID () const

Static Public Member Functions

static thread_id_t getSelfID ()

Private Attributes

THREAD_MAIN_TYP m_fncMainLoop
pthread_t * m_pThread
UINT8m_strName
UINT32 m_Id

Static Private Attributes

static UINT32 ms_LastId = 0

Detailed Description

This class could be used for creating a new thread.

The function which should be executed within this thread could be set be using the setMainLoop() method.

Some example on using CAThread:

First one needs to define a function which should be executed within the thread:

  THREAD_RETURN doSomeThing(void* param)
    {
      THREAD_RETURN_SUCCESS
    }

Now we can create the thread, set the main function, start the thread and wait for the thread to finish execution:

  CAThread* pThread=new CAThread();
  pThread->setMainLoop(doSomeThing);
  pThread->start(theParams);
  pThread->join();
  delete pThread;

Definition at line 104 of file CAThread.hpp.


Constructor & Destructor Documentation

Creates a CAThread object but no actual thread.

Definition at line 54 of file CAThread.cpp.

References m_fncMainLoop, m_Id, m_pThread, m_strName, and ms_LastId.

  {
    m_fncMainLoop=NULL;
#ifdef OS_TUDOS
    m_Thread=L4THREAD_INVALID_ID;
#ifdef PRINT_THREAD_STACK_TRACE 
    assert(ms_threadKey != L4_ENOKEY);
#endif //PRINT_THREAD_STACK_TRACE 
#else
    m_pThread=NULL;
#endif
    m_strName=NULL;
    m_Id=ms_LastId;
    ms_LastId++;
  }
CAThread::CAThread ( const UINT8 strName)

Creates a CAThread object but no actual thread.

Parameters:
strNamea name for this thread, useful mostly for debugging

Definition at line 70 of file CAThread.cpp.

References len, m_fncMainLoop, m_Id, m_pThread, m_strName, and ms_LastId.

  {
    m_fncMainLoop=NULL;
#ifdef OS_TUDOS
    m_Thread=L4THREAD_INVALID_ID;
#else
    m_pThread=NULL;
#endif
    m_strName=NULL;
    if(strName!=NULL)
      {
        UINT32 len=strlen((char*)strName);
        m_strName=new UINT8[len+1];
        memcpy(m_strName,strName,len);
        m_strName[len]=0;
      }
    m_Id=ms_LastId;
    ms_LastId++;
  }
CAThread::~CAThread ( ) [inline]

Definition at line 123 of file CAThread.hpp.

References m_pThread, and m_strName.

        {
          #ifdef OS_TUDOS
          m_Thread = L4THREAD_INVALID_ID;
          #else
          delete m_pThread;
          m_pThread = NULL;
          #endif
          delete[] m_strName;
          m_strName = NULL;
        }

Member Function Documentation

UINT32 CAThread::getID ( ) const [inline]

Definition at line 193 of file CAThread.hpp.

References m_Id.

        {
          return m_Id;
        }
UINT8* CAThread::getName ( ) const [inline]

Definition at line 188 of file CAThread.hpp.

References m_strName.

      {
        return m_strName;
      }
static thread_id_t CAThread::getSelfID ( ) [inline, static]

Definition at line 198 of file CAThread.hpp.

Referenced by CAAccountingDBInterface::checkOwner(), openssl_get_thread_id(), CAAccountingDBInterface::testAndResetOwner(), and CAAccountingDBInterface::testAndSetOwner().

        {
          #ifdef _WIN32
            return (unsigned long) pthread_self().p;
          #else
              return (unsigned long) pthread_self();
          #endif
        }

Waits for the main function to finish execution.

A call of this method will block until the main function exits.

Return values:
E_SUCCESSif successful
E_UNKNOWNotherwise

Definition at line 190 of file CAThread.cpp.

References E_SUCCESS, E_UNKNOWN, m_pThread, m_strName, and CAMsg::printMsg().

Referenced by CALastMix::clean(), CAFirstMix::clean(), CAFirstMix::deleteCountryStats(), CAThreadPool::destroy(), CALastMixA::loop(), CAFirstMixB::loop(), CAFirstMixA::loop(), CALastMixB::loop(), CAMiddleMix::loop(), CAFirstMixA::shutDown(), CADatabase::stop(), CAReplayDatabase::stop(), CAInfoService::stop(), CAReplayCtrlChannelMsgProc::stopTimeStampPorpagation(), CAQueue::test(), CAAccountingSettleThread::~CAAccountingSettleThread(), CAFirstMixChannelList::~CAFirstMixChannelList(), and CATempIPBlockList::~CATempIPBlockList().

{
#ifdef OS_TUDOS
  CAMsg::printMsg(LOG_ERR,"CAThread - join() L4 implement me !\n");
  if(m_Thread==L4THREAD_INVALID_ID)
    return E_SUCCESS;
  
  return E_UNKNOWN;
#else
  if(m_pThread==NULL)
    return E_SUCCESS;
  SINT32 ret=pthread_join(*m_pThread,NULL);
  if(ret==0)
  {
#ifdef DEBUG
      CAMsg::printMsg(LOG_DEBUG,"CAThread %s - join() successful\n", m_strName);
      m_pThreadList->remove(this);
#endif  
        
    delete m_pThread;
    m_pThread=NULL;
    return E_SUCCESS;
  }
  else
  {
    CAMsg::printMsg(LOG_ERR,"CAThread - join() not successful - Error was: %i\n",ret);
    return E_UNKNOWN;
  }
#endif
}

Here is the call graph for this function:

SINT32 CAThread::start ( void *  param,
bool  bDaemon = false,
bool  bSilent = false 
)

Starts the execution of the main function of this thread.

The main function could be set with setMainLoop().

Parameters:
parama pointer which is used as argument to the main function
bDaemontrue, if this thread should be a deamon thread. A daemon thread is a dettached thread, which will not
bSilentif true, no (log) messages about thats going on are produced. This is especially helpful to avoid any blocking on any mutex during a call to start(). preserve a join state. A daemon thread will automatically release resources which are associated with the thread. Normaly this is done by calling join(). The default value is false.
Return values:
E_SUCCESSif the thread could be started successfully
E_UNKNOWNotherwise

Definition at line 118 of file CAThread.cpp.

References bytes2hex(), E_SUCCESS, E_UNKNOWN, m_fncMainLoop, m_pThread, m_strName, and CAMsg::printMsg().

Referenced by CAAccountingSettleThread::CAAccountingSettleThread(), CAFirstMixChannelList::CAFirstMixChannelList(), CATempIPBlockList::CATempIPBlockList(), CAThreadPool::CAThreadPool(), CALastMix::init(), CAFirstMix::init(), CAFirstMix::initCountryStats(), CALastMixA::loop(), CAFirstMixA::loop(), CALastMixB::loop(), CAMiddleMix::loop(), CACmdLnOptions::reread(), CAInfoService::sendHelo(), CADatabase::start(), CAReplayDatabase::start(), CAInfoService::start(), CAReplayCtrlChannelMsgProc::startTimeStampPorpagation(), and CAQueue::test().

  {
    if(m_fncMainLoop==NULL)
      return E_UNKNOWN;

#ifndef OS_TUDOS
    m_pThread=new pthread_t;
#endif

    #ifdef DEBUG
      if(!bSilent)
        CAMsg::printMsg(LOG_DEBUG, "CAThread::start() - creating thread\n");
    #endif

#ifdef OS_TUDOS
    if ((m_Thread = l4thread_create(m_fncMainLoop, param, L4THREAD_CREATE_ASYNC)) < 1)
      {
        m_Thread = L4THREAD_INVALID_ID;
        if(!bSilent)
          CAMsg::printMsg(LOG_ERR, "CAThread::start() - creating new thread failed!\n");
        return E_UNKNOWN;
      }
#else
    SINT32 ret=pthread_create(m_pThread,NULL,m_fncMainLoop,param);
    if(ret!=0)
      {
        if(!bSilent)
          CAMsg::printMsg(LOG_ERR, "CAThread::start() - creating new thread failed! - Err: %i\n",ret);
        delete m_pThread;
        m_pThread=NULL;
        return E_UNKNOWN;
      }
    #endif
#ifdef _DEBUG
    if(m_pThreadList != NULL)
    {
      m_pThreadList->put(this);
    }

    else
    {
      CAMsg::printMsg(LOG_DEBUG, "CAThread::start() - Warning no thread list found\n");
    }
#endif
#ifdef DEBUG
    if(!bSilent)
        CAMsg::printMsg(LOG_DEBUG, "CAThread::start() - thread created sucessful\n");
#endif

    #ifdef OS_TUDOS

    if(m_strName!=NULL&&!bSilent)
      CAMsg::printMsg(LOG_DEBUG,
        "Thread with name: %s created - pthread_t: "l4util_idfmt"\n",
         m_strName, l4util_idstr(l4thread_l4_id(m_Thread)));

    if(bDaemon)
      CAMsg::printMsg(LOG_ERR, "TODO: Emulate pthread_detach on L4 ?!\n");
    #else
    if(m_strName!=NULL&&!bSilent)
      {
        UINT8* temp=bytes2hex(m_pThread,sizeof(pthread_t));
        CAMsg::printMsg(LOG_DEBUG,"Thread with name: %s created - pthread_t: %s\n",m_strName,temp);
        delete[] temp;
        temp = NULL;
      }
    if(bDaemon)
      pthread_detach(*m_pThread);
#endif
    return E_SUCCESS;
  }

Here is the call graph for this function:


Member Data Documentation

Definition at line 220 of file CAThread.hpp.

Referenced by CAThread(), setMainLoop(), and start().

Definition at line 227 of file CAThread.hpp.

Referenced by CAThread(), and getID().

pthread_t* CAThread::m_pThread [private]

Definition at line 224 of file CAThread.hpp.

Referenced by CAThread(), join(), start(), and ~CAThread().

Definition at line 226 of file CAThread.hpp.

Referenced by CAThread(), getName(), join(), start(), and ~CAThread().

UINT32 CAThread::ms_LastId = 0 [static, private]

Definition at line 228 of file CAThread.hpp.

Referenced by CAThread().


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