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.

191 lines
3.8 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. remlock.c
  5. Abstract:
  6. Common RemoveLock
  7. Authors:
  8. Jeff Midkiff
  9. Environment:
  10. kernel mode only
  11. Notes:
  12. Simple binary compatible RemoveLock definitions for Win9x & Win2k
  13. made to mimic the Win2k ONLY IoXxxRemoveLock functions.
  14. See the Win2k DDK for descriptions.
  15. Revision History:
  16. --*/
  17. #include "remlock.h"
  18. #include "debug.h"
  19. #if !(DBG && WIN2K_LOCKS)
  20. #pragma alloc_text(PAGEWCE1, InitializeRemoveLock)
  21. #pragma alloc_text(PAGEWCE1, ReleaseRemoveLockAndWait)
  22. VOID
  23. InitializeRemoveLock(
  24. IN PREMOVE_LOCK Lock
  25. )
  26. {
  27. PAGED_CODE();
  28. if (Lock) {
  29. Lock->Removed = FALSE;
  30. Lock->IoCount = 1;
  31. KeInitializeEvent( &Lock->RemoveEvent,
  32. SynchronizationEvent,
  33. FALSE );
  34. DbgDump(DBG_LOCKS, ("InitializeRemoveLock: %p, %d\n", Lock, Lock->IoCount));
  35. } else {
  36. DbgDump(DBG_ERR, ("InitializeRemoveLock: Invalid Parameter\n"));
  37. TEST_TRAP();
  38. }
  39. }
  40. NTSTATUS
  41. AcquireRemoveLock(
  42. IN PREMOVE_LOCK Lock,
  43. IN OPTIONAL PVOID Tag
  44. )
  45. {
  46. LONG ioCount;
  47. NTSTATUS status;
  48. UNREFERENCED_PARAMETER(Tag);
  49. #if DBG
  50. if (!Lock) {
  51. status = STATUS_INVALID_PARAMETER;
  52. DbgDump(DBG_ERR, ("AcquireRemoveLock error: 0x%x\n", status ));
  53. TEST_TRAP();
  54. }
  55. #endif
  56. //
  57. // Grab the remove lock
  58. //
  59. ioCount = InterlockedIncrement( &Lock->IoCount );
  60. ASSERTMSG("AcquireRemoveLock - lock negative : \n", (ioCount > 0));
  61. if ( !Lock->Removed ) {
  62. status = STATUS_SUCCESS;
  63. } else {
  64. if (0 == InterlockedDecrement( &Lock->IoCount ) ) {
  65. KeSetEvent( &Lock->RemoveEvent, 0, FALSE);
  66. }
  67. status = STATUS_DELETE_PENDING;
  68. TEST_TRAP();
  69. }
  70. DbgDump(DBG_LOCKS, ("AcquireRemoveLock: %d, %p\n", Lock->IoCount, Tag));
  71. return status;
  72. }
  73. VOID
  74. ReleaseRemoveLock(
  75. IN PREMOVE_LOCK Lock,
  76. IN OPTIONAL PVOID Tag
  77. )
  78. {
  79. LONG ioCount;
  80. UNREFERENCED_PARAMETER(Tag);
  81. #if DBG
  82. if (!Lock) {
  83. DbgDump(DBG_ERR, ("ReleaseRemoveLock: Invalid Parameter\n"));
  84. TEST_TRAP();
  85. }
  86. #endif
  87. ioCount = InterlockedDecrement( &Lock->IoCount );
  88. ASSERT(0 <= ioCount);
  89. if (0 == ioCount) {
  90. ASSERT(Lock->Removed);
  91. TEST_TRAP();
  92. //
  93. // The device needs to be removed. Signal the remove event
  94. // that it's safe to go ahead.
  95. //
  96. KeSetEvent(&Lock->RemoveEvent, IO_NO_INCREMENT, FALSE);
  97. }
  98. DbgDump(DBG_LOCKS, ("ReleaseRemoveLock: %d, %p\n", Lock->IoCount, Tag));
  99. return;
  100. }
  101. VOID
  102. ReleaseRemoveLockAndWait(
  103. IN PREMOVE_LOCK Lock,
  104. IN OPTIONAL PVOID Tag
  105. )
  106. {
  107. LONG ioCount;
  108. PAGED_CODE();
  109. UNREFERENCED_PARAMETER(Tag);
  110. #if DBG
  111. if (!Lock) {
  112. DbgDump(DBG_ERR, ("ReleaseRemoveLockAndWait: Invalid Parameter\n"));
  113. TEST_TRAP();
  114. }
  115. #endif
  116. DbgDump(DBG_LOCKS, ("ReleaseRemoveLockAndWait: %d, %p\n", Lock->IoCount, Tag));
  117. Lock->Removed = TRUE;
  118. ioCount = InterlockedDecrement( &Lock->IoCount );
  119. ASSERT (0 < ioCount);
  120. if (0 < InterlockedDecrement( &Lock->IoCount ) ) {
  121. DbgDump(DBG_LOCKS, ("ReleaseRemoveLockAndWait: waiting for %d IoCount...\n", Lock->IoCount));
  122. // BUGBUG: may want a timeout here inside a loop
  123. KeWaitForSingleObject( &Lock->RemoveEvent,
  124. Executive,
  125. KernelMode,
  126. FALSE,
  127. NULL);
  128. DbgDump(DBG_LOCKS, ("....ReleaseRemoveLockAndWait: done!\n"));
  129. }
  130. return;
  131. }
  132. #endif // !(DBG && WIN2K_LOCKS)
  133. // EOF