sl@0
|
1 |
/*
|
sl@0
|
2 |
* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
3 |
* All rights reserved.
|
sl@0
|
4 |
* This component and the accompanying materials are made available
|
sl@0
|
5 |
* under the terms of "Eclipse Public License v1.0"
|
sl@0
|
6 |
* which accompanies this distribution, and is available
|
sl@0
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* Initial Contributors:
|
sl@0
|
10 |
* Nokia Corporation - initial contribution.
|
sl@0
|
11 |
*
|
sl@0
|
12 |
* Contributors:
|
sl@0
|
13 |
*
|
sl@0
|
14 |
* Description:
|
sl@0
|
15 |
* Name : threadglobals.h
|
sl@0
|
16 |
* Part of : PThread library
|
sl@0
|
17 |
* Data structures needed for ptherad library
|
sl@0
|
18 |
* Version:
|
sl@0
|
19 |
*
|
sl@0
|
20 |
*/
|
sl@0
|
21 |
|
sl@0
|
22 |
|
sl@0
|
23 |
|
sl@0
|
24 |
#ifndef THREADGLOBALS_H
|
sl@0
|
25 |
#define THREADGLOBALS_H
|
sl@0
|
26 |
|
sl@0
|
27 |
#include <pthread.h>
|
sl@0
|
28 |
#include <e32std.h>
|
sl@0
|
29 |
#include <limits.h>
|
sl@0
|
30 |
#include "sysif.h"
|
sl@0
|
31 |
|
sl@0
|
32 |
#include <e32debug.h>
|
sl@0
|
33 |
|
sl@0
|
34 |
//the semaphore structure
|
sl@0
|
35 |
struct _sem_t
|
sl@0
|
36 |
{
|
sl@0
|
37 |
enum sem_state
|
sl@0
|
38 |
{
|
sl@0
|
39 |
EInitialized,
|
sl@0
|
40 |
EDestroyed,
|
sl@0
|
41 |
EInvalid,
|
sl@0
|
42 |
};
|
sl@0
|
43 |
sem_state iState;
|
sl@0
|
44 |
int iCount; //iCount and iMutex pair needed
|
sl@0
|
45 |
//because of need for implementing trywait
|
sl@0
|
46 |
RMutex iMutex;
|
sl@0
|
47 |
RSemaphore iSemaphore;
|
sl@0
|
48 |
/*******************************************************************
|
sl@0
|
49 |
Overloading new and delete operators so that they will
|
sl@0
|
50 |
allocate and deallocare memory from/to the private heap of backend
|
sl@0
|
51 |
********************************************************************/
|
sl@0
|
52 |
inline TAny* operator new(TUint aSize, TAny* aBase) __NO_THROW
|
sl@0
|
53 |
{
|
sl@0
|
54 |
Mem::FillZ(aBase, aSize); return aBase;
|
sl@0
|
55 |
}
|
sl@0
|
56 |
|
sl@0
|
57 |
inline TAny* operator new(TUint aSize) __NO_THROW
|
sl@0
|
58 |
{
|
sl@0
|
59 |
return Backend()->Alloc(aSize);
|
sl@0
|
60 |
}
|
sl@0
|
61 |
|
sl@0
|
62 |
inline TAny* operator new(TUint aSize, TLeave)
|
sl@0
|
63 |
{
|
sl@0
|
64 |
TAny* ptr = Backend()->Alloc(aSize);
|
sl@0
|
65 |
if (ptr == NULL)
|
sl@0
|
66 |
{
|
sl@0
|
67 |
User::Leave(KErrNoMemory);
|
sl@0
|
68 |
}
|
sl@0
|
69 |
return ptr;
|
sl@0
|
70 |
}
|
sl@0
|
71 |
|
sl@0
|
72 |
inline TAny* operator new(TUint aSize, TUint aExtraSize) __NO_THROW
|
sl@0
|
73 |
{
|
sl@0
|
74 |
return Backend()->Alloc(aSize + aExtraSize);
|
sl@0
|
75 |
}
|
sl@0
|
76 |
|
sl@0
|
77 |
inline TAny* operator new(TUint aSize, TLeave, TUint aExtraSize)
|
sl@0
|
78 |
{
|
sl@0
|
79 |
TAny* ptr = Backend()->Alloc(aSize + aExtraSize);
|
sl@0
|
80 |
if (ptr == NULL)
|
sl@0
|
81 |
{
|
sl@0
|
82 |
User::Leave(KErrNoMemory);
|
sl@0
|
83 |
}
|
sl@0
|
84 |
return ptr;
|
sl@0
|
85 |
}
|
sl@0
|
86 |
|
sl@0
|
87 |
inline void operator delete(TAny *aPtr) __NO_THROW
|
sl@0
|
88 |
{
|
sl@0
|
89 |
Backend()->Free( aPtr );
|
sl@0
|
90 |
}
|
sl@0
|
91 |
};
|
sl@0
|
92 |
|
sl@0
|
93 |
typedef struct _sem_node
|
sl@0
|
94 |
{
|
sl@0
|
95 |
struct _sem_node *next;
|
sl@0
|
96 |
_sem_t *sem;
|
sl@0
|
97 |
/*******************************************************************
|
sl@0
|
98 |
Overloading new and delete operators so that they will
|
sl@0
|
99 |
allocate and deallocare memory from/to the private heap of backend
|
sl@0
|
100 |
********************************************************************/
|
sl@0
|
101 |
inline TAny* operator new(TUint aSize, TAny* aBase) __NO_THROW
|
sl@0
|
102 |
{
|
sl@0
|
103 |
Mem::FillZ(aBase, aSize); return aBase;
|
sl@0
|
104 |
}
|
sl@0
|
105 |
|
sl@0
|
106 |
inline TAny* operator new(TUint aSize) __NO_THROW
|
sl@0
|
107 |
{
|
sl@0
|
108 |
return Backend()->Alloc(aSize);
|
sl@0
|
109 |
}
|
sl@0
|
110 |
|
sl@0
|
111 |
inline TAny* operator new(TUint aSize, TLeave)
|
sl@0
|
112 |
{
|
sl@0
|
113 |
TAny* ptr = Backend()->Alloc(aSize);
|
sl@0
|
114 |
if (ptr == NULL)
|
sl@0
|
115 |
{
|
sl@0
|
116 |
User::Leave(KErrNoMemory);
|
sl@0
|
117 |
}
|
sl@0
|
118 |
return ptr;
|
sl@0
|
119 |
}
|
sl@0
|
120 |
|
sl@0
|
121 |
inline TAny* operator new(TUint aSize, TUint aExtraSize) __NO_THROW
|
sl@0
|
122 |
{
|
sl@0
|
123 |
return Backend()->Alloc(aSize + aExtraSize);
|
sl@0
|
124 |
}
|
sl@0
|
125 |
|
sl@0
|
126 |
inline TAny* operator new(TUint aSize, TLeave, TUint aExtraSize)
|
sl@0
|
127 |
{
|
sl@0
|
128 |
TAny* ptr = Backend()->Alloc(aSize + aExtraSize);
|
sl@0
|
129 |
if (ptr == NULL)
|
sl@0
|
130 |
{
|
sl@0
|
131 |
User::Leave(KErrNoMemory);
|
sl@0
|
132 |
}
|
sl@0
|
133 |
return ptr;
|
sl@0
|
134 |
}
|
sl@0
|
135 |
|
sl@0
|
136 |
inline void operator delete(TAny *aPtr) __NO_THROW
|
sl@0
|
137 |
{
|
sl@0
|
138 |
Backend()->Free( aPtr );
|
sl@0
|
139 |
}
|
sl@0
|
140 |
}_sem_node_t;
|
sl@0
|
141 |
|
sl@0
|
142 |
|
sl@0
|
143 |
// Thread default attributes
|
sl@0
|
144 |
#define DEFAULT_STACK_SIZE 0x2000
|
sl@0
|
145 |
#define DEFAULT_DETACH_STATE PTHREAD_CREATE_JOINABLE
|
sl@0
|
146 |
|
sl@0
|
147 |
#define THR_DISABLE_PTHREAD_TRACE 1
|
sl@0
|
148 |
|
sl@0
|
149 |
#define DEFAULT_THREAD_PRIORITY 100
|
sl@0
|
150 |
|
sl@0
|
151 |
#ifdef THR_DISABLE_PTHREAD_TRACE
|
sl@0
|
152 |
|
sl@0
|
153 |
#define THR_PRINTF(string) \
|
sl@0
|
154 |
{\
|
sl@0
|
155 |
}
|
sl@0
|
156 |
|
sl@0
|
157 |
#else //THR_DISABLE_PTHREAD_TRACE
|
sl@0
|
158 |
|
sl@0
|
159 |
#define THR_PRINTF(string) \
|
sl@0
|
160 |
{\
|
sl@0
|
161 |
#ifdef _DEBUG \
|
sl@0
|
162 |
RDebug::Printf(string);\
|
sl@0
|
163 |
#endif \ //_DEBUG
|
sl@0
|
164 |
}
|
sl@0
|
165 |
|
sl@0
|
166 |
#endif //THR_DISABLE_PTHREAD_TRACE
|
sl@0
|
167 |
|
sl@0
|
168 |
#define THR_NULL_ASSERT(x,val,format) \
|
sl@0
|
169 |
{ \
|
sl@0
|
170 |
if ( (x) == NULL ) \
|
sl@0
|
171 |
{ \
|
sl@0
|
172 |
THR_PRINTF(format); \
|
sl@0
|
173 |
return ((void*)0); \
|
sl@0
|
174 |
} \
|
sl@0
|
175 |
}
|
sl@0
|
176 |
|
sl@0
|
177 |
#define STAT_FLAG_SIZE (PTHREAD_KEYS_MAX / 32)
|
sl@0
|
178 |
|
sl@0
|
179 |
/* thread state. */
|
sl@0
|
180 |
enum
|
sl@0
|
181 |
{
|
sl@0
|
182 |
_THREAD_RUNNING,
|
sl@0
|
183 |
_THREAD_ZOMBIE
|
sl@0
|
184 |
};
|
sl@0
|
185 |
|
sl@0
|
186 |
/* Key status */
|
sl@0
|
187 |
enum
|
sl@0
|
188 |
{
|
sl@0
|
189 |
_KEY_UNUSED,
|
sl@0
|
190 |
_KEY_USED
|
sl@0
|
191 |
};
|
sl@0
|
192 |
|
sl@0
|
193 |
/* MainFlag */
|
sl@0
|
194 |
enum
|
sl@0
|
195 |
{
|
sl@0
|
196 |
_MAIN_THREAD=0,
|
sl@0
|
197 |
_NON_MAIN_THREAD
|
sl@0
|
198 |
};
|
sl@0
|
199 |
|
sl@0
|
200 |
typedef struct
|
sl@0
|
201 |
{
|
sl@0
|
202 |
destructor_routine destr;
|
sl@0
|
203 |
}_pthread_key_node;
|
sl@0
|
204 |
|
sl@0
|
205 |
// TLS Keys link list node
|
sl@0
|
206 |
typedef struct _pkey_node
|
sl@0
|
207 |
{
|
sl@0
|
208 |
int keyNumber;
|
sl@0
|
209 |
struct _pkey_node *next;
|
sl@0
|
210 |
void *tls;
|
sl@0
|
211 |
/*******************************************************************
|
sl@0
|
212 |
Overloading new and delete operators so that they will
|
sl@0
|
213 |
allocate and deallocare memory from/to the private heap of backend
|
sl@0
|
214 |
********************************************************************/
|
sl@0
|
215 |
inline TAny* operator new(TUint aSize, TAny* aBase) __NO_THROW
|
sl@0
|
216 |
{
|
sl@0
|
217 |
Mem::FillZ(aBase, aSize); return aBase;
|
sl@0
|
218 |
}
|
sl@0
|
219 |
|
sl@0
|
220 |
inline TAny* operator new(TUint aSize) __NO_THROW
|
sl@0
|
221 |
{
|
sl@0
|
222 |
return Backend()->Alloc(aSize);
|
sl@0
|
223 |
}
|
sl@0
|
224 |
|
sl@0
|
225 |
inline TAny* operator new(TUint aSize, TLeave)
|
sl@0
|
226 |
{
|
sl@0
|
227 |
TAny* ptr = Backend()->Alloc(aSize);
|
sl@0
|
228 |
if (ptr == NULL)
|
sl@0
|
229 |
{
|
sl@0
|
230 |
User::Leave(KErrNoMemory);
|
sl@0
|
231 |
}
|
sl@0
|
232 |
return ptr;
|
sl@0
|
233 |
}
|
sl@0
|
234 |
|
sl@0
|
235 |
inline TAny* operator new(TUint aSize, TUint aExtraSize) __NO_THROW
|
sl@0
|
236 |
{
|
sl@0
|
237 |
return Backend()->Alloc(aSize + aExtraSize);
|
sl@0
|
238 |
}
|
sl@0
|
239 |
|
sl@0
|
240 |
inline TAny* operator new(TUint aSize, TLeave, TUint aExtraSize)
|
sl@0
|
241 |
{
|
sl@0
|
242 |
TAny* ptr = Backend()->Alloc(aSize + aExtraSize);
|
sl@0
|
243 |
if (ptr == NULL)
|
sl@0
|
244 |
{
|
sl@0
|
245 |
User::Leave(KErrNoMemory);
|
sl@0
|
246 |
}
|
sl@0
|
247 |
return ptr;
|
sl@0
|
248 |
}
|
sl@0
|
249 |
|
sl@0
|
250 |
inline void operator delete(TAny *aPtr) __NO_THROW
|
sl@0
|
251 |
{
|
sl@0
|
252 |
Backend()->Free( aPtr );
|
sl@0
|
253 |
}
|
sl@0
|
254 |
}_pkey_node_t;
|
sl@0
|
255 |
|
sl@0
|
256 |
typedef struct _pthread_node _pthread_node_t;
|
sl@0
|
257 |
#ifdef __X86GCC__
|
sl@0
|
258 |
// MinGW GCC compiler does not like typedef struct definitions with no tag
|
sl@0
|
259 |
typedef struct _global_data_tag
|
sl@0
|
260 |
#else
|
sl@0
|
261 |
typedef struct _global_data_t
|
sl@0
|
262 |
#endif //__X86GCC__
|
sl@0
|
263 |
{
|
sl@0
|
264 |
_pthread_node_t *start;
|
sl@0
|
265 |
unsigned int threadCount;
|
sl@0
|
266 |
RMutex lockThreadTable;
|
sl@0
|
267 |
RMutex globalLockForMutex;
|
sl@0
|
268 |
// TLS Keys
|
sl@0
|
269 |
_pthread_key_node pthread_key_list[PTHREAD_KEYS_MAX];
|
sl@0
|
270 |
unsigned int statusflag[STAT_FLAG_SIZE];
|
sl@0
|
271 |
// Semaphore list
|
sl@0
|
272 |
_sem_node_t *semStart;
|
sl@0
|
273 |
RMutex lockSemTable;
|
sl@0
|
274 |
/*******************************************************************
|
sl@0
|
275 |
Overloading new and delete operators so that they will
|
sl@0
|
276 |
allocate and deallocare memory from/to the private heap of backend
|
sl@0
|
277 |
********************************************************************/
|
sl@0
|
278 |
|
sl@0
|
279 |
inline TAny* operator new(TUint aSize, TAny* aBase) __NO_THROW
|
sl@0
|
280 |
{
|
sl@0
|
281 |
Mem::FillZ(aBase, aSize); return aBase;
|
sl@0
|
282 |
}
|
sl@0
|
283 |
|
sl@0
|
284 |
inline TAny* operator new(TUint aSize) __NO_THROW
|
sl@0
|
285 |
{
|
sl@0
|
286 |
return Backend()->Alloc(aSize);
|
sl@0
|
287 |
}
|
sl@0
|
288 |
|
sl@0
|
289 |
inline TAny* operator new(TUint aSize, TLeave)
|
sl@0
|
290 |
{
|
sl@0
|
291 |
TAny* ptr = Backend()->Alloc(aSize);
|
sl@0
|
292 |
if (ptr == NULL)
|
sl@0
|
293 |
{
|
sl@0
|
294 |
User::Leave(KErrNoMemory);
|
sl@0
|
295 |
}
|
sl@0
|
296 |
return ptr;
|
sl@0
|
297 |
}
|
sl@0
|
298 |
|
sl@0
|
299 |
inline TAny* operator new(TUint aSize, TUint aExtraSize) __NO_THROW
|
sl@0
|
300 |
{
|
sl@0
|
301 |
return Backend()->Alloc(aSize + aExtraSize);
|
sl@0
|
302 |
}
|
sl@0
|
303 |
|
sl@0
|
304 |
inline TAny* operator new(TUint aSize, TLeave, TUint aExtraSize)
|
sl@0
|
305 |
{
|
sl@0
|
306 |
TAny* ptr = Backend()->Alloc(aSize + aExtraSize);
|
sl@0
|
307 |
if (ptr == NULL)
|
sl@0
|
308 |
{
|
sl@0
|
309 |
User::Leave(KErrNoMemory);
|
sl@0
|
310 |
}
|
sl@0
|
311 |
return ptr;
|
sl@0
|
312 |
}
|
sl@0
|
313 |
|
sl@0
|
314 |
inline void operator delete(TAny *aPtr) __NO_THROW
|
sl@0
|
315 |
{
|
sl@0
|
316 |
Backend()->Free( aPtr );
|
sl@0
|
317 |
}
|
sl@0
|
318 |
|
sl@0
|
319 |
inline void operator delete(TAny *aPtr, TLeave)
|
sl@0
|
320 |
{
|
sl@0
|
321 |
Backend()->Free( aPtr );
|
sl@0
|
322 |
return;
|
sl@0
|
323 |
}
|
sl@0
|
324 |
|
sl@0
|
325 |
inline void operator delete(TAny *aPtr, TAny* aBase) __NO_THROW
|
sl@0
|
326 |
{
|
sl@0
|
327 |
aBase = aBase;
|
sl@0
|
328 |
Backend()->Free( aPtr );
|
sl@0
|
329 |
return;
|
sl@0
|
330 |
}
|
sl@0
|
331 |
|
sl@0
|
332 |
inline void operator delete(TAny *aPtr, TUint aExtraSize) __NO_THROW
|
sl@0
|
333 |
{
|
sl@0
|
334 |
aExtraSize = aExtraSize;
|
sl@0
|
335 |
Backend()->Free( aPtr );
|
sl@0
|
336 |
return;
|
sl@0
|
337 |
}
|
sl@0
|
338 |
|
sl@0
|
339 |
inline void operator delete(TAny *aPtr, TLeave, TUint aExtraSize)
|
sl@0
|
340 |
{
|
sl@0
|
341 |
aExtraSize = aExtraSize;
|
sl@0
|
342 |
Backend()->Free( aPtr );
|
sl@0
|
343 |
return;
|
sl@0
|
344 |
}
|
sl@0
|
345 |
|
sl@0
|
346 |
_global_data_t();
|
sl@0
|
347 |
~_global_data_t();
|
sl@0
|
348 |
|
sl@0
|
349 |
}_global_data_t;
|
sl@0
|
350 |
|
sl@0
|
351 |
|
sl@0
|
352 |
_global_data_t* GetGlobals();
|
sl@0
|
353 |
#define glbHeadNode GetGlobals()
|
sl@0
|
354 |
|
sl@0
|
355 |
typedef struct _pthread_node
|
sl@0
|
356 |
{
|
sl@0
|
357 |
struct _pthread_node *next;
|
sl@0
|
358 |
RMutex lockNode;
|
sl@0
|
359 |
_global_data_t *glbDataPtr;
|
sl@0
|
360 |
unsigned int detachState;
|
sl@0
|
361 |
unsigned int threadState;
|
sl@0
|
362 |
void *returnValue;
|
sl@0
|
363 |
TBool hasAnyThreadJoined;
|
sl@0
|
364 |
RThread rtHandle;
|
sl@0
|
365 |
unsigned int threadId;
|
sl@0
|
366 |
// void *tls[PTHREAD_KEYS_MAX];
|
sl@0
|
367 |
_pkey_node_t *tlsHead;
|
sl@0
|
368 |
int priority;
|
sl@0
|
369 |
int mainFlag;
|
sl@0
|
370 |
void *cleanStackPtr;
|
sl@0
|
371 |
/*******************************************************************
|
sl@0
|
372 |
Overloading new and delete operators so that they will
|
sl@0
|
373 |
allocate and deallocare memory from/to the private heap of backend
|
sl@0
|
374 |
********************************************************************/
|
sl@0
|
375 |
inline TAny* operator new(TUint aSize, TAny* aBase) __NO_THROW
|
sl@0
|
376 |
{
|
sl@0
|
377 |
Mem::FillZ(aBase, aSize); return aBase;
|
sl@0
|
378 |
}
|
sl@0
|
379 |
|
sl@0
|
380 |
inline TAny* operator new(TUint aSize) __NO_THROW
|
sl@0
|
381 |
{
|
sl@0
|
382 |
return Backend()->Alloc(aSize);
|
sl@0
|
383 |
}
|
sl@0
|
384 |
|
sl@0
|
385 |
inline TAny* operator new(TUint aSize, TLeave)
|
sl@0
|
386 |
{
|
sl@0
|
387 |
TAny* ptr = Backend()->Alloc(aSize);
|
sl@0
|
388 |
if (ptr == NULL)
|
sl@0
|
389 |
{
|
sl@0
|
390 |
User::Leave(KErrNoMemory);
|
sl@0
|
391 |
}
|
sl@0
|
392 |
return ptr;
|
sl@0
|
393 |
}
|
sl@0
|
394 |
|
sl@0
|
395 |
inline TAny* operator new(TUint aSize, TUint aExtraSize) __NO_THROW
|
sl@0
|
396 |
{
|
sl@0
|
397 |
return Backend()->Alloc(aSize + aExtraSize);
|
sl@0
|
398 |
}
|
sl@0
|
399 |
|
sl@0
|
400 |
inline TAny* operator new(TUint aSize, TLeave, TUint aExtraSize)
|
sl@0
|
401 |
{
|
sl@0
|
402 |
TAny* ptr = Backend()->Alloc(aSize + aExtraSize);
|
sl@0
|
403 |
if (ptr == NULL)
|
sl@0
|
404 |
{
|
sl@0
|
405 |
User::Leave(KErrNoMemory);
|
sl@0
|
406 |
}
|
sl@0
|
407 |
return ptr;
|
sl@0
|
408 |
}
|
sl@0
|
409 |
|
sl@0
|
410 |
inline void operator delete(TAny *aPtr) __NO_THROW
|
sl@0
|
411 |
{
|
sl@0
|
412 |
Backend()->Free( aPtr );
|
sl@0
|
413 |
}
|
sl@0
|
414 |
}_pthread_node_t;
|
sl@0
|
415 |
|
sl@0
|
416 |
|
sl@0
|
417 |
|
sl@0
|
418 |
|
sl@0
|
419 |
#endif //THREADGLOBALS_H
|