Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
3.1 KiB

  1. //***************************************************************************
  2. //
  3. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  4. //
  5. // ThreadBase.CPP
  6. //
  7. // Purpose: Implementation of CThreadBase class
  8. //
  9. //***************************************************************************
  10. #include "precomp.h"
  11. #include <assertbreak.h>
  12. ////////////////////////////////////////////////////////////////////////
  13. //
  14. // Function: CThreadBase::CThreadBase
  15. //
  16. // Class Constructor.
  17. //
  18. // Inputs: THREAD_SAFETY_MECHANISM etsm - Thread Safety Mechanism.
  19. //
  20. // Outputs: None.
  21. //
  22. // Return: None.
  23. //
  24. // Comments: None.
  25. //
  26. ////////////////////////////////////////////////////////////////////////
  27. CThreadBase::CThreadBase( THREAD_SAFETY_MECHANISM etsm /*=etsmSerialized*/ )
  28. : m_lRefCount( 1 ), // Our initial ref count is always 1
  29. m_etsm( etsm )
  30. {
  31. InitializeCriticalSection( &m_cs ); // Void function, so it best work.
  32. }
  33. ////////////////////////////////////////////////////////////////////////
  34. //
  35. // Function: CThreadBase::~CThreadBase
  36. //
  37. // Class Destructor.
  38. //
  39. // Inputs: None.
  40. //
  41. // Outputs: None.
  42. //
  43. // Return: None.
  44. //
  45. // Comments: None.
  46. //
  47. ////////////////////////////////////////////////////////////////////////
  48. CThreadBase::~CThreadBase( void )
  49. {
  50. DeleteCriticalSection( &m_cs );
  51. }
  52. ////////////////////////////////////////////////////////////////////////
  53. //
  54. // Function: CThreadBase::OnFinalRelease
  55. //
  56. // Virtual function called by Release() when our RefCount reaches 0.
  57. //
  58. // Inputs: None.
  59. //
  60. // Outputs: None.
  61. //
  62. // Return: None.
  63. //
  64. // Comments: Override if you want, but always call down to the base
  65. // implementation and let it call delete on 'this'.
  66. //
  67. ////////////////////////////////////////////////////////////////////////
  68. void CThreadBase::OnFinalRelease( void )
  69. {
  70. delete this;
  71. }
  72. ////////////////////////////////////////////////////////////////////////
  73. //
  74. // Function: CThreadBase::AddRef
  75. //
  76. // Increases our Reference count by one.
  77. //
  78. // Inputs: None.
  79. //
  80. // Outputs: None.
  81. //
  82. // Return: None.
  83. //
  84. // Comments: Uses Lock(), Unlock() to protect the data. We may want
  85. // to change the function to use InterlockedIncrement().
  86. //
  87. ////////////////////////////////////////////////////////////////////////
  88. LONG CThreadBase::AddRef( void )
  89. {
  90. LONG nRet = InterlockedIncrement(&m_lRefCount);
  91. return nRet;
  92. }
  93. ////////////////////////////////////////////////////////////////////////
  94. //
  95. // Function: CThreadBase::Release
  96. //
  97. // Decreases our Reference count by one.
  98. //
  99. // Inputs: None.
  100. //
  101. // Outputs: None.
  102. //
  103. // Return: None.
  104. //
  105. // Comments: Uses Lock(), Unlock() to protect the data. We may want
  106. // to use InterlockedDecrement();
  107. //
  108. ////////////////////////////////////////////////////////////////////////
  109. LONG CThreadBase::Release( void )
  110. {
  111. LONG nRet;
  112. BOOL fFinalRelease = ( (nRet = InterlockedDecrement(&m_lRefCount)) == 0 );
  113. ASSERT_BREAK(nRet >= 0);
  114. if ( fFinalRelease )
  115. {
  116. OnFinalRelease();
  117. }
  118. return nRet;
  119. }