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.

243 lines
6.7 KiB

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10. #ifndef __AFXTLS_H__
  11. #define __AFXTLS_H__
  12. #ifdef _AFX_PACKING
  13. #pragma pack(push, _AFX_PACKING)
  14. #endif
  15. #undef AFX_DATA
  16. #define AFX_DATA AFX_CORE_DATA
  17. // Classes declared in this file
  18. class CSimpleList;
  19. class CThreadSlotData; // for manipulationg thread local storage
  20. class CThreadLocalObject; // for storing thread local data
  21. class CProcessLocalObject; // for storing thread local data
  22. class CNoTrackObject;
  23. // template class CTypedSimpleList<>
  24. // template class CThreadLocal<>
  25. // template class CProcessLocal<>
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CSimpleList (simple/small subset of CList)
  28. class CSimpleList
  29. {
  30. public:
  31. CSimpleList(int nNextOffset = 0);
  32. void Construct(int nNextOffset);
  33. // Operations
  34. BOOL IsEmpty() const;
  35. void AddHead(void* p);
  36. void RemoveAll();
  37. void* GetHead() const;
  38. void* GetNext(void* p) const;
  39. BOOL Remove(void* p);
  40. // Implementation
  41. void* m_pHead;
  42. size_t m_nNextOffset;
  43. void** GetNextPtr(void* p) const; // somewhat trusting...
  44. };
  45. AFX_INLINE CSimpleList::CSimpleList(int nNextOffset)
  46. { m_pHead = NULL; m_nNextOffset = nNextOffset; }
  47. AFX_INLINE void CSimpleList::Construct(int nNextOffset)
  48. { ASSERT(m_pHead == NULL); m_nNextOffset = nNextOffset; }
  49. AFX_INLINE BOOL CSimpleList::IsEmpty() const
  50. { return m_pHead == NULL; }
  51. AFX_INLINE void** CSimpleList::GetNextPtr(void* p) const
  52. { ASSERT(p != NULL); return (void**)((BYTE*)p+m_nNextOffset); }
  53. AFX_INLINE void CSimpleList::RemoveAll()
  54. { m_pHead = NULL; }
  55. AFX_INLINE void* CSimpleList::GetHead() const
  56. { return m_pHead; }
  57. AFX_INLINE void* CSimpleList::GetNext(void* prevElement) const
  58. { return *GetNextPtr(prevElement); }
  59. template<class TYPE>
  60. class CTypedSimpleList : public CSimpleList
  61. {
  62. public:
  63. CTypedSimpleList(int nNextOffset = 0)
  64. : CSimpleList(nNextOffset) { }
  65. void AddHead(TYPE p)
  66. { CSimpleList::AddHead(p); }
  67. TYPE GetHead()
  68. { return (TYPE)CSimpleList::GetHead(); }
  69. TYPE GetNext(TYPE p)
  70. { return (TYPE)CSimpleList::GetNext(p); }
  71. BOOL Remove(TYPE p)
  72. { return CSimpleList::Remove((TYPE)p); }
  73. operator TYPE()
  74. { return (TYPE)CSimpleList::GetHead(); }
  75. };
  76. /////////////////////////////////////////////////////////////////////////////
  77. // CThreadSlotData - manages owned array of "slots" for thread local storage
  78. struct CThreadData; // private to implementation
  79. struct CSlotData; // private to implementation
  80. class CThreadSlotData
  81. {
  82. public:
  83. CThreadSlotData();
  84. // Operations
  85. int AllocSlot();
  86. void FreeSlot(int nSlot);
  87. void* GetValue(int nSlot);
  88. void SetValue(int nSlot, void* pValue);
  89. // delete all values in process/thread
  90. void DeleteValues(HINSTANCE hInst, BOOL bAll = FALSE);
  91. // assign instance handle to just constructed slots
  92. void AssignInstance(HINSTANCE hInst);
  93. // Implementation
  94. DWORD m_tlsIndex; // used to access system thread-local storage
  95. int m_nAlloc; // number of slots allocated (in UINTs)
  96. int m_nRover; // (optimization) for quick finding of free slots
  97. int m_nMax; // size of slot table below (in bits)
  98. CSlotData* m_pSlotData; // state of each slot (allocated or not)
  99. CTypedSimpleList<CThreadData*> m_list; // list of CThreadData structures
  100. CRITICAL_SECTION m_sect;
  101. void* GetThreadValue(int nSlot); // special version for threads only!
  102. void* PASCAL operator new(size_t, void* p)
  103. { return p; }
  104. void DeleteValues(CThreadData* pData, HINSTANCE hInst);
  105. ~CThreadSlotData();
  106. };
  107. class AFX_NOVTABLE CNoTrackObject
  108. {
  109. public:
  110. void* PASCAL operator new(size_t nSize);
  111. void PASCAL operator delete(void*);
  112. #if defined(_DEBUG) && !defined(_AFX_NO_DEBUG_CRT)
  113. void* PASCAL operator new(size_t nSize, LPCSTR, int);
  114. #if (_MSC_VER >= 1200) && (_MFC_VER >= 0x0600)
  115. void PASCAL operator delete(void* pObject, LPCSTR, int);
  116. #endif
  117. #endif
  118. virtual ~CNoTrackObject() { }
  119. };
  120. class AFX_NOVTABLE CThreadLocalObject
  121. {
  122. public:
  123. // Attributes
  124. CNoTrackObject* GetData(CNoTrackObject* (AFXAPI* pfnCreateObject)());
  125. CNoTrackObject* GetDataNA();
  126. // Implementation
  127. int m_nSlot;
  128. ~CThreadLocalObject();
  129. };
  130. class AFX_NOVTABLE CProcessLocalObject
  131. {
  132. public:
  133. // Attributes
  134. CNoTrackObject* GetData(CNoTrackObject* (AFXAPI* pfnCreateObject)());
  135. // Implementation
  136. CNoTrackObject* volatile m_pObject;
  137. ~CProcessLocalObject();
  138. };
  139. template<class TYPE>
  140. class CThreadLocal : public CThreadLocalObject
  141. {
  142. // Attributes
  143. public:
  144. AFX_INLINE TYPE* GetData()
  145. {
  146. TYPE* pData = (TYPE*)CThreadLocalObject::GetData(&CreateObject);
  147. ASSERT(pData != NULL);
  148. return pData;
  149. }
  150. AFX_INLINE TYPE* GetDataNA()
  151. {
  152. TYPE* pData = (TYPE*)CThreadLocalObject::GetDataNA();
  153. return pData;
  154. }
  155. AFX_INLINE operator TYPE*()
  156. { return GetData(); }
  157. AFX_INLINE TYPE* operator->()
  158. { return GetData(); }
  159. // Implementation
  160. public:
  161. static CNoTrackObject* AFXAPI CreateObject()
  162. { return new TYPE; }
  163. };
  164. #define THREAD_LOCAL(class_name, ident_name) \
  165. AFX_DATADEF CThreadLocal<class_name> ident_name;
  166. #define EXTERN_THREAD_LOCAL(class_name, ident_name) \
  167. extern AFX_DATA THREAD_LOCAL(class_name, ident_name)
  168. template<class TYPE>
  169. class CProcessLocal : public CProcessLocalObject
  170. {
  171. // Attributes
  172. public:
  173. AFX_INLINE TYPE* GetData()
  174. {
  175. TYPE* pData = (TYPE*)CProcessLocalObject::GetData(&CreateObject);
  176. ASSERT(pData != NULL);
  177. return pData;
  178. }
  179. AFX_INLINE TYPE* GetDataNA()
  180. { return (TYPE*)m_pObject; }
  181. AFX_INLINE operator TYPE*()
  182. { return GetData(); }
  183. AFX_INLINE TYPE* operator->()
  184. { return GetData(); }
  185. // Implementation
  186. public:
  187. static CNoTrackObject* AFXAPI CreateObject()
  188. { return new TYPE; }
  189. };
  190. #define PROCESS_LOCAL(class_name, ident_name) \
  191. AFX_DATADEF CProcessLocal<class_name> ident_name;
  192. #define EXTERN_PROCESS_LOCAL(class_name, ident_name) \
  193. extern AFX_DATA PROCESS_LOCAL(class_name, ident_name)
  194. /////////////////////////////////////////////////////////////////////////////
  195. void AFXAPI AfxInitLocalData(HINSTANCE hInstInit);
  196. void AFXAPI AfxTermLocalData(HINSTANCE hInstTerm, BOOL bAll = FALSE);
  197. void AFXAPI AfxTlsAddRef();
  198. void AFXAPI AfxTlsRelease();
  199. #ifdef _AFX_PACKING
  200. #pragma pack(pop)
  201. #endif
  202. #undef AFX_DATA
  203. #define AFX_DATA
  204. #endif //__AFXTLS_H__
  205. /////////////////////////////////////////////////////////////////////////////