Open
Graph Drawing
Framework

 v.2012.05
 

Thread.h
Go to the documentation of this file.
00001 /*
00002  * $Revision: 2299 $
00003  * 
00004  * last checkin:
00005  *   $Author: gutwenger $ 
00006  *   $Date: 2012-05-07 15:57:08 +0200 (Mon, 07 May 2012) $ 
00007  ***************************************************************/
00008  
00043 #ifdef _MSC_VER
00044 #pragma once
00045 #endif
00046 
00047 
00048 #ifndef OGDF_THREAD_H
00049 #define OGDF_THREAD_H
00050 
00051 #include <ogdf/basic/basic.h>
00052 
00053 #ifdef OGDF_SYSTEM_WINDOWS
00054 #include <process.h>
00055 #else
00056 #include <pthread.h>
00057 #endif
00058 
00059 namespace ogdf {
00060 
00061 #ifdef OGDF_SYSTEM_WINDOWS
00062 
00063 class Thread
00064 {
00065 public:
00066     enum State { tsRunning, tsSuspended };
00067     enum Priority {
00068         tpIdle     = -15,
00069         tpLowest   = -2,
00070         tpLow      = -1,
00071         tpNormal   = 0,
00072         tpHigh     = 1,
00073         tpHighest  = 2,
00074         tpCritical = 15
00075     };
00076 
00077     Thread() : m_handle(0), m_id(0) { }
00078     ~Thread() { CloseHandle(m_handle); }
00079 
00080     bool started() const { return m_id != 0; }
00081 
00082     void priority(Priority p) { SetThreadPriority(m_handle,p); }
00083 
00084     Priority priority() const { return (Priority)GetThreadPriority(m_handle); }
00085 
00086     __uint64 cpuAffinity(__uint64 mask) {
00087         return SetThreadAffinityMask(m_handle, (DWORD_PTR)mask);
00088     }
00089 
00090     void start(State state = tsRunning) {
00091         if(m_handle)
00092             CloseHandle(m_handle);
00093 
00094         m_handle = (HANDLE) _beginthreadex(0, 0, threadProc, this,
00095             (state == tsSuspended) ? CREATE_SUSPENDED : 0, &m_id);
00096     }
00097 
00098     long threadID() const { return (long)m_id; }
00099 
00100     void start(Priority p, State state = tsRunning) {
00101         OGDF_ASSERT(m_handle == 0);
00102         m_handle = (HANDLE) _beginthreadex(0, 0, threadProc, this, CREATE_SUSPENDED, &m_id);
00103         SetThreadPriority(m_handle,p);
00104         if(state == tsRunning)
00105             ResumeThread(m_handle);
00106     }
00107 
00108     int suspend() {
00109         return SuspendThread(m_handle);
00110     }
00111 
00112     int resume() {
00113         return ResumeThread(m_handle);
00114     }
00115 
00116     void join() {
00117         WaitForSingleObject(m_handle,INFINITE);
00118     }
00119 
00120     bool join(unsigned long milliseconds) {
00121         return (WaitForSingleObject(m_handle,milliseconds) == WAIT_OBJECT_0);
00122     }
00123 
00124 protected:
00125     virtual void doWork() = 0;
00126 
00127 private:
00128     static unsigned int __stdcall threadProc(void *pParam) {
00129         Thread *pThread = static_cast<Thread*>(pParam);
00130         OGDF_ALLOCATOR::initThread();
00131         pThread->doWork();
00132         OGDF_ALLOCATOR::flushPool();
00133         pThread->m_id = 0;
00134         _endthreadex(0);
00135         return 0;
00136     }
00137 
00138     HANDLE m_handle;
00139     unsigned int m_id;
00140 };
00141 
00142 
00143 #else
00144 
00145 class Thread
00146 {
00147 public:
00148     enum State { tsRunning, tsSuspended };
00149     enum Priority {
00150         tpIdle     = -15,
00151         tpLowest   = -2,
00152         tpLow      = -1,
00153         tpNormal   = 0,
00154         tpHigh     = 1,
00155         tpHighest  = 2,
00156         tpCritical = 15
00157     };
00158 
00159     Thread() : m_pt(0) { }
00160 
00161     bool started() const { return m_pt != 0; }
00162 
00163     //void priority(Priority p) { SetThreadPriority(m_handle,p); }
00164 
00165     //Priority priority() const { return (Priority)GetThreadPriority(m_handle); }
00166 
00167     //__uint64 cpuAffinity(__uint64 mask) {
00168     //  return SetThreadAffinityMask(m_handle, (DWORD_PTR)mask);
00169     //}
00170 
00171     void start(State state = tsRunning) {
00172         OGDF_ASSERT(m_pt == 0);
00173         pthread_create(&m_pt, NULL, threadProc, this);
00174     }
00175 
00176 //#ifdef OGDF_SYSTEM_OSX
00177     long threadID() const {
00178         return (long)m_pt;
00179     }   
00180 //#else
00181 //  int threadID() const {
00182 //      return (int)m_pt;
00183 //  }
00184 //#endif
00185 
00186     //void start(Priority p, State state = tsRunning) {
00187     //  OGDF_ASSERT(m_handle == 0);
00188     //  m_handle = (HANDLE) _beginthreadex(0, 0, threadProc, this, CREATE_SUSPENDED, 0);
00189     //  SetThreadPriority(m_handle,p);
00190     //  if(state == tsRunning)
00191     //      ResumeThread(m_handle);
00192     //}
00193 
00194     //int suspend() {
00195     //  return SuspendThread(m_handle);
00196     //}
00197 
00198     //int resume() {
00199     //  return ResumeThread(m_handle);
00200     //}
00201 
00202     void join() {
00203         if(m_pt != 0)
00204             pthread_join(m_pt,NULL);
00205     }
00206 
00207     //bool join(unsigned long milliseconds) {
00208     //  return (WaitForSingleObject(m_handle,milliseconds) == WAIT_OBJECT_0);
00209     //}
00210 
00211 protected:
00212     virtual void doWork() = 0;
00213 
00214 private:
00215     static void *threadProc(void *pParam) {
00216         Thread *pThread = static_cast<Thread*>(pParam);
00217         OGDF_ALLOCATOR::initThread();
00218         pThread->doWork();
00219         pthread_exit(NULL);
00220         OGDF_ALLOCATOR::flushPool();
00221         pThread->m_pt = 0;
00222         return 0;
00223     }
00224 
00225     pthread_t m_pt;
00226 };
00227 
00228 #endif
00229 
00230 } // end namespace ogdf
00231 
00232 
00233 #endif