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

C:/temp/src/j2k/Deprecated/old_18mar_diff/nto/Basic_PThread.inl

Go to the documentation of this file.
00001 #ifndef Basic_PThread_INL
00002 #define Basic_PThread_INL
00003 
00004 /*************************************************************************
00005  **  FILE NAME   : Basic_PThread.inl                                    **
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 #include <j2k/nto/Basic_PThread.hpp>
00017 
00018 /**************************************************************
00019 ***************************************************************
00020 ***  Attribute         Default Value                        ***
00021 ***  =========         =============                        *** 
00022 ***   detachstate       PTHREAD_CREATE_JOINABLE             *** 
00023 ***   schedpolicy       PTHREAD_INHERIT_SCHED               ***
00024 ***   schedparam        Inherited from parent PThread       *** 
00025 ***   contentionscope   PTHREAD_SCOPE_SYSTEM                *** 
00026 ***   stacksize         4096                                *** 
00027 ***   stackaddr         NULL                                ***
00028 ***************************************************************
00029 **************************************************************/
00030 
00031  // Default Constructor
00032  // Assign default values and set PID = -1
00033  // to indicate that no thread was created yet.
00034 
00035  inline Basic_PThread::Basic_PThread( BOOL autostart, BOOL autojoin )
00036   : pid( INVALID_PID ), isDetached( TRUE )
00037  {
00038    if ( autostart ) start();
00039    if ( autojoin  ) join();
00040  }
00041 
00042  // Virtual Destructor
00043  inline Basic_PThread::~Basic_PThread() 
00044  {
00045    stop();
00046    end();
00047  }
00048 
00049  inline void Basic_PThread::stop() {
00050    if ( pid < 0 ) return;
00051  
00052    pthread_cancel( pid );
00053    pid = INVALID_PID;
00054  }
00055 
00056  inline void Basic_PThread::end() {
00057    pthread_exit( NULL );
00058  }
00059 
00060  inline pid_t Basic_PThread::getPID() 
00061  {
00062    return pid;
00063  }
00064 
00065  inline BOOL Basic_PThread::isStarted() 
00066  {
00067    return ( pid >= 0 && (signed long)pid != INVALID_PID );
00068  }
00069 
00070  inline void Basic_PThread::yield() 
00071  {
00072    sched_yield();
00073  }
00074 
00075  inline void Basic_PThread::detach()
00076  {
00077    if ( pid < 0    ) return;
00078    if ( isDetached ) return;
00079     
00080    int err = pthread_detach( pid );
00081    isDetached = TRUE;
00082     
00083    MC_OnError( err, Error, "An error occured while detaching a PThread." );
00084  }
00085 
00086  // Join Basic_PThread
00087  inline void Basic_PThread::join( void** value_ptr )
00088  {
00089    int err = 0;
00090    if ( pid < 0 ) return;
00091    if ( !isDetached )
00092      err = pthread_join( pid, value_ptr );
00093    
00094     isDetached = FALSE;
00095     MC_OnError( err, Error, "An error occured while joining a PThread." );
00096   }
00097 
00098 #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