00001 #ifndef __J2K__Basic_PThread_HPP__ 00002 #define __J2K__Basic_PThread_HPP__ 00003 00004 /************************************************************************* 00005 ** FILE NAME : Basic_PThread.hpp ** 00006 ** CLASS NAME : Basic_PThread ** 00007 ** DESCRIPTION : This class allows the creation of Generic Thread ** 00008 ** which takes care of the internals ** 00009 ** of the PThread features. Any other subsequent ** 00010 ** classes can use this thread, or can appear as thread ** 00011 ** just by inheriting from this basic class. ** 00012 ** just by inheriting from this basic class. ** 00013 ** just by inheriting from this basic class. ** 00014 *************************************************************************/ 00015 00016 00017 // Default Standard header file 00018 #include <j2k/Fred/Standard.hpp> 00019 00020 //#include <j2k/Fred/Error/JErrorController.hpp> 00021 00022 #include <errno.h> 00023 00024 #define Error 1 00025 #define MC_OnError( x, y, z ) 00026 00027 00028 #include <pthread.h> 00029 #include <unistd.h> 00030 00031 #if defined( __sun ) || defined( __SUN__ ) 00032 #include <thread.h> 00033 #endif 00034 00035 // #include <process.h> 00036 00037 #include <sched.h> 00038 00039 #include <time.h> 00040 #include <signal.h> 00041 #include <sys/types.h> 00042 00043 #define pid_t pthread_t 00044 00045 #define INVALID_PID ( 255 ) 00046 00047 /************************************************************** 00048 *************************************************************** 00049 *** Attribute Default Value *** 00050 *** ========= ============= *** 00051 *** detachstate PTHREAD_CREATE_JOINABLE *** 00052 *** schedpolicy PTHREAD_INHERIT_SCHED *** 00053 *** schedparam Inherited from parent thread *** 00054 *** contentionscope PTHREAD_SCOPE_SYSTEM *** 00055 *** stacksize 4096 *** 00056 *** stackaddr NULL *** 00057 *************************************************************** 00058 **************************************************************/ 00059 00060 class Basic_PThread { 00061 public: 00062 00063 // Default Constructor 00064 inline Basic_PThread( BOOL autostart = FALSE, BOOL autojoin = FALSE ); 00065 00066 // Destructor 00067 inline virtual ~Basic_PThread(); 00068 00069 private: 00070 // Not implemented. 00071 inline Basic_PThread( const Basic_PThread& src ); 00072 inline const Basic_PThread& operator=( const Basic_PThread& src ); 00073 00074 public: 00075 // Get calling thread's ID 00076 inline pid_t getPID(); 00077 00078 00079 // Implement this, when deriving the class. 00080 // It should be overriden by it's children... 00081 virtual void run() = 0; 00082 00083 00084 // Callback can only call STATIC functions ! 00085 // ========================================= 00086 // This routine is a stub which will call 00087 // the run() routine given the class instance 00088 // as the void pointer argument. 00089 00090 static void* Basic_PThread::Wrapper( void* ptrThis ) 00091 { 00092 Basic_PThread* me = (Basic_PThread*)ptrThis; 00093 00094 // Call the derived classes version, since the function 00095 // run is implemented by it's children 00096 me->run(); 00097 00098 // run() should contain an Infinite Loop, i.e. for(;;) { ... } 00099 // Exit beautifully, just in case there is no infinite loop. 00100 00101 me->pid = INVALID_PID; 00102 pthread_exit( NULL ); 00103 00104 return NULL; 00105 } 00106 00107 // Start the PThread with the given priority 00108 // inline void startrel( int p ) 00109 // { 00110 // start( getprio( 0 ) + p ); 00111 // } 00112 00113 void start( int start_prio = 10 ); 00114 00115 // Let other threads run, I'm done for now... via sched_yield() 00116 inline void yield(); 00117 00118 // Cancel a PThread and make it invalid. 00119 inline void stop(); 00120 00121 // Exit a PThread 00122 inline void end(); 00123 00124 // Is this PThread started ? 00125 inline BOOL isStarted(); 00126 00127 // Join PThread 00128 inline void join( void** value_ptr = NULL ); 00129 00130 // Detach a Join PThread 00131 inline void detach(); 00132 00133 protected: 00134 // So, children can access easily the PID 00135 pid_t pid; 00136 BOOL isDetached; 00137 pthread_attr_t attr; 00138 sched_param param; 00139 int priority; 00140 }; 00141 00142 #include <j2k/nto/Basic_PThread.inl> 00143 00144 #endif