Leaked source code of windows server 2003
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.

164 lines
2.9 KiB

  1. /*++
  2. Copyright (c) 1999-2001 Microsoft Corporation
  3. Module Name:
  4. locks.h
  5. Abstract:
  6. Contains all the lock related macros.
  7. Author:
  8. ChunYe
  9. Environment:
  10. Kernel mode
  11. Revision History:
  12. --*/
  13. #ifndef _LOCKS_H
  14. #define _LOCKS_H
  15. #define IPSEC_READ_LOCK 1
  16. #define IPSEC_WRITE_LOCK 2
  17. #define INIT_LOCK(l) KeInitializeSpinLock(l)
  18. #define ACQUIRE_LOCK(l, q) KeAcquireSpinLock(l, q)
  19. #define RELEASE_LOCK(l, q) KeReleaseSpinLock(l, q)
  20. //
  21. // The Filter/SA database is protected by a single-writer (infrequent) and
  22. // multiple-reader (frequent) locking scheme.
  23. //
  24. typedef struct _MRSW_LOCK
  25. {
  26. KSPIN_LOCK SpinLock;
  27. ULONG RefCount;
  28. #if DBG
  29. ULONG LastLockLine;
  30. ULONG LastLockType;
  31. #endif
  32. } MRSW_LOCK, *PMRSW_LOCK;
  33. __inline
  34. VOID
  35. InitializeMRSWLock(
  36. PMRSW_LOCK pRWLock
  37. )
  38. {
  39. pRWLock->RefCount = 0;
  40. KeInitializeSpinLock(&pRWLock->SpinLock);
  41. #if DBG
  42. pRWLock->LastLockLine = 0;
  43. pRWLock->LastLockType = 0;
  44. #endif
  45. }
  46. __inline
  47. VOID
  48. AcquireReadLock(
  49. PMRSW_LOCK pRWLock,
  50. PKIRQL pIrql
  51. )
  52. {
  53. KeAcquireSpinLock(&pRWLock->SpinLock, pIrql);
  54. InterlockedIncrement(&pRWLock->RefCount);
  55. KeReleaseSpinLockFromDpcLevel(&pRWLock->SpinLock);
  56. #if DBG
  57. pRWLock->LastLockLine = __LINE__;
  58. pRWLock->LastLockType = 1;
  59. #endif
  60. }
  61. __inline
  62. VOID
  63. ReleaseReadLock(
  64. PMRSW_LOCK pRWLock,
  65. KIRQL Irql
  66. )
  67. {
  68. InterlockedDecrement(&pRWLock->RefCount);
  69. KeLowerIrql(Irql);
  70. }
  71. __inline
  72. VOID
  73. AcquireReadLockAtDpc(
  74. PMRSW_LOCK pRWLock
  75. )
  76. {
  77. KeAcquireSpinLockAtDpcLevel(&pRWLock->SpinLock);
  78. InterlockedIncrement(&pRWLock->RefCount);
  79. KeReleaseSpinLockFromDpcLevel(&pRWLock->SpinLock);
  80. #if DBG
  81. pRWLock->LastLockLine = __LINE__;
  82. pRWLock->LastLockType = IPSEC_READ_LOCK;
  83. #endif
  84. }
  85. __inline
  86. VOID
  87. ReleaseReadLockFromDpc(
  88. PMRSW_LOCK pRWLock
  89. )
  90. {
  91. InterlockedDecrement(&pRWLock->RefCount);
  92. }
  93. __inline
  94. VOID
  95. AcquireWriteLock(
  96. PMRSW_LOCK pRWLock,
  97. PKIRQL pIrql
  98. )
  99. {
  100. KeAcquireSpinLock(&pRWLock->SpinLock, pIrql);
  101. while (*((volatile *)&pRWLock->RefCount));
  102. #if DBG
  103. pRWLock->LastLockLine = __LINE__;
  104. pRWLock->LastLockType = IPSEC_WRITE_LOCK;
  105. #endif
  106. }
  107. __inline
  108. VOID
  109. ReleaseWriteLock(
  110. PMRSW_LOCK pRWLock,
  111. KIRQL Irql
  112. )
  113. {
  114. KeReleaseSpinLock(&pRWLock->SpinLock, Irql);
  115. }
  116. __inline
  117. VOID
  118. AcquireWriteLockAtDpc(
  119. PMRSW_LOCK pRWLock
  120. )
  121. {
  122. KeAcquireSpinLockAtDpcLevel(&pRWLock->SpinLock);
  123. while (*((volatile *)&pRWLock->RefCount));
  124. #if DBG
  125. pRWLock->LastLockLine = __LINE__;
  126. pRWLock->LastLockType = IPSEC_WRITE_LOCK;
  127. #endif
  128. }
  129. __inline
  130. VOID
  131. ReleaseWriteLockFromDpc(
  132. PMRSW_LOCK pRWLock
  133. )
  134. {
  135. KeReleaseSpinLockFromDpcLevel(&pRWLock->SpinLock);
  136. }
  137. #endif _LOCKS_H