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.

222 lines
7.2 KiB

  1. /***************************************************************************\
  2. *
  3. * File: Thread.h
  4. *
  5. * Description:
  6. * This file declares the main Thread that is maintained by the
  7. * ResourceManager to store per-thread information.
  8. *
  9. *
  10. * History:
  11. * 4/18/2000: JStall: Created
  12. *
  13. * Copyright (C) 2000 by Microsoft Corporation. All rights reserved.
  14. *
  15. \***************************************************************************/
  16. #if !defined(SERVICES__Thread_h__INCLUDED)
  17. #define SERVICES__Thread_h__INCLUDED
  18. #pragma once
  19. #include "GdiCache.h"
  20. #include "Buffer.h"
  21. #include "ComManager.h"
  22. class Context;
  23. class Thread;
  24. class SubThread;
  25. class ThreadPackBuilder;
  26. struct ReturnMem
  27. {
  28. ReturnMem * pNext;
  29. int cbSize;
  30. };
  31. const POOLBLOCK_SIZE = 128;
  32. /***************************************************************************\
  33. *****************************************************************************
  34. *
  35. * Thread provides a mechanism to store per-thread information. This object
  36. * is only used by its own thread and should not be directly accessed from
  37. * other threads. This object is created when a thread is registered with
  38. * the ResourceManager to create a Context.
  39. *
  40. *****************************************************************************
  41. \***************************************************************************/
  42. #pragma warning(disable:4324) // structure was padded due to __declspec(align())
  43. class Thread : public ListNodeT<Thread>
  44. {
  45. // Construction
  46. public:
  47. inline Thread();
  48. ~Thread();
  49. static HRESULT Build(BOOL fSRT, Thread ** ppthrNew);
  50. // Operations
  51. public:
  52. enum ESlot {
  53. slCore = 0, // Core
  54. slCOUNT // Number of sub-contexts
  55. };
  56. inline BOOL IsSRT() const;
  57. inline void Lock();
  58. inline BOOL Unlock();
  59. inline void MarkOrphaned();
  60. inline GdiCache * GetGdiCache() const;
  61. inline BufferManager *
  62. GetBufferManager() const;
  63. inline ComManager* GetComManager() const;
  64. inline Context * GetContext() const;
  65. inline void SetContext(Context * pContext);
  66. inline SubThread * GetST(ESlot slot) const;
  67. inline void xwLeftContextLockNL();
  68. inline TempHeap * GetTempHeap() const;
  69. ReturnMem * AllocMemoryNL(int cbSize);
  70. inline void ReturnMemoryNL(ReturnMem * prMem);
  71. // Implementation
  72. #if DBG
  73. public:
  74. virtual void DEBUG_AssertValid() const;
  75. #endif
  76. // Data
  77. public:
  78. HRGN hrgnClip;
  79. protected:
  80. void xwDestroySubThreads();
  81. static void CALLBACK xwContextFinalUnlockProc(BaseObject * pobj, void * pvData);
  82. void ReturnAllMemoryNL();
  83. struct PoolMem : public ReturnMem
  84. {
  85. BYTE rgbData[POOLBLOCK_SIZE - sizeof(ReturnMem)];
  86. };
  87. AllocPoolNL<PoolMem, 512>
  88. m_poolReturn; // Pool of reserved memory
  89. GdiCache m_GdiCache;
  90. BufferManager m_manBuffer;
  91. ComManager m_manCOM;
  92. Context * m_pContext; // Current Context for this Thread
  93. TempHeap m_heapTemp; // Temporary heap
  94. GInterlockedList<ReturnMem>
  95. m_lstReturn; // Returned memory list
  96. SubThread* m_rgSTs[slCOUNT]; // Sub-context information
  97. UINT m_cRef; // Outstanding references on this Thread
  98. UINT m_cMemAlloc; // Outstanding ReturnMem allocations
  99. BOOL m_fSRT:1; // Thread is an SRT
  100. BOOL m_fStartDestroy:1; // Thread has started destruction
  101. BOOL m_fDestroySubThreads:1; // Sub-threads have been destroyed
  102. BOOL m_fOrphaned:1; // Thread was orphaned
  103. };
  104. #pragma warning(default:4324) // structure was padded due to __declspec(align())
  105. /***************************************************************************\
  106. *****************************************************************************
  107. *
  108. * SubThread defines a "extensibility" mechanism that allows individual
  109. * projects in DirectUser to provide additional data to store on the thread.
  110. * To use this, the project must add a new slot in Thread, derive a class
  111. * from SubThread that is created per Thread instance, and derive a class
  112. * from ThreadPackBuilder to register the extension.
  113. *
  114. *****************************************************************************
  115. \***************************************************************************/
  116. class SubThread
  117. {
  118. // Construction
  119. public:
  120. virtual ~SubThread() { }
  121. virtual HRESULT Create() { return S_OK; }
  122. // Operations
  123. public:
  124. inline Thread * GetParent() const { return m_pParent; }
  125. inline void SetParent(Thread * pParent);
  126. virtual void xwLeftContextLockNL() { }
  127. // Implementation
  128. #if DBG
  129. public:
  130. virtual void DEBUG_AssertValid() const;
  131. #endif
  132. // Data
  133. protected:
  134. Thread * m_pParent;
  135. };
  136. /***************************************************************************\
  137. *****************************************************************************
  138. *
  139. * ThreadPackBuilder registers an SubThread "extension" to be created
  140. * whenever a new Thread is created. The constructor is expected to set the
  141. * slot corresponding to the ESlot value.
  142. *
  143. *****************************************************************************
  144. \***************************************************************************/
  145. class ThreadPackBuilder
  146. {
  147. // Construction
  148. public:
  149. // Operations
  150. public:
  151. virtual SubThread* New(Thread * pThread) PURE;
  152. static inline ThreadPackBuilder *
  153. GetBuilder(Thread::ESlot slot);
  154. // Data
  155. protected:
  156. static ThreadPackBuilder *
  157. s_rgBuilders[Thread::slCOUNT];
  158. };
  159. #define IMPLEMENT_SUBTHREAD(id, obj) \
  160. class obj##Builder : public ThreadPackBuilder \
  161. { \
  162. public: \
  163. virtual SubThread * New(Thread * pParent) \
  164. { \
  165. SubThread * psc = ProcessNew(obj); \
  166. if (psc != NULL) { \
  167. psc->SetParent(pParent); \
  168. } \
  169. return psc; \
  170. } \
  171. } g_##obj##B \
  172. #define PREINIT_SUBTHREAD(obj) \
  173. class obj##Builder; \
  174. extern obj##Builder g_##obj##B \
  175. #define INIT_SUBTHREAD(obj) \
  176. (ThreadPackBuilder *) &g_##obj##B \
  177. inline Thread * GetThread();
  178. inline BOOL IsInitThread();
  179. #include "Thread.inl"
  180. #endif // SERVICES__Thread_h__INCLUDED