1 | /* -*- C++ -*- */ |
---|
2 | |
---|
3 | #ifndef _THREADHEAP_H_ |
---|
4 | #define _THREADHEAP_H_ |
---|
5 | |
---|
6 | #include <assert.h> |
---|
7 | #include <new> |
---|
8 | |
---|
9 | #if !defined(_WIN32) |
---|
10 | #include <pthread.h> |
---|
11 | #endif |
---|
12 | |
---|
13 | #if defined(__SVR4) // Solaris |
---|
14 | extern "C" unsigned int lwp_self(void); |
---|
15 | #endif |
---|
16 | |
---|
17 | /* |
---|
18 | |
---|
19 | A ThreadHeap comprises NumHeaps "per-thread" heaps. |
---|
20 | |
---|
21 | To pick a per-thread heap, the current thread id is hashed (mod NumHeaps). |
---|
22 | |
---|
23 | malloc gets memory from its hashed per-thread heap. |
---|
24 | free returns memory to its hashed per-thread heap. |
---|
25 | |
---|
26 | (This allows the per-thread heap to determine the return |
---|
27 | policy -- 'pure private heaps', 'private heaps with ownership', |
---|
28 | etc.) |
---|
29 | |
---|
30 | NB: We assume that the thread heaps are 'locked' as needed. */ |
---|
31 | |
---|
32 | namespace HL { |
---|
33 | |
---|
34 | template <int NumHeaps, class PerThreadHeap> |
---|
35 | class ThreadHeap : public PerThreadHeap { |
---|
36 | public: |
---|
37 | |
---|
38 | inline void * malloc (size_t sz) { |
---|
39 | int tid = getThreadId() % NumHeaps; |
---|
40 | assert (tid >= 0); |
---|
41 | assert (tid < NumHeaps); |
---|
42 | return getHeap(tid)->malloc (sz); |
---|
43 | } |
---|
44 | |
---|
45 | inline void free (void * ptr) { |
---|
46 | int tid = getThreadId() % NumHeaps; |
---|
47 | assert (tid >= 0); |
---|
48 | assert (tid < NumHeaps); |
---|
49 | getHeap(tid)->free (ptr); |
---|
50 | } |
---|
51 | |
---|
52 | inline size_t getSize (void * ptr) { |
---|
53 | int tid = getThreadId() % NumHeaps; |
---|
54 | assert (tid >= 0); |
---|
55 | assert (tid < NumHeaps); |
---|
56 | return getHeap(tid)->getSize (ptr); |
---|
57 | } |
---|
58 | |
---|
59 | |
---|
60 | private: |
---|
61 | |
---|
62 | // Access the given heap within the buffer. |
---|
63 | inline PerThreadHeap * getHeap (int index) { |
---|
64 | assert (index >= 0); |
---|
65 | assert (index < NumHeaps); |
---|
66 | return &ptHeaps[index]; |
---|
67 | } |
---|
68 | |
---|
69 | // Get a suitable thread id number. |
---|
70 | inline static volatile int getThreadId (void); |
---|
71 | |
---|
72 | PerThreadHeap ptHeaps[NumHeaps]; |
---|
73 | |
---|
74 | }; |
---|
75 | |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | // A platform-dependent way to get a thread id. |
---|
80 | |
---|
81 | // Include the necessary platform-dependent crud. |
---|
82 | #if defined(WIN32) || defined(__WIN32__) || defined(_WIN32) |
---|
83 | #ifndef WIN32 |
---|
84 | #define WIN32 1 |
---|
85 | #endif |
---|
86 | #include <windows.h> |
---|
87 | #include <process.h> |
---|
88 | #endif |
---|
89 | |
---|
90 | template <int NumHeaps, class PerThreadHeap> |
---|
91 | inline volatile int |
---|
92 | HL::ThreadHeap<NumHeaps, PerThreadHeap>::getThreadId (void) { |
---|
93 | #if defined(WIN32) |
---|
94 | // It looks like thread id's are always multiples of 4, so... |
---|
95 | int tid = GetCurrentThreadId() >> 2; |
---|
96 | // Now hash in some of the first bits. |
---|
97 | // return (tid & ~(1024-1)) ^ tid; |
---|
98 | return tid; |
---|
99 | #endif |
---|
100 | #if defined(__BEOS__) |
---|
101 | return find_thread(0); |
---|
102 | #endif |
---|
103 | #if defined(__linux) |
---|
104 | // Consecutive thread id's in Linux are 1024 apart; |
---|
105 | // dividing off the 1024 gives us an appropriate thread id. |
---|
106 | return (int) pthread_self() >> 10; // (>> 10 = / 1024) |
---|
107 | #endif |
---|
108 | #if defined(__SVR4) |
---|
109 | // printf ("lwp_self = %d\n", (int) lwp_self()); |
---|
110 | return (int) lwp_self(); |
---|
111 | #endif |
---|
112 | #if defined(__APPLE__) |
---|
113 | return (int) pthread_self(); |
---|
114 | #endif |
---|
115 | #if defined(POSIX) |
---|
116 | return (int) pthread_self(); |
---|
117 | #endif |
---|
118 | #if USE_SPROC |
---|
119 | // This hairiness has the same effect as calling getpid(), |
---|
120 | // but it's MUCH faster since it avoids making a system call |
---|
121 | // and just accesses the sproc-local data directly. |
---|
122 | int pid = (int) PRDA->sys_prda.prda_sys.t_pid; |
---|
123 | return pid; |
---|
124 | #endif |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | #endif |
---|