Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

vuThread.cpp

Go to the documentation of this file.
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 void* _ret_kickoff(void *ptr)
00034 {
00035   vuThread *th = (vuThread*)ptr;
00036   int whatsup = th->m_Whatsup;
00037   th->m_WhatsupMutex.unlock();
00038 
00039   return th->retrun(whatsup);
00040 
00041 //  return NULL;
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 //  cout << "here" << endl;
00056 
00057   pthread_attr_setdetachstate (&tattr, PTHREAD_CREATE_DETACHED);
00058 
00059 //  cout << "s1" << endl;
00060 
00061   int t = pthread_create(thread, NULL, &_kickoff, (void*) this);
00062 
00063   if (t != 0)
00064 
00065   {
00066 //      cout << "s2.5" << endl;
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 //  cout << "s3" << endl;
00077 
00078   return true;
00079 }
00080 
00081 /*bool vuThread::retStartThread(int whatsup)
00082 
00083 {
00084   pthread_t *thread = new pthread_t [1];
00085 
00086   m_WhatsupMutex.lock();
00087   m_Whatsup = whatsup;
00088 
00089   int t = pthread_create(thread, NULL, &_ret_kickoff, (void*) this);
00090 
00091   if (t != 0)
00092 
00093   {
00094         cout << strerror (errno) << endl;
00095         if (t == EAGAIN)
00096                 cout << "eagain" << endl;
00097 
00098         return false;
00099   }
00100 
00101   pthread_detach (*thread);
00102 
00103   return true;
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         //mutex = (void*)CreateMutex( NULL, FALSE, NULL );
00147         
00148         //mutex = (void*)new CRITICAL_SECTION;
00149         //InitializeCriticalSection((CRITICAL_SECTION*)mutex);
00150 
00151         //mutex = (void*)CreateSemaphore(NULL, 1, 0x7fffffff, NULL);
00152 
00153         mutex = (void*)CreateEvent(NULL,FALSE,TRUE,NULL);
00154 }
00155 
00156 vuMutex::~vuMutex()
00157 {
00158         if(mutex) {
00159                 //DeleteCriticalSection((CRITICAL_SECTION*)mutex);
00160                 //delete (CRITICAL_SECTION*)mutex;
00161                 CloseHandle((HANDLE)mutex);
00162                 mutex = NULL;
00163         }
00164 }
00165 
00166 void vuMutex::lock()
00167 {
00168         WaitForSingleObject( (HANDLE)mutex, INFINITE );
00169         //EnterCriticalSection((CRITICAL_SECTION*)mutex);
00170 }
00171 
00172 bool vuMutex::trylock()
00173 {
00174   return WaitForSingleObject( (HANDLE)mutex, 1) != WAIT_TIMEOUT;
00175 //      return false;
00176 }
00177 
00178 void vuMutex::unlock()
00179 {
00180         //ReleaseSemaphore( (HANDLE)mutex, 1, NULL );
00181         //LeaveCriticalSection((CRITICAL_SECTION*)mutex);
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         //HANDLE thread = CreateThread(NULL, 0, _kickoff, this, 0, NULL);
00204         while(m_Whatsup == -1);
00205 
00206         m_WhatsupMutex.lock();
00207         m_WhatsupMutex.unlock();
00208         if (thread == (unsigned long)(-1))
00209         {
00210 //      cout << "s2.5" << endl;
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         //_endthread();
00224 }
00225 
00226 #endif

Generated on Wed Dec 15 21:20:38 2004 for vuVolume by  doxygen 1.3.9.1