Mixe for Privacy and Anonymity in the Internet
CAThread.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2000, The JAP-Team
3 All rights reserved.
4 Redistribution and use in source and binary forms, with or without modification,
5 are permitted provided that the following conditions are met:
6 
7  - Redistributions of source code must retain the above copyright notice,
8  this list of conditions and the following disclaimer.
9 
10  - Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation and/or
12  other materials provided with the distribution.
13 
14  - Neither the name of the University of Technology Dresden, Germany nor the names of its contributors
15  may be used to endorse or promote products derived from this software without specific
16  prior written permission.
17 
18 
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
20 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS
22 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
27 */
28 #ifndef __CATHREAD__
29 #define __CATHREAD__
30 
31 #if !defined ONLY_LOCAL_PROXY || defined INCLUDE_MIDDLE_MIX
32 
33 #include "CAMsg.hpp"
34 
35 class CAThreadList;
36 
37 #ifdef PRINT_THREAD_STACK_TRACE
38  #define INIT_STACK METHOD_STACK* _stack
39  #define SAVE_STACK(methodName, methodPosition) \
40  _stack = new METHOD_STACK; \
41  _stack->strMethodName = (methodName); \
42  _stack->strPosition = (methodPosition); \
43  CAThread::setCurrentStack(_stack)
44 
45  #define FINISH_STACK(methodName) SAVE_STACK(methodName, CAThread::METHOD_END)
46  #define BEGIN_STACK(methodName) SAVE_STACK(methodName, CAThread::METHOD_BEGIN)
47 #else
48  #define INIT_STACK
49  #define BEGIN_STACK(methodName)
50  #define FINISH_STACK(methodName)
51  #define SAVE_STACK(methodName, methodPosition)
52 #endif
53 
67 typedef THREAD_RETURN(*THREAD_MAIN_TYP)(void *);
68 
70 typedef unsigned long long thread_id_t;
71 
72 #ifdef PRINT_THREAD_STACK_TRACE
73  struct __METHOD_STACK__
74  {
75  const char* strMethodName;
76  const char* strPosition;
77  };
78 
79  typedef struct __METHOD_STACK__ METHOD_STACK;
80 #endif
81 
114 class CAThread
115  {
116  public:
119  CAThread();
120 
124  CAThread(const UINT8* strName);
125 
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  }
137 
138 #ifdef PRINT_THREAD_STACK_TRACE
139  static void setCurrentStack(METHOD_STACK* a_stack);
140  static METHOD_STACK* getCurrentStack();
141 #endif
142 
149  {
150  m_fncMainLoop=fnc;
151  return E_SUCCESS;
152  }
153 
166  SINT32 start(void* param,bool bDaemon=false,bool bSilent=false);
167 
173  SINT32 join();
174 
175 /* SINT32 sleep(UINT32 msSeconds)
176  {
177  m_CondVar.lock();
178  m_CondVar.wait(msSeconds);
179  m_CondVar.unlock();
180  return E_SUCCESS;
181  }
182 
183  SINT32 wakeup()
184  {
185  m_CondVar.lock();
186  m_CondVar.unlock();
187  m_CondVar.signal();
188  return E_SUCCESS;
189  }
190 */
191  UINT8* getName() const
192  {
193  return m_strName;
194  }
195 
196  UINT32 getID() const
197  {
198  return m_Id;
199  }
200 
202  {
203  #ifdef _WIN32
204  return (thread_id_t)pthread_self().p;
205  #else
206  return (thread_id_t)pthread_self();
207  #endif
208  }
209 
210 #ifdef PRINT_THREAD_STACK_TRACE
211  static const char* METHOD_BEGIN;
212  static const char* METHOD_END;
213 #endif
214  private:
215 #ifdef PRINT_THREAD_STACK_TRACE
216  static void destroyValue(void* a_stack);
217  static void initKey();
218 
219 
220  static pthread_key_t ms_threadKey;
221  static pthread_once_t ms_threadKeyInit;
222 #endif
224 #ifdef OS_TUDOS
225  l4thread_t m_Thread;
226 #else
227  pthread_t* m_pThread;
228 #endif
229  UINT8* m_strName; //< a name mostly for debugging purpose...
230  UINT32 m_Id; // some unique identifier
232 #if defined _DEBUG && ! defined(ONLY_LOCAL_PROXY)
233  static CAThreadList* m_pThreadList;
234  public:
235  static void setThreadList(CAThreadList* pThreadList)
236  {
237  m_pThreadList=pThreadList;
238  }
239 #endif
240  };
241 #endif
242 
243 #endif //ONLY_LOCAL_PROXY
THREAD_RETURN(* THREAD_MAIN_TYP)(void *)
Defines the type of the main function of the thread.
Definition: CAThread.hpp:67
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
#define THREAD_RETURN
Definition: StdAfx.h:540
signed int SINT32
Definition: basetypedefs.h:132
unsigned char UINT8
Definition: basetypedefs.h:135
unsigned int UINT32
Definition: basetypedefs.h:131
static UINT32 ms_LastId
Definition: CAThread.hpp:231
THREAD_MAIN_TYP m_fncMainLoop
Definition: CAThread.hpp:223
CAThread()
Creates a CAThread object but no actual thread.
Definition: CAThread.cpp:54
UINT32 m_Id
Definition: CAThread.hpp:230
UINT8 * getName() const
Definition: CAThread.hpp:191
UINT8 * m_strName
Definition: CAThread.hpp:229
pthread_t * m_pThread
Definition: CAThread.hpp:227
UINT32 getID() const
Definition: CAThread.hpp:196
static thread_id_t getSelfID()
Definition: CAThread.hpp:201
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
const SINT32 E_SUCCESS
Definition: errorcodes.hpp:2