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.

154 lines
4.0 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000.
  5. //
  6. // File: U P S Y N C . H
  7. //
  8. // Contents: Synchronization classes
  9. //
  10. // Notes:
  11. //
  12. // Author: mbend 17 Aug 2000
  13. //
  14. //----------------------------------------------------------------------------
  15. #pragma once
  16. #include "ncbase.h"
  17. //+---------------------------------------------------------------------------
  18. //
  19. // Class: CUCriticalSection
  20. //
  21. // Purpose: Wrapper for Win32 critical sections
  22. //
  23. // Author: mbend 17 Aug 2000
  24. //
  25. // Notes:
  26. // The most important and lightweight Windows NT synchronization primitive.
  27. // Allows only one thread to enter itself at a single time.
  28. // An important property of critical sections is that they are
  29. // thread reentrant which means that if a thread owns a critical
  30. // section and tries to enter it again, the thread is allowed to
  31. // do so. When exiting the lock, the lock is not freed until it is
  32. // exited once for each time that it was entered.
  33. //
  34. // Critical sections are lightweight because they are not kernel
  35. // objects. Instead they are implemented through a simple in
  36. // memory structure CRITICAL_SECTION. If there is no thread
  37. // contention, they are implemented by simple memory operations
  38. // and spin locks that are hundreds of times faster than
  39. // alternative kernel object solutions such as mutexes. However,
  40. // if there is thread contention, critical sections degraded into
  41. // kernel objects.
  42. //
  43. class CUCriticalSection
  44. {
  45. public:
  46. CUCriticalSection()
  47. {
  48. InitializeCriticalSection(&m_critsec);
  49. }
  50. ~CUCriticalSection()
  51. {
  52. DeleteCriticalSection(&m_critsec);
  53. }
  54. void Enter()
  55. {
  56. EnterCriticalSection(&m_critsec);
  57. }
  58. BOOL FTryEnter()
  59. {
  60. return TryEnterCriticalSection(&m_critsec);
  61. }
  62. void Leave()
  63. {
  64. LeaveCriticalSection(&m_critsec);
  65. }
  66. DWORD DwSetSpinCount(DWORD dwSpinCount)
  67. {
  68. return SetCriticalSectionSpinCount(&m_critsec, dwSpinCount);
  69. }
  70. private:
  71. CUCriticalSection(const CUCriticalSection &);
  72. CUCriticalSection & operator=(const CUCriticalSection &);
  73. CRITICAL_SECTION m_critsec;
  74. };
  75. //+---------------------------------------------------------------------------
  76. //
  77. // Class: CLock
  78. //
  79. // Purpose: Used to place an auto lock on a critical section
  80. //
  81. // Author: mbend 17 Aug 2000
  82. //
  83. // Notes:
  84. // Typically this class is used in an class object methods like so:
  85. //
  86. // class SynchronizedExample {
  87. // public:
  88. // void LockedMethod() {
  89. // CLock lock(m_critSec);
  90. // ...
  91. // }
  92. // private:
  93. // CUCriticalSection m_critSec;
  94. // };
  95. class CLock {
  96. public:
  97. // Not attached
  98. CLock(CUCriticalSection & critsec) : m_critsec(critsec)
  99. {
  100. m_critsec.Enter();
  101. }
  102. ~CLock()
  103. {
  104. m_critsec.Leave();
  105. }
  106. private:
  107. CLock(const CLock &);
  108. CLock & operator=(const CLock &);
  109. CUCriticalSection & m_critsec;
  110. };
  111. #ifdef __ATLCOM_H__
  112. //+---------------------------------------------------------------------------
  113. //
  114. // Class: CALock
  115. //
  116. // Purpose: Used to place an auto lock on a CComObjectRootEx<CComMultiThreadModel> derived class
  117. //
  118. // Author: mbend 17 Aug 2000
  119. //
  120. // Notes:
  121. // Typically this class is used in an class object methods like so:
  122. //
  123. // class SynchronizedExample : public CComObjectRootEx<CComMultiThreadModel> {
  124. // public:
  125. // void LockedMethod() {
  126. // CALock lock(*this);
  127. // ...
  128. // }
  129. // };
  130. class CALock {
  131. public:
  132. // Not attached
  133. CALock(CComObjectRootEx<CComMultiThreadModel> & object) : m_object(object)
  134. {
  135. m_object.Lock();
  136. }
  137. ~CALock()
  138. {
  139. m_object.Unlock();
  140. }
  141. private:
  142. CALock(const CLock &);
  143. CALock & operator=(const CLock &);
  144. CComObjectRootEx<CComMultiThreadModel> & m_object;
  145. };
  146. #endif // __ATLCOM_H__