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.

151 lines
3.3 KiB

  1. /**************************************************************************\
  2. *
  3. * Copyright (c) 1999 Microsoft Corporation
  4. *
  5. * Module Name:
  6. *
  7. * Locking
  8. *
  9. * Abstract:
  10. *
  11. * Lockable: A base class for lockable objects, which contains a lock.
  12. * Lock: Represents a held lock on an object (acquires and releases an
  13. * object's lock in its constructor and destructor).
  14. *
  15. * Revision History:
  16. *
  17. * 02/22/1999 davidx
  18. * Created it.
  19. * 09/08/1999 agodfrey
  20. * Moved to Runtime\Lockable.hpp
  21. *
  22. \**************************************************************************/
  23. #ifndef _LOCKABLE_HPP
  24. #define _LOCKABLE_HPP
  25. namespace GpRuntime
  26. {
  27. //
  28. // TODO: Remove the 'Gp' prefix from GpLockable and GpLock
  29. //
  30. //--------------------------------------------------------------------------
  31. // Base class for lockable API objects
  32. //--------------------------------------------------------------------------
  33. class GpLockable
  34. {
  35. friend class GpLock;
  36. public:
  37. GpLockable()
  38. {
  39. LockCount = -1;
  40. }
  41. ~GpLockable()
  42. {
  43. // Comment out to prevent Office crash. 3/14/00 -- ikkof
  44. // ASSERTMSG(LockCount == -1, ("~GpLock: non-zero lock count"));
  45. }
  46. LONG* GetLockCount()
  47. {
  48. return &LockCount;
  49. }
  50. BOOL IsLocked() const
  51. {
  52. return (LockCount != -1);
  53. }
  54. VOID Reset()
  55. {
  56. LockCount = -1;
  57. }
  58. // Copy constructor and assignment operator
  59. GpLockable(GpLockable & lockable)
  60. {
  61. LockCount = -1;
  62. }
  63. GpLockable &operator=(const GpLockable & lockable)
  64. {
  65. return *this;
  66. }
  67. protected:
  68. LONG LockCount; // number of locks, minus one
  69. };
  70. //--------------------------------------------------------------------------
  71. // Class for locking API objects
  72. // NOTE: These locks are not reentrant!
  73. //--------------------------------------------------------------------------
  74. class GpLock
  75. {
  76. public:
  77. GpLock(GpLockable* lockable)
  78. {
  79. // In flatapi, we sometimes need to check if an optional (NULL)
  80. // parameter is busy, which means we would pass NULL into this
  81. // constructor. An optional parameter should not be considered locked.
  82. if (lockable != NULL)
  83. {
  84. LockCount = &lockable->LockCount;
  85. // Note that it generates less code when we store the result
  86. // here than it is to convert to a BOOL here and store that.
  87. Result = InterlockedIncrement(LockCount);
  88. }
  89. else
  90. {
  91. Result = 0;
  92. LockCount = &Result;
  93. }
  94. }
  95. ~GpLock()
  96. {
  97. InterlockedDecrement(LockCount);
  98. }
  99. BOOL IsValid() const
  100. {
  101. return (Result == 0);
  102. }
  103. BOOL LockFailed() const
  104. {
  105. return (Result != 0);
  106. }
  107. VOID MakePermanentLock()
  108. {
  109. // This is useful when deleting an object. First, lock it, then
  110. // leave it in a locked state. Technically the memory will be released
  111. // and available for reuse, but the ObjectLock will still be left in
  112. // a locked state.
  113. LockCount = &Result;
  114. }
  115. private:
  116. LONG Result;
  117. LONG *LockCount;
  118. };
  119. }
  120. #endif // !_LOCKABLE_HPP