17pte_osMutexHandle __globalTlsLock;
24pte_osResult pteTlsGlobalInit(
int maxEntries)
29 pte_osMutexCreate(&__globalTlsLock);
30 __keysUsed = (
int *)malloc(maxEntries *
sizeof(
int));
32 if (__keysUsed != NULL) {
33 for (i = 0; i < maxEntries; i++) {
37 __maxTlsValues = maxEntries;
40 result = PTE_OS_NO_RESOURCES;
46void *pteTlsThreadInit(
void)
51 pTlsStruct = (
void **)malloc(__maxTlsValues *
sizeof(
void *));
54 for (i = 0; i < __maxTlsValues; i++) {
58 return (
void *)pTlsStruct;
61pte_osResult __pteTlsAlloc(
unsigned int *pKey)
64 pte_osResult result = PTE_OS_NO_RESOURCES;
66 pte_osMutexLock(__globalTlsLock);
68 for (i = 0; i < __maxTlsValues; i++) {
69 if (__keysUsed[i] == 0) {
78 pte_osMutexUnlock(__globalTlsLock);
82void *pteTlsGetValue(
void *pTlsThreadStruct,
unsigned int index)
84 void **pTls = (
void **)pTlsThreadStruct;
86 if (__keysUsed[index - 1] && pTls != NULL) {
87 return pTls[index - 1];
93pte_osResult __pteTlsSetValue(
void *pTlsThreadStruct,
unsigned int index,
void *value)
96 void **pTls = (
void **)pTlsThreadStruct;
99 pTls[index - 1] = value;
102 result = PTE_OS_INVALID_PARAM;
108void *__getTlsStructFromThread(s32 thid)
117 if (threadInfo->tlsPtr) {
118 pTls = threadInfo->tlsPtr;
126pte_osResult pteTlsFree(
unsigned int index)
130 if (__keysUsed != NULL) {
131 pte_osMutexLock(__globalTlsLock);
132 __keysUsed[index - 1] = 0;
133 pte_osMutexUnlock(__globalTlsLock);
137 result = PTE_OS_GENERAL_FAILURE;
143void pteTlsThreadDestroy(
void *pTlsThreadStruct)
145 free(pTlsThreadStruct);
148void pteTlsGlobalDestroy(
void)
150 pte_osMutexDelete(__globalTlsLock);
154pte_osResult pte_osTlsSetValue(
unsigned int key,
void * value)
158 pTls = __getTlsStructFromThread(GetThreadId());
160 return __pteTlsSetValue(pTls, key, value);
163void * pte_osTlsGetValue(
unsigned int index)
167 pTls = __getTlsStructFromThread(GetThreadId());
169 return (
void *) pteTlsGetValue(pTls, index);
173pte_osResult pte_osTlsAlloc(
unsigned int *pKey)
175 __getTlsStructFromThread(GetThreadId());
177 return __pteTlsAlloc(pKey);
181pte_osResult pte_osTlsFree(
unsigned int index)
183 return pteTlsFree(index);