CLAM-Development
1.1
|
00001 /* 00002 * Copyright (c) 2004 MUSIC TECHNOLOGY GROUP (MTG) 00003 * UNIVERSITAT POMPEU FABRA 00004 * 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 * 00020 */ 00021 00022 #ifndef __THREAD__ 00023 #define __THREAD__ 00024 00025 #include <pthread.h> 00026 #include "CBL.hxx" 00027 #include "Condition.hxx" 00028 00029 // there is a Yield method defined in a Windows API which is included by windows.h 00030 // this undefines the Windows Yield method 00031 #ifdef Yield 00032 #undef Yield 00033 #endif 00034 00035 namespace CLAM 00036 { 00037 class Condition; 00038 00039 class Thread 00040 { 00041 public: 00042 00043 Thread(bool realtime = false); 00044 00045 virtual ~Thread(); 00046 00047 virtual void Start(); 00048 00049 virtual void Stop(); 00050 00051 void Sleep(); 00052 00053 void Sleep( unsigned int milliseconds ); 00054 00055 void WakeUp(); 00056 00057 void Yield(); 00058 00059 void SetThreadCode( const CBL::Functor0& thread_code ); 00060 00061 void SetCleanupCode( const CBL::Functor0& cleanup_code ); 00062 00063 bool operator==( const Thread& other ) const; 00064 00065 inline bool IsCancelled() const 00066 { 00067 return mIsCancelled; 00068 } 00069 00070 inline bool IsRunning() const 00071 { 00072 return mRunning; 00073 } 00074 00075 protected: 00076 virtual void SetupPriorityPolicy(); 00077 00078 bool mRealtime; 00079 bool mHasCode; 00080 bool mHasCleanup; 00081 pthread_t mThreadID; 00082 bool mIsCancelled; 00083 bool mRunning; 00084 CBL::Functor0 mThreadCode; 00085 CBL::Functor0 mCleanUpCode; 00086 Condition mSleepCondition; 00087 00088 private: 00089 00090 void SleepDetail( unsigned int millisecs ); 00091 00092 // Pthreads needed static functions for launching and cleaning 00093 00094 static void* LaunchThread( void* pSelf ); 00095 00096 static void LaunchThreadCleanup( void* pSelf ); 00097 }; 00098 00099 } 00100 00101 #endif // Thread.hxx 00102