1 | // -*- C++ -*- |
---|
2 | |
---|
3 | #ifndef _THREADPOOLHEAP_H_ |
---|
4 | #define _THREADPOOLHEAP_H_ |
---|
5 | |
---|
6 | #include <assert.h> |
---|
7 | |
---|
8 | #include "array.h" |
---|
9 | #include "cpuinfo.h" |
---|
10 | |
---|
11 | extern volatile int anyThreadCreated; |
---|
12 | |
---|
13 | namespace Hoard { |
---|
14 | |
---|
15 | template <int NumThreads, |
---|
16 | int NumHeaps, |
---|
17 | class PerThreadHeap_> |
---|
18 | class ThreadPoolHeap : public PerThreadHeap_ { |
---|
19 | public: |
---|
20 | |
---|
21 | typedef PerThreadHeap_ PerThreadHeap; |
---|
22 | |
---|
23 | enum { MaxThreads = NumThreads }; |
---|
24 | enum { NumThreadsMask = NumThreads - 1}; |
---|
25 | enum { NumHeapsMask = NumHeaps - 1}; |
---|
26 | |
---|
27 | HL::sassert<((NumHeaps & NumHeapsMask) == 0)> verifyPowerOfTwoHeaps; |
---|
28 | HL::sassert<((NumThreads & NumThreadsMask) == 0)> verifyPowerOfTwoThreads; |
---|
29 | |
---|
30 | enum { MaxHeaps = NumHeaps }; |
---|
31 | |
---|
32 | ThreadPoolHeap (void) |
---|
33 | { |
---|
34 | // Note: The tidmap values should be set externally. |
---|
35 | int j = 0; |
---|
36 | for (int i = 0; i < NumThreads; i++) { |
---|
37 | setTidMap(i, j % NumHeaps); |
---|
38 | j++; |
---|
39 | } |
---|
40 | } |
---|
41 | |
---|
42 | inline PerThreadHeap& getHeap (void) { |
---|
43 | int tid; |
---|
44 | if (anyThreadCreated) { |
---|
45 | tid = HL::CPUInfo::getThreadId(); |
---|
46 | } else { |
---|
47 | tid = 0; |
---|
48 | } |
---|
49 | int heapno = _tidMap(tid & NumThreadsMask); |
---|
50 | return _heap(heapno); |
---|
51 | } |
---|
52 | |
---|
53 | inline void * malloc (size_t sz) { |
---|
54 | return getHeap().malloc (sz); |
---|
55 | } |
---|
56 | |
---|
57 | inline void free (void * ptr) { |
---|
58 | getHeap().free (ptr); |
---|
59 | } |
---|
60 | |
---|
61 | inline void clear (void) { |
---|
62 | getHeap().clear(); |
---|
63 | } |
---|
64 | |
---|
65 | inline size_t getSize (void * ptr) { |
---|
66 | return PerThreadHeap::getSize (ptr); |
---|
67 | } |
---|
68 | |
---|
69 | void setTidMap (int index, int value) { |
---|
70 | assert ((value >= 0) && (value < MaxHeaps)); |
---|
71 | _tidMap(index) = value; |
---|
72 | } |
---|
73 | |
---|
74 | int getTidMap (int index) const { |
---|
75 | return _tidMap(index); |
---|
76 | } |
---|
77 | |
---|
78 | void setInusemap (int index, int value) { |
---|
79 | _inUseMap(index) = value; |
---|
80 | } |
---|
81 | |
---|
82 | int getInusemap (int index) const { |
---|
83 | return _inUseMap(index); |
---|
84 | } |
---|
85 | |
---|
86 | |
---|
87 | private: |
---|
88 | |
---|
89 | /// Which heap is assigned to which thread, indexed by thread. |
---|
90 | Array<MaxThreads, int> _tidMap; |
---|
91 | |
---|
92 | /// Which heap is in use (a reference count). |
---|
93 | Array<MaxHeaps, int> _inUseMap; |
---|
94 | |
---|
95 | /// The array of heaps we choose from. |
---|
96 | Array<MaxHeaps, PerThreadHeap> _heap; |
---|
97 | |
---|
98 | }; |
---|
99 | |
---|
100 | } |
---|
101 | |
---|
102 | #endif |
---|