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