Mixe for Privacy and Anonymity in the Internet
Public Member Functions | Static Public Member Functions | Private Attributes | Static Private Attributes | List of all members
CAThread Class Reference

#include <CAThread.hpp>

Collaboration diagram for CAThread:

Public Member Functions

 CAThread ()
 Creates a CAThread object but no actual thread. More...
 
 CAThread (const UINT8 *strName)
 Creates a CAThread object but no actual thread. More...
 
 ~CAThread ()
 
SINT32 setMainLoop (THREAD_MAIN_TYP fnc)
 Sets the main function which will be executed within this thread. More...
 
SINT32 start (void *param, bool bDaemon=false, bool bSilent=false)
 Starts the execution of the main function of this thread. More...
 
SINT32 join ()
 Waits for the main function to finish execution. More...
 
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

@ingroup threading

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)
{
}
#define THREAD_RETURN
Definition: StdAfx.h:540
#define THREAD_RETURN_SUCCESS
Definition: StdAfx.h:542

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;
CAThread()
Creates a CAThread object but no actual thread.
Definition: CAThread.cpp:54
SINT32 start(void *param, bool bDaemon=false, bool bSilent=false)
Starts the execution of the main function of this thread.
Definition: CAThread.cpp:115
SINT32 setMainLoop(THREAD_MAIN_TYP fnc)
Sets the main function which will be executed within this thread.
Definition: CAThread.hpp:148
SINT32 join()
Waits for the main function to finish execution.
Definition: CAThread.cpp:187

Definition at line 114 of file CAThread.hpp.

Constructor & Destructor Documentation

◆ CAThread() [1/2]

CAThread::CAThread ( )

Creates a CAThread object but no actual thread.

Definition at line 54 of file CAThread.cpp.

55  {
56  m_fncMainLoop=NULL;
57 #ifdef OS_TUDOS
58  m_Thread=L4THREAD_INVALID_ID;
59 #ifdef PRINT_THREAD_STACK_TRACE
60  assert(ms_threadKey != L4_ENOKEY);
61 #endif //PRINT_THREAD_STACK_TRACE
62 #else
63  m_pThread=NULL;
64 #endif
65  m_strName=NULL;
67  ms_LastId++;
68  }
static UINT32 ms_LastId
Definition: CAThread.hpp:231
THREAD_MAIN_TYP m_fncMainLoop
Definition: CAThread.hpp:223
UINT32 m_Id
Definition: CAThread.hpp:230
UINT8 * m_strName
Definition: CAThread.hpp:229
pthread_t * m_pThread
Definition: CAThread.hpp:227

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

◆ CAThread() [2/2]

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.

71  {
72  m_fncMainLoop=NULL;
73 #ifdef OS_TUDOS
74  m_Thread=L4THREAD_INVALID_ID;
75 #else
76  m_pThread=NULL;
77 #endif
78  m_strName=NULL;
79  if(strName!=NULL)
80  {
81  UINT32 len=strlen((char*)strName);
82  m_strName=new UINT8[len+1];
83  memcpy(m_strName,strName,len);
84  m_strName[len]=0;
85  }
87  ms_LastId++;
88  }
unsigned char UINT8
Definition: basetypedefs.h:135
unsigned int UINT32
Definition: basetypedefs.h:131
UINT16 len
Definition: typedefs.hpp:0

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

◆ ~CAThread()

CAThread::~CAThread ( )
inline

Definition at line 126 of file CAThread.hpp.

127  {
128  #ifdef OS_TUDOS
129  m_Thread = L4THREAD_INVALID_ID;
130  #else
131  delete m_pThread;
132  m_pThread = NULL;
133  #endif
134  delete[] m_strName;
135  m_strName = NULL;
136  }

References m_pThread, and m_strName.

Member Function Documentation

◆ getID()

UINT32 CAThread::getID ( ) const
inline

Definition at line 196 of file CAThread.hpp.

197  {
198  return m_Id;
199  }

References m_Id.

Referenced by CAThreadList::remove(), CAThreadList::removeAll(), CAThreadList::showAll(), and CAThreadList::waitAndRemoveAll().

Here is the caller graph for this function:

◆ getName()

UINT8* CAThread::getName ( ) const
inline

Definition at line 191 of file CAThread.hpp.

192  {
193  return m_strName;
194  }

References m_strName.

Referenced by CAThreadList::removeAll(), CAThreadList::showAll(), and CAThreadList::waitAndRemoveAll().

Here is the caller graph for this function:

◆ getSelfID()

static thread_id_t CAThread::getSelfID ( )
inlinestatic

Definition at line 201 of file CAThread.hpp.

202  {
203  #ifdef _WIN32
204  return (thread_id_t)pthread_self().p;
205  #else
206  return (thread_id_t)pthread_self();
207  #endif
208  }
unsigned long long thread_id_t
Type of an ID for a thread which can be used to identify the current and other threads.
Definition: CAThread.hpp:70

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

Here is the caller graph for this function:

◆ join()

SINT32 CAThread::join ( )

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 187 of file CAThread.cpp.

