home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / mfc / include / afxtls_.h < prev    next >
C/C++ Source or Header  |  1998-06-16  |  7KB  |  244 lines

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