|
Mixe for Privacy and Anonymity in the Internet
|
00001 /* 00002 Copyright (c) 2000, The JAP-Team 00003 All rights reserved. 00004 Redistribution and use in source and binary forms, with or without modification, 00005 are permitted provided that the following conditions are met: 00006 00007 - Redistributions of source code must retain the above copyright notice, 00008 this list of conditions and the following disclaimer. 00009 00010 - Redistributions in binary form must reproduce the above copyright notice, 00011 this list of conditions and the following disclaimer in the documentation and/or 00012 other materials provided with the distribution. 00013 00014 - Neither the name of the University of Technology Dresden, Germany nor the names of its contributors 00015 may be used to endorse or promote products derived from this software without specific 00016 prior written permission. 00017 00018 00019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS 00020 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 00021 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS 00022 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00023 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 00024 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 00025 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00026 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE 00027 */ 00028 #include "StdAfx.h" 00029 #ifndef ONLY_LOCAL_PROXY 00030 #include "CAThread.hpp" 00031 #include "CAUtil.hpp" 00032 #include "CAMsg.hpp" 00033 #ifdef _DEBUG 00034 #include "CAThreadList.hpp" 00035 #endif 00036 00037 #ifdef OS_TUDOS 00038 const int l4thread_max_threads = 64; 00039 #endif 00040 00041 #ifdef PRINT_THREAD_STACK_TRACE 00042 pthread_once_t CAThread::ms_threadKeyInit = PTHREAD_ONCE_INIT; 00043 pthread_key_t CAThread::ms_threadKey; 00044 const char* CAThread::METHOD_BEGIN = "Begin of method"; 00045 const char* CAThread::METHOD_END = "End of method"; 00046 #endif 00047 00048 #if defined (DEBUG) && ! defined(ONLY_LOCAL_PROXY) 00049 CAThreadList* CAThread::m_pThreadList=NULL; 00050 #endif 00051 00052 UINT32 CAThread::ms_LastId=0; 00053 00054 CAThread::CAThread() 00055 { 00056 m_fncMainLoop=NULL; 00057 #ifdef OS_TUDOS 00058 m_Thread=L4THREAD_INVALID_ID; 00059 #ifdef PRINT_THREAD_STACK_TRACE 00060 assert(ms_threadKey != L4_ENOKEY); 00061 #endif //PRINT_THREAD_STACK_TRACE 00062 #else 00063 m_pThread=NULL; 00064 #endif 00065 m_strName=NULL; 00066 m_Id=ms_LastId; 00067 ms_LastId++; 00068 } 00069 00070 CAThread::CAThread(const UINT8* strName) 00071 { 00072 m_fncMainLoop=NULL; 00073 #ifdef OS_TUDOS 00074 m_Thread=L4THREAD_INVALID_ID; 00075 #else 00076 m_pThread=NULL; 00077 #endif 00078 m_strName=NULL; 00079 if(strName!=NULL) 00080 { 00081 UINT32 len=strlen((char*)strName); 00082 m_strName=new UINT8[len+1]; 00083 memcpy(m_strName,strName,len); 00084 m_strName[len]=0; 00085 } 00086 m_Id=ms_LastId; 00087 ms_LastId++; 00088 } 00089 #ifdef PRINT_THREAD_STACK_TRACE 00090 void CAThread::destroyValue(void* a_value) 00091 { 00092 delete a_value; 00093 a_value = NULL; 00094 } 00095 00096 void CAThread::initKey() 00097 { 00098 pthread_key_create(&ms_threadKey, destroyValue); 00099 } 00100 00101 void CAThread::setCurrentStack(METHOD_STACK* a_value) 00102 { 00103 pthread_once(&ms_threadKeyInit, initKey); 00104 void *value = pthread_getspecific(ms_threadKey); 00105 00106 delete value; 00107 value = a_value; 00108 pthread_setspecific(ms_threadKey, value); 00109 } 00110 00111 CAThread::METHOD_STACK* CAThread::getCurrentStack() 00112 { 00113 pthread_once(&ms_threadKeyInit, initKey); 00114 return (METHOD_STACK*)pthread_getspecific(ms_threadKey); 00115 } 00116 #endif 00117 00118 SINT32 CAThread::start(void* param,bool bDaemon,bool bSilent) 00119 { 00120 if(m_fncMainLoop==NULL) 00121 return E_UNKNOWN; 00122 00123 #ifndef OS_TUDOS 00124 m_pThread=new pthread_t; 00125 #endif 00126 00127 #ifdef DEBUG 00128 if(!bSilent) 00129 CAMsg::printMsg(LOG_DEBUG, "CAThread::start() - creating thread\n"); 00130 #endif 00131 00132 #ifdef OS_TUDOS 00133 if ((m_Thread = l4thread_create(m_fncMainLoop, param, L4THREAD_CREATE_ASYNC)) < 1) 00134 { 00135 m_Thread = L4THREAD_INVALID_ID; 00136 if(!bSilent) 00137 CAMsg::printMsg(LOG_ERR, "CAThread::start() - creating new thread failed!\n"); 00138 return E_UNKNOWN; 00139 } 00140 #else 00141 SINT32 ret=pthread_create(m_pThread,NULL,m_fncMainLoop,param); 00142 if(ret!=0) 00143 { 00144 if(!bSilent) 00145 CAMsg::printMsg(LOG_ERR, "CAThread::start() - creating new thread failed! - Err: %i\n",ret); 00146 delete m_pThread; 00147 m_pThread=NULL; 00148 return E_UNKNOWN; 00149 } 00150 #endif 00151 #ifdef _DEBUG 00152 if(m_pThreadList != NULL) 00153 { 00154 m_pThreadList->put(this); 00155 } 00156 00157 else 00158 { 00159 CAMsg::printMsg(LOG_DEBUG, "CAThread::start() - Warning no thread list found\n"); 00160 } 00161 #endif 00162 #ifdef DEBUG 00163 if(!bSilent) 00164 CAMsg::printMsg(LOG_DEBUG, "CAThread::start() - thread created sucessful\n"); 00165 #endif 00166 00167 #ifdef OS_TUDOS 00168 00169 if(m_strName!=NULL&&!bSilent) 00170 CAMsg::printMsg(LOG_DEBUG, 00171 "Thread with name: %s created - pthread_t: "l4util_idfmt"\n", 00172 m_strName, l4util_idstr(l4thread_l4_id(m_Thread))); 00173 00174 if(bDaemon) 00175 CAMsg::printMsg(LOG_ERR, "TODO: Emulate pthread_detach on L4 ?!\n"); 00176 #else 00177 if(m_strName!=NULL&&!bSilent) 00178 { 00179 UINT8* temp=bytes2hex(m_pThread,sizeof(pthread_t)); 00180 CAMsg::printMsg(LOG_DEBUG,"Thread with name: %s created - pthread_t: %s\n",m_strName,temp); 00181 delete[] temp; 00182 temp = NULL; 00183 } 00184 if(bDaemon) 00185 pthread_detach(*m_pThread); 00186 #endif 00187 return E_SUCCESS; 00188 } 00189 00190 SINT32 CAThread::join() 00191 { 00192 #ifdef OS_TUDOS 00193 CAMsg::printMsg(LOG_ERR,"CAThread - join() L4 implement me !\n"); 00194 if(m_Thread==L4THREAD_INVALID_ID) 00195 return E_SUCCESS; 00196 00197 return E_UNKNOWN; 00198 #else 00199 if(m_pThread==NULL) 00200 return E_SUCCESS; 00201 SINT32 ret=pthread_join(*m_pThread,NULL); 00202 if(ret==0) 00203 { 00204 #ifdef DEBUG 00205 CAMsg::printMsg(LOG_DEBUG,"CAThread %s - join() successful\n", m_strName); 00206 m_pThreadList->remove(this); 00207 #endif 00208 00209 delete m_pThread; 00210 m_pThread=NULL; 00211 return E_SUCCESS; 00212 } 00213 else 00214 { 00215 CAMsg::printMsg(LOG_ERR,"CAThread - join() not successful - Error was: %i\n",ret); 00216 return E_UNKNOWN; 00217 } 00218 #endif 00219 } 00220 #endif //ONLY_LOCAL_PROXY
1.7.6.1