Marsyas  0.5.0-beta1
/Users/jleben/code/marsyas/src/marsyas/Thread.cpp
Go to the documentation of this file.
00001 /***************************************************/
00012 /***************************************************/
00013 
00014 
00015 #include <marsyas/common_source.h>
00016 
00017 #if (defined(MARSYAS_LINUX) || defined(MARSYAS_CYGWIN) || defined(MARSYAS_MACOSX))
00018 #include <pthread.h>
00019 #define THREAD_TYPE
00020 typedef pthread_t THREAD_HANDLE;
00021 typedef void * THREAD_RETURN;
00022 typedef void * (*THREAD_FUNCTION)(void *);
00023 typedef pthread_mutex_t MUTEX;
00024 #endif
00025 
00026 
00027 
00028 #if defined(MARSYAS_WIN32)
00029 
00030 #define _WINSOCKAPI_
00031 
00032 #ifndef NOMINMAX
00033 #define NOMINMAX
00034 #endif
00035 
00036 #include <windows.h>
00037 #include <process.h>
00038 #define THREAD_TYPE __stdcall
00039 typedef unsigned long THREAD_HANDLE;
00040 typedef unsigned THREAD_RETURN;
00041 typedef unsigned (__stdcall *THREAD_FUNCTION)(void *);
00042 typedef CRITICAL_SECTION MUTEX;
00043 
00044 #endif
00045 
00046 
00047 #if defined(MARSYAS_MINGW)
00048 
00049 #define _WINSOCKAPI_
00050 
00051 #ifndef NOMINMAX
00052 #define NOMINMAX
00053 #endif
00054 
00055 #include <windows.h>
00056 #include <process.h>
00057 #define THREAD_TYPE __stdcall
00058 typedef unsigned long THREAD_HANDLE;
00059 typedef unsigned THREAD_RETURN;
00060 typedef unsigned (__stdcall *THREAD_FUNCTION)(void *);
00061 typedef CRITICAL_SECTION MUTEX;
00062 
00063 #endif
00064 
00065 
00066 
00067 #include "Thread.h"
00068 
00069 using namespace Marsyas;
00070 
00071 
00072 Thread :: Thread()
00073 {
00074   thread = 0;
00075 }
00076 
00077 Thread :: ~Thread()
00078 {
00079 #if (defined(MARSYAS_CYGWIN) || defined(MARSYAS_CYGWIN) || defined(MARSYAS_MACOSX))
00080 
00081   pthread_cancel(thread);
00082   pthread_join(thread, NULL);
00083 
00084 #elif defined(MARSYAS_WIN32)
00085 
00086   if ( thread )
00087     TerminateThread((HANDLE)thread, 0);
00088 
00089 #endif
00090 }
00091 
00092 bool Thread :: start( THREAD_FUNCTION routine, void * ptr )
00093 {
00094   bool result = false;
00095 #if (defined(MARSYAS_CYGWIN) || defined(MARSYAS_LINUX) || defined(MARSYAS_MACOSX))
00096 
00097   if ( pthread_create(&thread, NULL, *routine, ptr) == 0 )
00098     result = true;
00099 
00100 #elif defined(MARSYAS_WIN32)
00101   unsigned thread_id;
00102   thread = _beginthreadex(NULL, 0, routine, ptr, 0, &thread_id);
00103   if ( thread ) result = true;
00104 
00105 #else
00106   (void) routine;
00107   (void) ptr;
00108 
00109 #endif
00110   return result;
00111 }
00112 
00113 bool Thread :: wait( long milliseconds )
00114 {
00115   bool result = false;
00116 #if (defined(MARSYAS_CYGWIN) || defined(MARSYAS_LINUX) || defined(MARSYAS_MACOSX))
00117 
00118   (void) milliseconds;
00119   pthread_cancel(thread);
00120   pthread_join(thread, NULL);
00121 
00122 #elif defined(MARSYAS_WIN32)
00123 
00124   DWORD timeout, retval;
00125   if ( milliseconds < 0 ) timeout = INFINITE;
00126   else timeout = milliseconds;
00127   retval = WaitForSingleObject( (HANDLE)thread, timeout );
00128   if ( retval == WAIT_OBJECT_0 ) {
00129     CloseHandle( (HANDLE)thread );
00130     thread = 0;
00131     result = true;
00132   }
00133 
00134 #endif
00135   return result;
00136 }
00137 
00138 void Thread :: test(void)
00139 {
00140 #if (defined(MARSYAS_CYGWIN) || defined(MARSYAS_CYGWIN) || defined(MARSYAS_MACOSX))
00141 
00142   pthread_testcancel();
00143 
00144 #endif
00145 }
00146 
00147 
00148 Mutex :: Mutex()
00149 {
00150 
00151 #if (defined(MARSYAS_CYGWIN) || defined(MARSYAS_CYGWIN) || defined(MARSYAS_MACOSX))
00152 
00153   pthread_mutex_init(&mutex, NULL);
00154 
00155 #elif defined(MARSYAS_WIN32)
00156 
00157   InitializeCriticalSection(&mutex);
00158 
00159 #endif
00160 }
00161 
00162 Mutex :: ~Mutex()
00163 {
00164 #if (defined(MARSYAS_CYGWIN) || defined(MARSYAS_CYGWIN) || defined(MARSYAS_MACOSX))
00165 
00166   pthread_mutex_destroy(&mutex);
00167 
00168 #elif defined(MARSYAS_WIN32)
00169 
00170   DeleteCriticalSection(&mutex);
00171 
00172 #endif
00173 }
00174 
00175 void Mutex :: lock()
00176 {
00177 #if (defined(MARSYAS_CYGWIN) || defined(MARSYAS_CYGWIN) || defined(MARSYAS_MACOSX))
00178 
00179   pthread_mutex_lock(&mutex);
00180 
00181 #elif defined(MARSYAS_WIN32)
00182 
00183   EnterCriticalSection(&mutex);
00184 
00185 #endif
00186 }
00187 
00188 void Mutex :: unlock()
00189 {
00190 #if (defined(MARSYAS_CYGWIN) || defined(MARSYAS_CYGWIN) || defined(MARSYAS_MACOSX))
00191 
00192   pthread_mutex_unlock(&mutex);
00193 
00194 #elif defined(MARSYAS_WIN32)
00195 
00196   LeaveCriticalSection(&mutex);
00197 
00198 #endif
00199 }