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.

181 lines
4.4 KiB

  1. /***************************************************************************\
  2. *
  3. * File: BaseObject.inl
  4. *
  5. * History:
  6. * 11/05/1999: JStall: Created
  7. *
  8. * Copyright (C) 2000 by Microsoft Corporation. All rights reserved.
  9. *
  10. \***************************************************************************/
  11. #if !defined(BASE__BaseObject_inl__INCLUDED)
  12. #define BASE__BaseObject_inl__INCLUDED
  13. #pragma once
  14. #include "Locks.h"
  15. //------------------------------------------------------------------------------
  16. inline HANDLE GetHandle(const BaseObject * pbase)
  17. {
  18. if (pbase != NULL) {
  19. return pbase->GetHandle();
  20. }
  21. return NULL;
  22. }
  23. /***************************************************************************\
  24. *****************************************************************************
  25. *
  26. * class BaseObject
  27. *
  28. *****************************************************************************
  29. \***************************************************************************/
  30. //------------------------------------------------------------------------------
  31. inline
  32. BaseObject::BaseObject()
  33. {
  34. m_cRef = 1; // Start off with a valid reference
  35. }
  36. //------------------------------------------------------------------------------
  37. inline HANDLE
  38. BaseObject::GetHandle() const
  39. {
  40. return (HANDLE) this;
  41. }
  42. //------------------------------------------------------------------------------
  43. inline BaseObject *
  44. BaseObject::ValidateHandle(HANDLE h)
  45. {
  46. return (BaseObject *) h;
  47. }
  48. #if DBG
  49. //------------------------------------------------------------------------------
  50. inline void
  51. BaseObject::DEBUG_CheckValidLockCount() const
  52. {
  53. if (!DEBUG_IsZeroLockCountValid()) {
  54. AssertMsg(m_cRef > 0, "Object must have an outstanding reference.");
  55. }
  56. }
  57. #endif // DBG
  58. //------------------------------------------------------------------------------
  59. inline void
  60. BaseObject::Lock()
  61. {
  62. #if DBG
  63. DEBUG_CheckValidLockCount();
  64. #endif // DBG
  65. SafeIncrement(&m_cRef);
  66. }
  67. /***************************************************************************\
  68. *
  69. * BaseObject::xwUnlock
  70. *
  71. * xwUnlock() decrements the objects's usage count by 1. When the usage count
  72. * reaches 0, the object will be destroyed.
  73. *
  74. * NOTE: This function is designed to be called from the ResourceManager
  75. * and should not normally be called directly.
  76. *
  77. * <retval> Returns if the object is still valid (has not been destroyed)</retval>
  78. *
  79. \***************************************************************************/
  80. inline BOOL
  81. BaseObject::xwUnlock()
  82. {
  83. #if DBG
  84. DEBUG_CheckValidLockCount();
  85. #endif // DBG
  86. #if DBG
  87. AssertMsg((this != s_DEBUG_pobjEnsure) || (m_cRef > 1), "Ensure Object is remains valid");
  88. #endif // DBG
  89. if (SafeDecrement(&m_cRef) == 0) {
  90. xwDestroy();
  91. return FALSE;
  92. }
  93. return TRUE;
  94. }
  95. //------------------------------------------------------------------------------
  96. inline BOOL
  97. BaseObject::xwUnlockNL(FinalUnlockProc pfnFinal, void * pvData)
  98. {
  99. #if DBG
  100. DEBUG_CheckValidLockCount();
  101. #endif // DBG
  102. #if DBG
  103. AssertMsg((this != s_DEBUG_pobjEnsure) || (m_cRef > 1), "Ensure Object is remains valid");
  104. #endif // DBG
  105. if (SafeDecrement(&m_cRef) == 0) {
  106. //
  107. // Object needs to be destroyed, so notify the caller so that it has
  108. // an opportunity to prepare.
  109. //
  110. if (pfnFinal != NULL) {
  111. pfnFinal(this, pvData);
  112. }
  113. xwDestroy();
  114. return FALSE;
  115. }
  116. return TRUE;
  117. }
  118. /***************************************************************************\
  119. *****************************************************************************
  120. *
  121. * class ObjectLock
  122. *
  123. *****************************************************************************
  124. \***************************************************************************/
  125. //------------------------------------------------------------------------------
  126. inline
  127. ObjectLock::ObjectLock(BaseObject * pobjLock)
  128. {
  129. AssertMsg(pobjLock->GetHandleType() != htContext, "Use ContextLock to lock a Context");
  130. pobj = pobjLock;
  131. pobj->Lock();
  132. #if DBG
  133. pobjLock->DEBUG_AssertValid();
  134. #endif
  135. }
  136. //------------------------------------------------------------------------------
  137. inline
  138. ObjectLock::~ObjectLock()
  139. {
  140. pobj->xwUnlock();
  141. }
  142. #endif // BASE__BaseObject_inl__INCLUDED