Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

C:/temp/src/j2k/Posix/Basic_PThread.hpp

Go to the documentation of this file.
00001 // Made by Fred, for Coen445.
00002 // LGPL licensed.
00003 
00004 #ifndef __J2K__Basic_PThread_HPP__
00005 #define __J2K__Basic_PThread_HPP__
00006 
00007 #include <j2k/Fred/Standard.hpp>
00008 #include <j2k/Fred/Error/JErrorController.hpp>
00009 #include <pthread.h>
00010 #include <unistd.h>
00011 
00012 #if defined( __sun ) || defined( __SUN__ )
00013 #include <thread.h>
00014 #endif
00015 // #include <process.h>
00016 
00017 #include <sched.h>
00018 
00019 #include <time.h>
00020 #include <signal.h>
00021 #include <sys/types.h>
00022 
00023 #define  pid_t   pthread_t
00024 
00025 #define  INVALID_PID   ((unsigned)(-1))
00026 
00027 /**************************************************************
00028 ***************************************************************
00029 ***  Attribute         Default Value                        ***
00030 ***  =========         =============                        *** 
00031 ***   detachstate       PTHREAD_CREATE_JOINABLE             *** 
00032 ***   schedpolicy       PTHREAD_INHERIT_SCHED               ***
00033 ***   schedparam        Inherited from parent thread        *** 
00034 ***   contentionscope   PTHREAD_SCOPE_SYSTEM                *** 
00035 ***   stacksize         4096                                *** 
00036 ***   stackaddr         NULL                                ***
00037 ***************************************************************
00038 **************************************************************/
00039 
00040 class Basic_PThread {
00041 // MC_SerializeHeader()
00042 
00043 public:
00044 
00045    // Default Constructor
00046    inline Basic_PThread( BOOL autostart = FALSE, BOOL autojoin = FALSE );
00047 
00048    // Destructor
00049    inline virtual ~Basic_PThread();
00050 
00051 private:
00052    // Not implemented.
00053    inline Basic_PThread( const Basic_PThread& src );
00054    inline const Basic_PThread& operator=( const Basic_PThread& src );
00055 
00056 public:
00057    // Get calling thread's ID
00058    inline pid_t getPID();
00059  
00060    // Implement this, when deriving the class.
00061    // It should be overriden by it's children...
00062    virtual void run() = 0;
00063 
00064    //  Callback can only call STATIC functions !
00065    //  =========================================
00066    //  This routine is a stub which will call
00067    //  the run() routine given the class instance
00068    //  as the void pointer argument.
00069 
00070    static void* Basic_PThread::Wrapper( void* ptrThis ) 
00071    {
00072      Basic_PThread* me = (Basic_PThread*)ptrThis;
00073  
00074      // Call the derived classes version, since the function
00075      // run is implemented by it's children
00076      me->run();  
00077 
00078      // run() should contain an Infinite Loop, i.e. for(;;) { ... }
00079      // Exit beautifully, just in case there is no infinite loop.
00080   
00081      me->pid = (unsigned)(-1); 
00082      pthread_exit( NULL );
00083  
00084      return NULL;
00085    }
00086 
00087    inline void start( int start_prio = 20 );
00088 
00089    inline void yield();
00090 
00091    inline void stop();
00092    inline void end();
00093 
00094    inline BOOL isStarted();
00095 
00096    // Join PThread
00097    inline void join( void** value_ptr = NULL );
00098 
00099    inline void detach();
00100     
00101 protected:
00102   // So, children can access easily the PID
00103   pid_t  pid; 
00104   BOOL   isDetached;
00105   pthread_attr_t attr;
00106   sched_param    param;
00107 };
00108 
00109 
00110 #endif

Generated on Sun Oct 14 18:46:21 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001