00001 #include "vuThread.h"
00002 #include <stdlib.h>
00003 #include <iostream.h>
00004 #include <errno.h>
00005 #include <string.h>
00006 #include <fstream.h>
00007 #include "vuSimpleTypes.h"
00008
00009 vuMutex logmut;
00010
00011 vuMutex& getLogMut() {return logmut;}
00012
00013 #ifndef WIN32
00014
00015 #include <sys/time.h>
00016 #include <pthread.h>
00017
00018
00019 void* _kickoff(void* ptr)
00020 {
00021 vuThread *th = (vuThread*)ptr;
00022 int whatsup = th->m_Whatsup;
00023 void* data = th->m_AdditionalData;
00024 th->m_WhatsupMutex.unlock();
00025
00026 th->run(whatsup, data);
00027
00028 th->stopThread();
00029 return NULL;
00030 }
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 bool vuThread::startThread(int whatsup, void* data)
00045
00046 {
00047 pthread_t *thread = new pthread_t;
00048
00049 m_WhatsupMutex.lock();
00050 m_Whatsup = whatsup;
00051 m_AdditionalData = data;
00052
00053 pthread_attr_t tattr;
00054
00055
00056
00057 pthread_attr_setdetachstate (&tattr, PTHREAD_CREATE_DETACHED);
00058
00059
00060
00061 int t = pthread_create(thread, NULL, &_kickoff, (void*) this);
00062
00063 if (t != 0)
00064
00065 {
00066
00067 cout << strerror (errno) << endl;
00068 if (t == EAGAIN)
00069 cout << "eagain" << endl;
00070
00071 return false;
00072 }
00073
00074 pthread_detach (*thread);
00075
00076
00077
00078 return true;
00079 }
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106 void vuThread::stopThread()
00107 {
00108 pthread_exit(NULL);
00109 }
00110
00111 vuMutex::vuMutex()
00112 {
00113 mutex = (void*) new pthread_mutex_t;
00114 pthread_mutex_init((pthread_mutex_t*)mutex, NULL);
00115 }
00116
00117 vuMutex::~vuMutex()
00118 {
00119 pthread_mutex_destroy((pthread_mutex_t*) mutex);
00120 delete (pthread_mutex_t*)mutex;
00121 }
00122
00123 void vuMutex::lock()
00124 {
00125 pthread_mutex_lock( (pthread_mutex_t*) mutex );
00126 }
00127
00128 bool vuMutex::trylock()
00129 {
00130 return pthread_mutex_trylock( (pthread_mutex_t*) mutex ) == 1;
00131 }
00132
00133 void vuMutex::unlock()
00134 {
00135 pthread_mutex_unlock( (pthread_mutex_t*) mutex );
00136 }
00137
00138 #else // WIN32 defined
00139
00140 #include <windows.h>
00141 #include <process.h>
00142 #include <sys/time.h>
00143
00144 vuMutex::vuMutex()
00145 {
00146
00147
00148
00149
00150
00151
00152
00153 mutex = (void*)CreateEvent(NULL,FALSE,TRUE,NULL);
00154 }
00155
00156 vuMutex::~vuMutex()
00157 {
00158 if(mutex) {
00159
00160
00161 CloseHandle((HANDLE)mutex);
00162 mutex = NULL;
00163 }
00164 }
00165
00166 void vuMutex::lock()
00167 {
00168 WaitForSingleObject( (HANDLE)mutex, INFINITE );
00169
00170 }
00171
00172 bool vuMutex::trylock()
00173 {
00174 return WaitForSingleObject( (HANDLE)mutex, 1) != WAIT_TIMEOUT;
00175
00176 }
00177
00178 void vuMutex::unlock()
00179 {
00180
00181
00182 SetEvent((HANDLE)mutex);
00183 }
00184
00185 void _kickoff(void* ptr)
00186 {
00187 vuThread *th = reinterpret_cast<vuThread*>(ptr);
00188 int whatsup = th->m_Whatsup;
00189 void* data = th->m_AdditionalData;
00190 th->m_Whatsup = -1;
00191 th->m_WhatsupMutex.unlock();
00192 th->run(whatsup, data);
00193 return;
00194 }
00195
00196 bool vuThread::startThread(int whatsup, void* data)
00197 {
00198 m_WhatsupMutex.lock();
00199 m_Whatsup = whatsup;
00200 m_AdditionalData = data;
00201
00202 unsigned long thread = _beginthread(_kickoff, 0, (void*)this);
00203
00204 while(m_Whatsup == -1);
00205
00206 m_WhatsupMutex.lock();
00207 m_WhatsupMutex.unlock();
00208 if (thread == (unsigned long)(-1))
00209 {
00210
00211 cout << strerror (errno) << endl;
00212 if (errno == EAGAIN)
00213 cout << "eagain" << endl;
00214
00215 return false;
00216 }
00217
00218 return true;
00219 }
00220
00221 void vuThread::stopThread()
00222 {
00223
00224 }
00225
00226 #endif