Mixe for Privacy and Anonymity in the Internet
CAThread.hpp
Go to the documentation of this file.
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 #ifndef __CATHREAD__
00029 #define __CATHREAD__
00030 
00031 #ifndef ONLY_LOCAL_PROXY
00032 
00033 #include "CAMsg.hpp"
00034 
00035 class CAThreadList;
00036 
00037 #ifdef PRINT_THREAD_STACK_TRACE
00038   #define INIT_STACK CAThread::METHOD_STACK* _stack
00039   #define SAVE_STACK(methodName, methodPosition) \
00040   _stack = new CAThread::METHOD_STACK; \
00041   _stack->strMethodName = (methodName); \
00042   _stack->strPosition = (methodPosition); \
00043   CAThread::setCurrentStack(_stack)
00044 
00045   #define FINISH_STACK(methodName) SAVE_STACK(methodName, CAThread::METHOD_END)
00046   #define BEGIN_STACK(methodName) SAVE_STACK(methodName, CAThread::METHOD_BEGIN)
00047 #else
00048   #define INIT_STACK
00049   #define BEGIN_STACK(methodName)
00050   #define FINISH_STACK(methodName)
00051   #define SAVE_STACK(methodName, methodPosition)
00052 #endif
00053 
00067 typedef THREAD_RETURN(*THREAD_MAIN_TYP)(void *);
00068 
00070 typedef unsigned long thread_id_t;
00071 
00104 class CAThread
00105   {
00106     public:
00107 #ifdef PRINT_THREAD_STACK_TRACE
00108       struct METHOD_STACK
00109       {
00110         const char* strMethodName;
00111         const char* strPosition;
00112       };
00113 #endif
00114 
00116       CAThread();
00117 
00121       CAThread(const UINT8* strName);
00122       
00123       ~CAThread()
00124         {
00125           #ifdef OS_TUDOS
00126           m_Thread = L4THREAD_INVALID_ID;
00127           #else
00128           delete m_pThread;
00129           m_pThread = NULL;
00130           #endif
00131           delete[] m_strName;
00132           m_strName = NULL;
00133         }
00134       
00135 #ifdef PRINT_THREAD_STACK_TRACE     
00136       static void setCurrentStack(METHOD_STACK* a_stack); 
00137       static METHOD_STACK* getCurrentStack();
00138 #endif      
00139       
00145       SINT32 setMainLoop(THREAD_MAIN_TYP fnc)
00146         {
00147           m_fncMainLoop=fnc;
00148           return E_SUCCESS;
00149         }
00150 
00163       SINT32 start(void* param,bool bDaemon=false,bool bSilent=false);
00164       
00170       SINT32 join();
00171 
00172 /*      SINT32 sleep(UINT32 msSeconds)
00173         {
00174           m_CondVar.lock();
00175           m_CondVar.wait(msSeconds);
00176           m_CondVar.unlock();
00177           return E_SUCCESS;
00178         }
00179 
00180       SINT32 wakeup()
00181         {
00182           m_CondVar.lock();
00183           m_CondVar.unlock();
00184           m_CondVar.signal();
00185           return E_SUCCESS;
00186         }
00187 */
00188       UINT8* getName() const
00189       {
00190         return m_strName;
00191       }
00192 
00193       UINT32 getID() const
00194         {
00195           return m_Id;
00196         }
00197 
00198       static thread_id_t getSelfID()
00199         {
00200           #ifdef _WIN32
00201             return (unsigned long) pthread_self().p;
00202           #else
00203               return (unsigned long) pthread_self();
00204           #endif
00205         }
00206 
00207 #ifdef PRINT_THREAD_STACK_TRACE
00208       static const char* METHOD_BEGIN;
00209       static const char* METHOD_END;
00210 #endif
00211     private:
00212 #ifdef PRINT_THREAD_STACK_TRACE
00213       static void destroyValue(void* a_stack);
00214       static void initKey();
00215     
00216     
00217       static pthread_key_t ms_threadKey; 
00218       static pthread_once_t ms_threadKeyInit;
00219 #endif
00220       THREAD_MAIN_TYP m_fncMainLoop;
00221 #ifdef OS_TUDOS
00222       l4thread_t m_Thread;
00223 #else 
00224       pthread_t* m_pThread;
00225 #endif
00226       UINT8* m_strName; //< a name mostly for debugging purpose...
00227       UINT32 m_Id; // some unique identifier
00228       static UINT32 ms_LastId;
00229 #if defined _DEBUG && ! defined(ONLY_LOCAL_PROXY)
00230       static CAThreadList* m_pThreadList;
00231     public:
00232       static void setThreadList(CAThreadList* pThreadList)
00233         {
00234           m_pThreadList=pThreadList;
00235         }
00236 #endif
00237   };
00238 #endif
00239 
00240 #endif //ONLY_LOCAL_PROXY