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.

126 lines
1.7 KiB

  1. /*++
  2. Copyright (c) 2000-2001 Microsoft Corporation
  3. Module Name:
  4. rwlock.h
  5. Abstract:
  6. This module contains public declarations for a multiple-reader
  7. single-writer lock.
  8. Author:
  9. Chun Ye (chunye) 20-Dec-2000
  10. Revision History:
  11. --*/
  12. #ifndef _RWLOCK_H_
  13. #define _RWLOCK_H_
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. //
  18. // The R/W Lock implements the multiple-reader single-writer locking scheme.
  19. //
  20. typedef struct _UL_RW_LOCK
  21. {
  22. UL_SPIN_LOCK SpinLock;
  23. LONG RefCount;
  24. } UL_RW_LOCK, *PUL_RW_LOCK;
  25. //
  26. // R/W Lock functions.
  27. //
  28. __inline
  29. VOID
  30. FASTCALL
  31. UlInitializeRWLock(
  32. OUT PUL_RW_LOCK pLock
  33. )
  34. {
  35. pLock->RefCount = 0;
  36. UlInitializeSpinLock( &pLock->SpinLock, "RWLock" );
  37. }
  38. __inline
  39. VOID
  40. FASTCALL
  41. UlAcquireRWLockForRead(
  42. IN OUT PUL_RW_LOCK pLock,
  43. IN OUT PKIRQL pIrql
  44. )
  45. {
  46. UlAcquireSpinLock( &pLock->SpinLock, pIrql );
  47. InterlockedIncrement( &pLock->RefCount );
  48. UlReleaseSpinLockFromDpcLevel( &pLock->SpinLock );
  49. }
  50. __inline
  51. VOID
  52. FASTCALL
  53. UlAcquireRWLockForWrite(
  54. IN OUT PUL_RW_LOCK pLock,
  55. IN OUT PKIRQL pIrql
  56. )
  57. {
  58. UlAcquireSpinLock(&pLock->SpinLock, pIrql);
  59. while (*((volatile LONG *)&pLock->RefCount) != 0);
  60. }
  61. __inline
  62. VOID
  63. FASTCALL
  64. UlReleaseRWLockFromRead(
  65. IN OUT PUL_RW_LOCK pLock,
  66. IN KIRQL Irql
  67. )
  68. {
  69. ASSERT( KeGetCurrentIrql() == DISPATCH_LEVEL );
  70. InterlockedDecrement( &pLock->RefCount );
  71. KeLowerIrql( Irql );
  72. }
  73. __inline
  74. VOID
  75. FASTCALL
  76. UlReleaseRWLockFromWrite(
  77. IN OUT PUL_RW_LOCK pLock,
  78. IN KIRQL Irql
  79. )
  80. {
  81. ASSERT( KeGetCurrentIrql() == DISPATCH_LEVEL );
  82. UlReleaseSpinLock( &pLock->SpinLock, Irql );
  83. }
  84. #ifdef __cplusplus
  85. }; // extern "C"
  86. #endif
  87. #endif // _RWLOCK_H_