home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 May / PCFMay2001.iso / Xenon / C++ / FreeCommandLineTools.exe / Include / thrdbase.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-31  |  2.2 KB  |  109 lines

  1. #pragma option push -b -a8 -pc -A- /*P_O_Push*/
  2. //***************************************************************************
  3. //
  4. //  Copyright (c) 1997-1999 Microsoft Corporation
  5. //
  6. //  ThrdBase.h
  7. //
  8. //  Purpose: Definition of ThreadBase class
  9. //
  10. //***************************************************************************
  11.  
  12. #if _MSC_VER > 1000
  13. #pragma once
  14. #endif
  15.  
  16. #ifndef __THREADBASE_H__
  17. #define __THREADBASE_H__
  18.  
  19. class POLARITY CThreadBase
  20. {
  21. public:
  22.  
  23.     enum THREAD_SAFETY_MECHANISM
  24.     {
  25.         etsmFirst        = 0,
  26.         etsmSerialized    = 0,
  27.         etsmPriorityRead,
  28.         etsmPriorityWrite,
  29.         etsmLast
  30.     };
  31.  
  32.     // Construction/Destruction
  33.     CThreadBase( THREAD_SAFETY_MECHANISM etsm = etsmSerialized );
  34.     virtual ~CThreadBase();
  35.  
  36.     // Thread Safe Ref/Counting functions
  37.     LONG    AddRef( void );
  38.     LONG    Release( void );
  39.  
  40.     // Provide Readable Read/Write accessors should
  41.     // we not want to serialize at a later date.  Note
  42.     // that timeouts have no meaning unless we're
  43.     // doing a non-serialized implementation.
  44.  
  45.     BOOL    BeginRead( DWORD dwTimeOut = INFINITE );
  46.     void    EndRead( void );
  47.  
  48.     BOOL    BeginWrite( DWORD dwTimeOut = INFINITE );
  49.     void    EndWrite( void );
  50.  
  51. protected:
  52.  
  53.     virtual void    OnFinalRelease( void );
  54.  
  55.     // Thread Safety functions
  56.  
  57.  
  58. private:
  59.  
  60.     CRITICAL_SECTION        m_cs;
  61.     LONG                    m_lRefCount;
  62.     THREAD_SAFETY_MECHANISM    m_etsm;
  63.  
  64.     // Private thread safety functions.  We can maybe promote
  65.     // these to protected if we see a need to later, however
  66.     // for right now, everyone should specify if they mean
  67.     // to read or write when they wish to access data that
  68.     // may change.
  69.  
  70.     void    Lock( void );
  71.     void    Unlock( void );
  72.  
  73. };
  74.  
  75. inline BOOL CThreadBase::BeginRead( DWORD dwTimeout /*=INFINITE*/ )
  76. {
  77.     EnterCriticalSection( &m_cs );
  78.     return TRUE;
  79. }
  80.  
  81. inline void CThreadBase::EndRead( void )
  82. {
  83.     LeaveCriticalSection( &m_cs );
  84. }
  85.  
  86. inline BOOL CThreadBase::BeginWrite( DWORD dwTimeout /*=INFINITE*/ )
  87. {
  88.     EnterCriticalSection( &m_cs );
  89.     return TRUE;
  90. }
  91.  
  92. inline void CThreadBase::EndWrite( void )
  93. {
  94.     LeaveCriticalSection( &m_cs );
  95. }
  96.  
  97. inline void CThreadBase::Lock( void )
  98. {
  99.     EnterCriticalSection( &m_cs );
  100. }
  101.  
  102. inline void CThreadBase::Unlock( void )
  103. {
  104.     LeaveCriticalSection( &m_cs );
  105. }
  106.  
  107. #endif
  108. #pragma option pop /*P_O_Pop*/
  109.