188 {
189 #ifdef OS_TUDOS
190  CAMsg::printMsg(LOG_ERR,"CAThread - join() L4 implement me !\n");
191  if(m_Thread==L4THREAD_INVALID_ID)
192  return E_SUCCESS;
193 
194  return E_UNKNOWN;
195 #else
196  if(m_pThread==NULL)
197  return E_SUCCESS;
198  SINT32 ret=pthread_join(*m_pThread,NULL);
199  if(ret==0)
200  {
201 #if defined DEBUG && !defined ONLY_LOCAL_PROXY
202  CAMsg::printMsg(LOG_DEBUG,"CAThread %s - join() successful\n", m_strName);
203  m_pThreadList->remove(this);
204 #endif
205 
206  delete m_pThread;
207  m_pThread=NULL;
208  return E_SUCCESS;
209  }
210  else
211  {
212  CAMsg::printMsg(LOG_ERR,"CAThread - join() not successful - Error was: %i\n",ret);
213  return E_UNKNOWN;
214  }
215 #endif
216 }
signed int SINT32
Definition: basetypedefs.h:132
static SINT32 printMsg(UINT32 typ, const char *format,...)
Writes a given message to the log.
Definition: CAMsg.cpp:251
const SINT32 E_SUCCESS
Definition: errorcodes.hpp:2
#define E_UNKNOWN
Definition: errorcodes.hpp:3

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

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

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setMainLoop()

SINT32 CAThread::setMainLoop ( THREAD_MAIN_TYP  fnc)
inline

Sets the main function which will be executed within this thread.

Parameters
fncthe fuction to be executed
Return values
E_SUCCESS

Definition at line 148 of file CAThread.hpp.

149  {
150  m_fncMainLoop=fnc;
151  return E_SUCCESS;
152  }

References E_SUCCESS, and m_fncMainLoop.

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

Here is the caller graph for this function:

◆ start()

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 115 of file CAThread.cpp.

116  {
117  if(m_fncMainLoop==NULL)
118  return E_UNKNOWN;
119 
120 #ifndef OS_TUDOS
121  m_pThread=new pthread_t;
122 #endif
123 
124  #ifdef DEBUG
125  if(!bSilent)
126  CAMsg::printMsg(LOG_DEBUG, "CAThread::start() - creating thread\n");
127  #endif
128 
129 #ifdef OS_TUDOS
130  if ((m_Thread = l4thread_create(m_fncMainLoop, param, L4THREAD_CREATE_ASYNC)) < 1)
131  {
132  m_Thread = L4THREAD_INVALID_ID;
133  if(!bSilent)
134  CAMsg::printMsg(LOG_ERR, "CAThread::start() - creating new thread failed!\n");
135  return E_UNKNOWN;
136  }
137 #else
138  SINT32 ret=pthread_create(m_pThread,NULL,m_fncMainLoop,param);
139  if(ret!=0)
140  {
141  if(!bSilent)
142  CAMsg::printMsg(LOG_ERR, "CAThread::start() - creating new thread failed! - Err: %i\n",ret);
143  delete m_pThread;
144  m_pThread=NULL;
145  return E_UNKNOWN;
146  }
147  #endif
148 #if defined _DEBUG && !defined(ONLY_LOCAL_PROXY)
149  if(m_pThreadList != NULL)
150  {
151  m_pThreadList->put(this);
152  }
153 
154  else
155  {
156  CAMsg::printMsg(LOG_DEBUG, "CAThread::start() - Warning no thread list found\n");
157  }
158 #endif
159 #ifdef DEBUG
160  if(!bSilent)
161  CAMsg::printMsg(LOG_DEBUG, "CAThread::start() - thread created sucessful\n");
162 #endif
163 
164  #ifdef OS_TUDOS
165 
166  if(m_strName!=NULL&&!bSilent)
167  CAMsg::printMsg(LOG_DEBUG,
168  "Thread with name: %s created - pthread_t: "l4util_idfmt"\n",
169  m_strName, l4util_idstr(l4thread_l4_id(m_Thread)));
170 
171  if(bDaemon)
172  CAMsg::printMsg(LOG_ERR, "TODO: Emulate pthread_detach on L4 ?!\n");
173  #else
174  if(m_strName!=NULL&&!bSilent)
175  {
176  UINT8* temp=bytes2hex(m_pThread,sizeof(pthread_t));
177  CAMsg::printMsg(LOG_DEBUG,"Thread with name: %s created - pthread_t: %s\n",m_strName,temp);
178  delete[] temp;
179  temp = NULL;
180  }
181  if(bDaemon)
182  pthread_detach(*m_pThread);
183 #endif
184  return E_SUCCESS;
185  }
UINT8 * bytes2hex(const void *bytes, UINT32 len)
Converts the byte array to a hex string.
Definition: CAUtil.cpp:90

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

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

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ m_fncMainLoop

THREAD_MAIN_TYP CAThread::m_fncMainLoop
private

Definition at line 223 of file CAThread.hpp.

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

◆ m_Id

UINT32 CAThread::m_Id
private

Definition at line 230 of file CAThread.hpp.

Referenced by CAThread(), and getID().

◆ m_pThread

pthread_t* CAThread::m_pThread
private

Definition at line 227 of file CAThread.hpp.

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

◆ m_strName

UINT8* CAThread::m_strName
private

Definition at line 229 of file CAThread.hpp.

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

◆ ms_LastId

UINT32 CAThread::ms_LastId =0
staticprivate

Definition at line 231 of file CAThread.hpp.

Referenced by CAThread().


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