Mixe for Privacy and Anonymity in the Internet
CAThread.cpp
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 #include "StdAfx.h"
29 #if !defined ONLY_LOCAL_PROXY || defined INCLUDE_MIDDLE_MIX
30 #include "CAThread.hpp"
31 #include "CAUtil.hpp"
32 #include "CAMsg.hpp"
33 #ifdef _DEBUG
34 #include "CAThreadList.hpp"
35 #endif
36 
37 #ifdef OS_TUDOS
38  const int l4thread_max_threads = 64;
39 #endif
40 
41 #ifdef PRINT_THREAD_STACK_TRACE
42  pthread_once_t CAThread::ms_threadKeyInit = PTHREAD_ONCE_INIT;
43  pthread_key_t CAThread::ms_threadKey;
44  const char* CAThread::METHOD_BEGIN = "Begin of method";
45  const char* CAThread::METHOD_END = "End of method";
46 #endif
47 
48 #if defined (DEBUG) && ! defined(ONLY_LOCAL_PROXY)
49  CAThreadList* CAThread::m_pThreadList=NULL;
50 #endif
51 
53 
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  }
69 
70 CAThread::CAThread(const UINT8* strName)
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  }
89 #ifdef PRINT_THREAD_STACK_TRACE
90 void CAThread::destroyValue(void* a_value)
91 {
92  delete (METHOD_STACK* )a_value;
93 }
94 
95 void CAThread::initKey()
96 {
97  pthread_key_create(&ms_threadKey, destroyValue);
98 }
99 
100 void CAThread::setCurrentStack(METHOD_STACK* a_value)
101 {
102  pthread_once(&ms_threadKeyInit, initKey);
103  METHOD_STACK* value = (METHOD_STACK*)pthread_getspecific(ms_threadKey);
104  delete value;
105  pthread_setspecific(ms_threadKey, a_value);
106 }
107 
108 METHOD_STACK* CAThread::getCurrentStack()
109 {
110  pthread_once(&ms_threadKeyInit, initKey);
111  return (METHOD_STACK*)pthread_getspecific(ms_threadKey);
112 }
113 #endif
114 
115 SINT32 CAThread::start(void* param,bool bDaemon,bool bSilent)
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  }
186 
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 }
217 #endif //ONLY_LOCAL_PROXY
UINT8 * bytes2hex(const void *bytes, UINT32 len)
Converts the byte array to a hex string.
Definition: CAUtil.cpp:90
signed int SINT32
Definition: basetypedefs.h:132
unsigned char UINT8
Definition: basetypedefs.h:135
unsigned int UINT32
Definition: basetypedefs.h:131
static SINT32 printMsg(UINT32 typ, const char *format,...)
Writes a given message to the log.
Definition: CAMsg.cpp:251
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 * m_strName
Definition: CAThread.hpp:229
pthread_t * m_pThread
Definition: CAThread.hpp:227
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 join()
Waits for the main function to finish execution.
Definition: CAThread.cpp:187
const SINT32 E_SUCCESS
Definition: errorcodes.hpp:2
#define E_UNKNOWN
Definition: errorcodes.hpp:3
UINT16 len
Definition: typedefs.hpp:0