RWLock.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright: JessMA Open Source (ldcsaa@gmail.com)
  3. *
  4. * Author : Bruce Liang
  5. * Website : https://github.com/ldcsaa
  6. * Project : https://github.com/ldcsaa/HP-Socket
  7. * Blog : http://www.cnblogs.com/ldcsaa
  8. * Wiki : http://www.oschina.net/p/hp-socket
  9. * QQ Group : 44636872, 75375912
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. */
  23. #pragma once
  24. #include "hpsocket/GlobalDef.h"
  25. #include "CriSec.h"
  26. #include <shared_mutex>
  27. #include <condition_variable>
  28. using namespace std;
  29. class CMutexRWLock
  30. {
  31. public:
  32. VOID WaitToRead();
  33. VOID WaitToWrite();
  34. VOID ReadDone();
  35. VOID WriteDone();
  36. private:
  37. BOOL IsOwner() {return ::IsSelfThread(m_dwWriterTID);}
  38. VOID SetOwner() {m_dwWriterTID = SELF_THREAD_ID;}
  39. VOID DetachOwner() {m_dwWriterTID = 0;}
  40. public:
  41. CMutexRWLock();
  42. ~CMutexRWLock();
  43. DECLARE_NO_COPY_CLASS(CMutexRWLock)
  44. private:
  45. int m_nActive;
  46. THR_ID m_dwWriterTID;
  47. CSpinGuard m_cs;
  48. shared_timed_mutex m_mtx;
  49. };
  50. class CSEMRWLock
  51. {
  52. public:
  53. VOID WaitToRead();
  54. VOID WaitToWrite();
  55. VOID ReadDone();
  56. VOID WriteDone();
  57. private:
  58. BOOL IsOwner() {BOOL bOwner = ::IsSelfThread(m_dwWriterTID); ASSERT(!bOwner || m_nActive < 0); return bOwner;}
  59. VOID SetOwner() {m_dwWriterTID = SELF_THREAD_ID;}
  60. VOID DetachOwner() {m_dwWriterTID = 0;}
  61. public:
  62. CSEMRWLock();
  63. ~CSEMRWLock();
  64. DECLARE_NO_COPY_CLASS(CSEMRWLock)
  65. private:
  66. int m_nWaitingReaders;
  67. int m_nWaitingWriters;
  68. int m_nActive;
  69. THR_ID m_dwWriterTID;
  70. CMTX m_mtx;
  71. condition_variable m_cvRead;
  72. condition_variable m_cvWrite;
  73. };
  74. template<class CLockObj> class CLocalReadLock
  75. {
  76. public:
  77. CLocalReadLock(CLockObj& obj) : m_wait(obj) {m_wait.WaitToRead();}
  78. ~CLocalReadLock() {m_wait.ReadDone();}
  79. DECLARE_NO_COPY_CLASS(CLocalReadLock)
  80. private:
  81. CLockObj& m_wait;
  82. };
  83. template<class CLockObj> class CLocalWriteLock
  84. {
  85. public:
  86. CLocalWriteLock(CLockObj& obj) : m_wait(obj) {m_wait.WaitToWrite();}
  87. ~CLocalWriteLock() {m_wait.WriteDone();}
  88. DECLARE_NO_COPY_CLASS(CLocalWriteLock)
  89. private:
  90. CLockObj& m_wait;
  91. };
  92. using CSimpleRWLock = shared_timed_mutex;
  93. using CReadLock = shared_lock<shared_timed_mutex>;
  94. using CWriteLock = lock_guard<shared_timed_mutex>;
  95. using CWriteLock2 = unique_lock<shared_timed_mutex>;
  96. #if !defined(_USE_MUTEX_RW_LOCK)
  97. using CRWLock = CSEMRWLock;
  98. #else
  99. using CRWLock = CMutexRWLock;
  100. #endif
  101. using CReentrantReadLock = CLocalReadLock<CRWLock>;
  102. using CReentrantWriteLock = CLocalWriteLock<CRWLock>;