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

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