Guard.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // "$Id: Guard.h 55347 2012-03-22 11:26:45Z qin_fenglin $"
  3. //
  4. // Copyright (c)1992-2007, ZheJiang Dahua Technology Stock CO.LTD.
  5. // All Rights Reserved.
  6. //
  7. // Description:
  8. // Revisions: Year-Month-Day SVN-Author Modification
  9. //
  10. #ifndef __INFRA3_GUARD_H__
  11. #define __INFRA3_GUARD_H__
  12. #include "Mutex.h"
  13. #include "RecursiveMutex.h"
  14. #include "ReadWriteMutex.h"
  15. namespace Dahua{
  16. namespace Infra{
  17. /// \class CGuard
  18. /// \brief 守护者类。
  19. ///
  20. /// 构造时自动调用CMutex::Enter,析构时自动调用CMutex::Leave,避免不
  21. /// 配对的情况发生。示例如下:
  22. /// \code
  23. /// CMutex mutex;
  24. /// int needProtect = 0;
  25. /// void testAdd()
  26. /// {
  27. /// there:
  28. /// if(needProtect % 2) // not protected
  29. /// {
  30. /// needProtect++;
  31. /// }
  32. /// else
  33. /// {
  34. /// return;
  35. /// }
  36. ///
  37. /// CGuard guard(mutex);
  38. /// needProtect++;
  39. /// goto there; // will also bring ~CGuard(), thus mutex.leave()
  40. /// }
  41. /// void testSub()
  42. /// {
  43. /// needProtect--; // not protected
  44. ///
  45. /// CGuard guard(mutex);
  46. /// needProtect--;
  47. /// }
  48. /// \endcode
  49. class CGuard
  50. {
  51. CGuard(CGuard const&);
  52. CGuard& operator=(CGuard const&);
  53. public:
  54. /// 构造函数,自动调用CMutex::enter
  55. /// \param mutex 互斥量引用,CGuard并不创建互斥量。
  56. inline CGuard(CMutex& mutex)
  57. :m_mutex(mutex)
  58. {
  59. m_mutex.enter();
  60. };
  61. /// 析构函数,自动调用CMutex::leave
  62. inline ~CGuard()
  63. {
  64. m_mutex.leave();
  65. };
  66. private:
  67. CMutex &m_mutex; ///< 需要自动调用的互斥量引用
  68. };
  69. ////////////////////////////////////////////////////////////////////////////////
  70. class CRecursiveGuard
  71. {
  72. CRecursiveGuard(CRecursiveGuard const&);
  73. CRecursiveGuard& operator=(CRecursiveGuard const&);
  74. public:
  75. /// 构造函数,自动调用CMutex::enter
  76. /// \param mutex 互斥量引用,CGuard并不创建互斥量。
  77. inline CRecursiveGuard(CRecursiveMutex& mutex)
  78. :m_mutex(mutex)
  79. {
  80. m_mutex.enter();
  81. };
  82. /// 析构函数,自动调用CMutex::leave
  83. inline ~CRecursiveGuard()
  84. {
  85. m_mutex.leave();
  86. };
  87. private:
  88. CRecursiveMutex &m_mutex; ///< 需要自动调用的互斥量引用
  89. };
  90. ////////////////////////////////////////////////////////////////////////////////
  91. /// 读写锁的读操作保护
  92. class CGuardReading
  93. {
  94. CGuardReading(CGuardReading const&);
  95. CGuardReading& operator=(CGuardReading const&);
  96. public:
  97. /// 构造函数,自动调用 CRwMutex::EnterReading
  98. /// \param mutex 互斥量引用,CGuard并不创建互斥量。
  99. inline CGuardReading(CReadWriteMutex& mutex)
  100. : m_mutex(mutex)
  101. {
  102. m_mutex.enterReading();
  103. };
  104. /// 析构函数,自动调用 CReadWriteMutex::leave
  105. inline ~CGuardReading()
  106. {
  107. m_mutex.leave();
  108. };
  109. private:
  110. CReadWriteMutex& m_mutex; ///< 需要自动调用的互斥量引用
  111. };
  112. ////////////////////////////////////////////////////////////////////////////////
  113. /// 读写锁的写操作保护
  114. class CGuardWriting
  115. {
  116. CGuardWriting(CGuardWriting const&);
  117. CGuardWriting& operator=(CGuardWriting const&);
  118. public:
  119. /// 构造函数,自动调用 CReadWriteMutex::EnterWriting
  120. /// \param mutex 互斥量引用,CGuard并不创建互斥量。
  121. inline CGuardWriting(CReadWriteMutex& mutex)
  122. : m_mutex(mutex)
  123. {
  124. m_mutex.enterWriting();
  125. };
  126. /// 析构函数,自动调用 CReadWriteMutex::leave
  127. inline ~CGuardWriting()
  128. {
  129. m_mutex.leave();
  130. };
  131. private:
  132. CReadWriteMutex& m_mutex; ///< 需要自动调用的互斥量引用
  133. };
  134. } // namespace Infra
  135. } // namespace Dahua
  136. #endif //__INFRA_GUARD_H__