home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / MS_VC.50 / VC / MFC / INCLUDE / AFXTLS_.H < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-30  |  6.5 KB  |  241 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1997 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. inline CSimpleList::CSimpleList(int nNextOffset)
  58.     { m_pHead = NULL; m_nNextOffset = nNextOffset; }
  59. inline void CSimpleList::Construct(int nNextOffset)
  60.     { ASSERT(m_pHead == NULL); m_nNextOffset = nNextOffset; }
  61. inline BOOL CSimpleList::IsEmpty() const
  62.     { return m_pHead == NULL; }
  63. inline void** CSimpleList::GetNextPtr(void* p) const
  64.     { ASSERT(p != NULL); return (void**)((BYTE*)p+m_nNextOffset); }
  65. inline void CSimpleList::RemoveAll()
  66.     { m_pHead = NULL; }
  67. inline void* CSimpleList::GetHead() const
  68.     { return m_pHead; }
  69. 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 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. #endif
  137.     virtual ~CNoTrackObject() { }
  138. };
  139.  
  140. class CThreadLocalObject
  141. {
  142. public:
  143. // Attributes
  144.     CNoTrackObject* GetData(CNoTrackObject* (AFXAPI* pfnCreateObject)());
  145.     CNoTrackObject* GetDataNA();
  146.  
  147. // Implementation
  148.     int m_nSlot;
  149.     ~CThreadLocalObject();
  150. };
  151.  
  152. class CProcessLocalObject
  153. {
  154. public:
  155. // Attributes
  156.     CNoTrackObject* GetData(CNoTrackObject* (AFXAPI* pfnCreateObject)());
  157.  
  158. // Implementation
  159.     CNoTrackObject* volatile m_pObject;
  160.     ~CProcessLocalObject();
  161. };
  162.  
  163. template<class TYPE>
  164. class CThreadLocal : public CThreadLocalObject
  165. {
  166. // Attributes
  167. public:
  168.     inline TYPE* GetData()
  169.     {
  170.         TYPE* pData = (TYPE*)CThreadLocalObject::GetData(&CreateObject);
  171.         ASSERT(pData != NULL);
  172.         return pData;
  173.     }
  174.     inline TYPE* GetDataNA()
  175.     {
  176.         TYPE* pData = (TYPE*)CThreadLocalObject::GetDataNA();
  177.         return pData;
  178.     }
  179.     inline operator TYPE*()
  180.         { return GetData(); }
  181.     inline TYPE* operator->()
  182.         { return GetData(); }
  183.  
  184. // Implementation
  185. public:
  186.     static CNoTrackObject* AFXAPI CreateObject()
  187.         { return new TYPE; }
  188. };
  189.  
  190. #define THREAD_LOCAL(class_name, ident_name) \
  191.     AFX_DATADEF CThreadLocal<class_name> ident_name;
  192. #define EXTERN_THREAD_LOCAL(class_name, ident_name) \
  193.     extern AFX_DATA THREAD_LOCAL(class_name, ident_name)
  194.  
  195. template<class TYPE>
  196. class CProcessLocal : public CProcessLocalObject
  197. {
  198. // Attributes
  199. public:
  200.     inline TYPE* GetData()
  201.     {
  202.         TYPE* pData = (TYPE*)CProcessLocalObject::GetData(&CreateObject);
  203.         ASSERT(pData != NULL);
  204.         return pData;
  205.     }
  206.     inline TYPE* GetDataNA()
  207.         { return (TYPE*)m_pObject; }
  208.     inline operator TYPE*()
  209.         { return GetData(); }
  210.     inline TYPE* operator->()
  211.         { return GetData(); }
  212.  
  213. // Implementation
  214. public:
  215.     static CNoTrackObject* AFXAPI CreateObject()
  216.         { return new TYPE; }
  217. };
  218.  
  219. #define PROCESS_LOCAL(class_name, ident_name) \
  220.     AFX_DATADEF CProcessLocal<class_name> ident_name;
  221. #define EXTERN_PROCESS_LOCAL(class_name, ident_name) \
  222.     extern AFX_DATA PROCESS_LOCAL(class_name, ident_name)
  223.  
  224. /////////////////////////////////////////////////////////////////////////////
  225.  
  226. void AFXAPI AfxInitLocalData(HINSTANCE hInstInit);
  227. void AFXAPI AfxTermLocalData(HINSTANCE hInstTerm, BOOL bAll = FALSE);
  228. void AFXAPI AfxTlsAddRef();
  229. void AFXAPI AfxTlsRelease();
  230.  
  231. #ifdef _AFX_PACKING
  232. #pragma pack(pop)
  233. #endif
  234.  
  235. #undef AFX_DATA
  236. #define AFX_DATA
  237.  
  238. #endif //__AFXTLS_H__
  239.  
  240. /////////////////////////////////////////////////////////////////////////////
  241.