00001 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 #include <stdio.h>
00033 #ifdef _WIN32
00034 #include <process.h>
00035 #include "socket_include.h"
00036 #else
00037 #include <unistd.h>
00038 #endif
00039 
00040 #include "Thread.h"
00041 #include "Utility.h"
00042 
00043 
00044 #ifdef SOCKETS_NAMESPACE
00045 namespace SOCKETS_NAMESPACE {
00046 #endif
00047 
00048 
00049 Thread::Thread(bool release)
00050 :m_thread(0)
00051 ,m_running(true)
00052 ,m_release(false)
00053 ,m_b_delete_on_exit(false)
00054 ,m_b_destructor(false)
00055 {
00056 #ifdef _WIN32
00057 
00058         m_thread = (HANDLE)_beginthreadex(NULL, 0, &StartThread, this, 0, &m_dwThreadId);
00059 #else
00060         pthread_attr_init(&m_attr);
00061         pthread_attr_setdetachstate(&m_attr,PTHREAD_CREATE_DETACHED);
00062         if (pthread_create(&m_thread,&m_attr, StartThread,this) == -1)
00063         {
00064                 perror("Thread: create failed");
00065                 SetRunning(false);
00066         }
00067 #endif
00068         m_release = release;
00069         if (release)
00070                 m_sem.Post();
00071 }
00072 
00073 
00074 Thread::~Thread()
00075 {
00076         m_b_destructor = true;
00077         if (m_running)
00078         {
00079                 SetRelease(true);
00080                 SetRunning(false);
00081                 
00082 
00083 
00084 
00085                 Utility::Sleep(1000);
00086         }
00087 #ifdef _WIN32
00088         if (m_thread)
00089                 ::CloseHandle(m_thread);
00090 #else
00091         pthread_attr_destroy(&m_attr);
00092 #endif
00093 }
00094 
00095 
00096 threadfunc_t STDPREFIX Thread::StartThread(threadparam_t zz)
00097 {
00098         
00099 
00100 
00101 
00102         Utility::Sleep(5);
00103 
00104         Thread *p = (Thread *)zz;
00105 
00106         p -> Wait();
00107         if (p -> m_running)
00108         {
00109                 p -> Run();
00110         }
00111         p -> SetRunning(false); 
00112         if (p -> DeleteOnExit() && !p -> IsDestructor())
00113         {
00114                 delete p;
00115         }
00116 #ifdef _WIN32
00117         _endthreadex(0);
00118 #endif
00119         return (threadfunc_t)NULL;
00120 }
00121 
00122 
00123 bool Thread::IsRunning()
00124 {
00125         return m_running;
00126 }
00127 
00128 
00129 void Thread::SetRunning(bool x)
00130 {
00131         m_running = x;
00132 }
00133 
00134 
00135 bool Thread::IsReleased()
00136 {
00137         return m_release;
00138 }
00139 
00140 
00141 void Thread::SetRelease(bool x)
00142 {
00143         m_release = x;
00144         if (x)
00145                 m_sem.Post();
00146 }
00147 
00148 
00149 bool Thread::DeleteOnExit()
00150 {
00151         return m_b_delete_on_exit;
00152 }
00153 
00154 
00155 void Thread::SetDeleteOnExit(bool x)
00156 {
00157         m_b_delete_on_exit = x;
00158 }
00159 
00160 
00161 bool Thread::IsDestructor()
00162 {
00163         return m_b_destructor;
00164 }
00165 
00166 
00167 void Thread::Wait()
00168 {
00169         m_sem.Wait();
00170 }
00171 
00172 
00173 #ifdef SOCKETS_NAMESPACE
00174 }
00175 #endif
00176 
00177