Eneboo - Documentación para desarrolladores
|
00001 // -*- C++ -*- 00002 00003 #ifndef _THREADPOOLHEAP_H_ 00004 #define _THREADPOOLHEAP_H_ 00005 00006 #include <cassert> 00007 00008 #include "array.h" 00009 #include "cpuinfo.h" 00010 00011 extern volatile int anyThreadCreated; 00012 00013 namespace Hoard { 00014 00015 template <int NumThreads, 00016 int NumHeaps, 00017 class PerThreadHeap_> 00018 class ThreadPoolHeap : public PerThreadHeap_ { 00019 public: 00020 00021 typedef PerThreadHeap_ PerThreadHeap; 00022 00023 enum { MaxThreads = NumThreads }; 00024 enum { NumThreadsMask = NumThreads - 1}; 00025 enum { NumHeapsMask = NumHeaps - 1}; 00026 00027 HL::sassert<((NumHeaps & NumHeapsMask) == 0)> verifyPowerOfTwoHeaps; 00028 HL::sassert<((NumThreads & NumThreadsMask) == 0)> verifyPowerOfTwoThreads; 00029 00030 enum { MaxHeaps = NumHeaps }; 00031 00032 ThreadPoolHeap (void) 00033 { 00034 // Note: The tidmap values should be set externally. 00035 int j = 0; 00036 for (int i = 0; i < NumThreads; i++) { 00037 setTidMap(i, j % NumHeaps); 00038 j++; 00039 } 00040 } 00041 00042 inline PerThreadHeap& getHeap (void) { 00043 int tid; 00044 if (anyThreadCreated) { 00045 tid = HL::CPUInfo::getThreadId(); 00046 } else { 00047 tid = 0; 00048 } 00049 int heapno = _tidMap(tid & NumThreadsMask); 00050 return _heap(heapno); 00051 } 00052 00053 inline void * malloc (size_t sz) { 00054 return getHeap().malloc (sz); 00055 } 00056 00057 inline void free (void * ptr) { 00058 getHeap().free (ptr); 00059 } 00060 00061 inline void clear (void) { 00062 getHeap().clear(); 00063 } 00064 00065 inline size_t getSize (void * ptr) { 00066 return PerThreadHeap::getSize (ptr); 00067 } 00068 00069 void setTidMap (int index, int value) { 00070 assert ((value >= 0) && (value < MaxHeaps)); 00071 _tidMap(index) = value; 00072 } 00073 00074 int getTidMap (int index) const { 00075 return _tidMap(index); 00076 } 00077 00078 void setInusemap (int index, int value) { 00079 _inUseMap(index) = value; 00080 } 00081 00082 int getInusemap (int index) const { 00083 return _inUseMap(index); 00084 } 00085 00086 00087 private: 00088 00090 Array<MaxThreads, int> _tidMap; 00091 00093 Array<MaxHeaps, int> _inUseMap; 00094 00096 Array<MaxHeaps, PerThreadHeap> _heap; 00097 00098 }; 00099 00100 } 00101 00102 #endif