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.

204 lines
4.6 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. lock.cxx
  5. Abstract:
  6. Contains code for the Service Control Database manager. This includes
  7. all the database lock routines. This file contains the following
  8. classes:
  9. CCountingResource
  10. CServiceListLock
  11. CGroupListLock
  12. Author:
  13. Anirudh Sahni (anirudhs) 09-Jan-1997
  14. Environment:
  15. User Mode -Win32
  16. Revision History:
  17. 09-Jan-1997 AnirudhS
  18. Created, replacing the old locking functions in dataman.cxx.
  19. --*/
  20. //
  21. // INCLUDES
  22. //
  23. #include "precomp.hxx"
  24. //
  25. // Macros
  26. //
  27. #define LOCK_LOG(string) SC_LOG3(LOCKS, " %s" string " level = %ld\n", \
  28. _ShortName, _Name, CurrentLevel())
  29. //
  30. // Globals
  31. //
  32. CServiceRecordLock ScServiceRecordLock;
  33. CServiceListLock ScServiceListLock;
  34. CGroupListLock ScGroupListLock;
  35. /*************************************************************************/
  36. /* CCountingResource methods */
  37. /*************************************************************************/
  38. #if DBG
  39. void
  40. CCountingResource::GetShared()
  41. {
  42. LOCK_LOG(" Asking for %s lock shared...");
  43. SC_ASSERT(!HaveExclusive());
  44. RtlAcquireResourceShared(&_Lock, TRUE);
  45. SetCurrentLevel(CurrentLevel() + 1);
  46. LOCK_LOG("+Acquired %s lock shared,");
  47. }
  48. void
  49. CCountingResource::GetExclusive()
  50. {
  51. LOCK_LOG(" Asking for %s lock exclusive...");
  52. SC_ASSERT(!Have() || HaveExclusive());
  53. RtlAcquireResourceExclusive(&_Lock, TRUE);
  54. SetCurrentLevel(CurrentLevel() - 1);
  55. LOCK_LOG("+Acquired %s lock exclusive,");
  56. }
  57. void
  58. CCountingResource::MakeShared()
  59. {
  60. LOCK_LOG("vConverting %s lock to shared...");
  61. SC_ASSERT(CurrentLevel() == -1);
  62. RtlConvertExclusiveToShared(&_Lock);
  63. SetCurrentLevel(1);
  64. LOCK_LOG(" Converted %s lock to shared,");
  65. }
  66. void
  67. CCountingResource::MakeExclusive()
  68. {
  69. // WARNING: This option is easily misused.
  70. LOCK_LOG(" Converting %s lock to exclusive...");
  71. SC_ASSERT(CurrentLevel() == 1);
  72. RtlConvertSharedToExclusive(&_Lock);
  73. SetCurrentLevel(-1);
  74. LOCK_LOG("^Converted %s lock to exclusive,");
  75. }
  76. void
  77. CCountingResource::Release()
  78. {
  79. LOCK_LOG("-Releasing %s lock...");
  80. SC_ASSERT(Have());
  81. RtlReleaseResource( &_Lock );
  82. if (CurrentLevel() > 0)
  83. {
  84. SetCurrentLevel(CurrentLevel() - 1);
  85. }
  86. else
  87. {
  88. SetCurrentLevel(CurrentLevel() + 1);
  89. }
  90. LOCK_LOG(" Released %s lock,");
  91. }
  92. #endif // DBG
  93. /*************************************************************************/
  94. /* CServiceListLock methods */
  95. /*************************************************************************/
  96. #if DBG
  97. void
  98. CServiceListLock::GetShared()
  99. {
  100. // The record lock mustn't be acquired before the list lock
  101. if (!Have())
  102. {
  103. SC_ASSERT(! ScServiceRecordLock.Have());
  104. }
  105. CCountingResource::GetShared();
  106. }
  107. void
  108. CServiceListLock::GetExclusive()
  109. {
  110. // The record lock mustn't be acquired before the list lock
  111. if (!Have())
  112. {
  113. SC_ASSERT(! ScServiceRecordLock.Have());
  114. }
  115. CCountingResource::GetExclusive();
  116. }
  117. void
  118. CServiceListLock::Release()
  119. {
  120. // We mustn't release this if we still have the record lock
  121. if (CurrentLevel() == 1 || CurrentLevel() == -1)
  122. {
  123. SC_ASSERT(! ScServiceRecordLock.Have());
  124. }
  125. CCountingResource::Release();
  126. }
  127. #endif // DBG
  128. /*************************************************************************/
  129. /* CGroupListLock methods */
  130. /*************************************************************************/
  131. #if DBG
  132. void
  133. CGroupListLock::GetShared()
  134. {
  135. // The service list lock mustn't be acquired before the group list lock
  136. if (!Have())
  137. {
  138. SC_ASSERT(! ScServiceListLock.Have());
  139. }
  140. CCountingResource::GetShared();
  141. }
  142. void
  143. CGroupListLock::GetExclusive()
  144. {
  145. // The service list lock mustn't be acquired before the group list lock
  146. if (!Have())
  147. {
  148. SC_ASSERT(! ScServiceListLock.Have());
  149. }
  150. CCountingResource::GetExclusive();
  151. }
  152. void
  153. CGroupListLock::Release()
  154. {
  155. // We mustn't release this if we still have the service list lock or
  156. // service record lock
  157. if (CurrentLevel() == 1 || CurrentLevel() == -1)
  158. {
  159. SC_ASSERT(! ScServiceRecordLock.Have());
  160. SC_ASSERT(! ScServiceListLock.Have());
  161. }
  162. CCountingResource::Release();
  163. }
  164. #endif // DBG