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