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.

144 lines
4.8 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 2000
  3. Module Name:
  4. SContext.hxx
  5. Abstract:
  6. Class definitions for the server side context handles.
  7. Author:
  8. Kamen Moutafov [KamenM]
  9. Revision History:
  10. KamenM Sep 2000 Created
  11. --*/
  12. #if _MSC_VER >= 1200
  13. #pragma once
  14. #endif
  15. #ifndef __SCONTEXT_HXX_
  16. #define __SCONTEXT_HXX_
  17. class ServerContextHandle
  18. {
  19. public:
  20. ServerContextHandle (
  21. IN void *CtxGuard
  22. )
  23. {
  24. // leave ContextChain uninitialized for now
  25. UserContext = NULL;
  26. UserRunDown = NULL;
  27. this->CtxGuard = CtxGuard;
  28. RpcpMemorySet(&WireContext, 0, sizeof(WIRE_CONTEXT));
  29. OwnerSID = NULL;
  30. ReferenceCount = 1;
  31. Flags = ContextPendingAlloc;
  32. DeadlockTag = 0;
  33. }
  34. inline void
  35. AddReference (
  36. void
  37. )
  38. {
  39. long LocalRefCount;
  40. ASSERT(ReferenceCount >= 0);
  41. LocalRefCount = InterlockedIncrement(&ReferenceCount);
  42. LogEvent(SU_CTXHANDLE, EV_INC, this, 0, LocalRefCount, 1, 0);
  43. }
  44. inline long
  45. RemoveReference (
  46. void
  47. )
  48. {
  49. long LocalRefCount;
  50. LocalRefCount = InterlockedDecrement(&ReferenceCount);
  51. LogEvent(SU_CTXHANDLE, EV_DEC, this, 0, LocalRefCount, 1, 0);
  52. ASSERT(LocalRefCount >= 0);
  53. return LocalRefCount;
  54. }
  55. LIST_ENTRY ContextChain; // entry for the context chain
  56. void *UserContext; // context for the user
  57. NDR_RUNDOWN UserRunDown; // user routine to call
  58. void *CtxGuard;
  59. WIRE_CONTEXT WireContext;
  60. // a context handle is not locked until it is put into ContextCompletedAlloc
  61. // state and inserted into the collection.
  62. SWMRLock Lock;
  63. PSID OwnerSID;
  64. // Not protected. Use interlocks. There is one lifetime reference,
  65. // and one or more usage reference. Two paths may clear the lifetime
  66. // reference - the rundown and the marshall path. Both have the lock
  67. // and need to check for the deleted flag to avoid the case where both
  68. // take the lifetime reference
  69. // Whenever the lifetime reference is taken away, the object must be
  70. // removed from the collection so that no new code picks it up
  71. long ReferenceCount;
  72. // All contexts in the collection must have the ContextCompletedAlloc
  73. // flag. Since by the time they enter the collection, this flags
  74. // is constant, no synchronization discipline are necessary for this
  75. // flag
  76. static const long ContextPendingAlloc = 0;
  77. static const long ContextCompletedAlloc = 1;
  78. static const long ContextAllocState = 1;
  79. // Set in the rundown and marshall paths. Checked in the
  80. // the marshall path. All those paths
  81. // set it/check it under the protection of the
  82. // collection lock. If this flag is set, the context is already
  83. // removed, and the lifetime reference is taken away - don't remove
  84. // it and don't take the lifetime reference
  85. // The opposite is also true - if the context handle is in the
  86. // collection, this flag must not be set
  87. static const long ContextRemovedFromCollection = 2;
  88. static const long ContextRemovedFromCollectionMask = 2;
  89. // Set in the rundown path. Checked whenever the refcount drops
  90. // to 0. In both cases protected by the collection lock
  91. static const long ContextNeedsRundown = 4;
  92. static const long ContextNeedsRundownMask = 4;
  93. // the first time newly created context is marshalled, this flag
  94. // is raised. The first time it is unmarshalled, it is taken down
  95. // This allows us to figure out which contexts have been created,
  96. // but not returned to the client in case marshalling fails and treat
  97. // them appropriately. Set in the marshalling path. Checked in the
  98. // unmarshall path. In both cases this is done under the protection
  99. // of the lock
  100. static const long ContextNewlyCreated = 8;
  101. static const long ContextNewlyCreatedMask = 8;
  102. // can be ContextDeleted, ContextPendingAlloc/ContextCompletedAlloc,
  103. // ContextNeedsRundown, ContextNewlyCreated
  104. // Protected by the collection lock for all context handles inserted
  105. // in the collection. No protection needed for the others as no
  106. // multithreaded access is possible
  107. unsigned long Flags;
  108. // When multiple Context handles are active in a call, the possibility
  109. // of cross-call deadlock exists. This field is used to detect possible
  110. // deadlocks when unmarshalling a context handle. The routines AquireDeadlockProtection
  111. // and ReleaseDeadlockProtection use this value to detect possible deadlock,
  112. // see those routines for an explanation of the algorithm.
  113. volatile long DeadlockTag;
  114. };
  115. #endif // __SCONTEXT_HXX