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.

168 lines
3.3 KiB

  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. Module Name:
  4. lockcode.c
  5. Abstract:
  6. Author:
  7. Chuck Lenzmeier (chuckl) 30-Jan-1994
  8. Manny Weiser (mannyw) 17-May-1994
  9. Revision History:
  10. --*/
  11. #include "Procs.h"
  12. #ifndef QFE_BUILD
  13. #ifdef ALLOC_PRAGMA
  14. #pragma alloc_text( PAGE, NwReferenceUnlockableCodeSection )
  15. #pragma alloc_text( PAGE, NwDereferenceUnlockableCodeSection )
  16. #endif
  17. extern BOOLEAN TimerStop; // From Timer.c
  18. //
  19. // The debug trace level
  20. //
  21. #define Dbg (DEBUG_TRACE_CREATE)
  22. VOID
  23. NwReferenceUnlockableCodeSection (
  24. VOID
  25. )
  26. {
  27. ULONG oldCount;
  28. //
  29. // Lock the lockable code database.
  30. //
  31. ExAcquireResourceExclusiveLite( &NwUnlockableCodeResource, TRUE );
  32. //
  33. // Increment the reference count for the section.
  34. //
  35. oldCount = NwSectionDescriptor.ReferenceCount++;
  36. if ( oldCount == 0 && NwSectionDescriptor.Handle == NULL ) {
  37. //
  38. // This is the first reference to the section. Start the timer.
  39. // Lock our code.
  40. //
  41. NwSectionDescriptor.Handle = MmLockPagableCodeSection( NwSectionDescriptor.Base );
  42. StartTimer( );
  43. } else {
  44. //
  45. // This is not the first reference to the section. The section
  46. // had better be locked!
  47. //
  48. ASSERT( NwSectionDescriptor.Handle != NULL );
  49. //
  50. // Restart the timer if the rdr was stopped but didn't unload.
  51. //
  52. if (TimerStop == TRUE) {
  53. StartTimer();
  54. }
  55. }
  56. DebugTrace(+0, Dbg, "NwReferenceCodeSection %d\n", NwSectionDescriptor.ReferenceCount );
  57. ExReleaseResourceLite( &NwUnlockableCodeResource );
  58. return;
  59. } // NwReferenceUnlockableCodeSection
  60. VOID
  61. NwDereferenceUnlockableCodeSection (
  62. VOID
  63. )
  64. {
  65. ULONG newCount;
  66. //
  67. // Lock the lockable code database.
  68. //
  69. ExAcquireResourceExclusiveLite( &NwUnlockableCodeResource, TRUE );
  70. ASSERT( NwSectionDescriptor.Handle != NULL );
  71. ASSERT( NwSectionDescriptor.ReferenceCount > 0 &&
  72. NwSectionDescriptor.ReferenceCount < 0x7FFF );
  73. //
  74. // Decrement the reference count for the section.
  75. //
  76. newCount = --NwSectionDescriptor.ReferenceCount;
  77. DebugTrace(+0, Dbg, "NwDereferenceCodeSection %d\n", NwSectionDescriptor.ReferenceCount );
  78. ExReleaseResourceLite( &NwUnlockableCodeResource );
  79. return;
  80. } // NwDereferenceUnlockableCodeSection
  81. BOOLEAN
  82. NwUnlockCodeSections(
  83. IN BOOLEAN BlockIndefinitely
  84. )
  85. {
  86. //
  87. // Lock the lockable code database.
  88. //
  89. if (!ExAcquireResourceExclusiveLite( &NwUnlockableCodeResource, BlockIndefinitely )) {
  90. return FALSE; // Avoid potential deadlock in timer.c
  91. }
  92. DebugTrace(+0, Dbg, "NwUnlockCodeSections %d\n", NwSectionDescriptor.ReferenceCount );
  93. if ( NwSectionDescriptor.ReferenceCount == 0 ) {
  94. if ( NwSectionDescriptor.Handle != NULL ) {
  95. //
  96. // This is the last reference to the section. Stop the timer and
  97. // unlock the code.
  98. //
  99. StopTimer();
  100. MmUnlockPagableImageSection( NwSectionDescriptor.Handle );
  101. NwSectionDescriptor.Handle = NULL;
  102. }
  103. ExReleaseResourceLite( &NwUnlockableCodeResource );
  104. return TRUE;
  105. }
  106. ExReleaseResourceLite( &NwUnlockableCodeResource );
  107. return FALSE;
  108. }
  109. #endif