sdk-core v2026.11.05
PS2 SDK - core tier (build-critical EE/IOP modules)
Loading...
Searching...
No Matches
tls-helper.c
1/*
2# _____ ___ ____ ___ ____
3# ____| | ____| | | |____|
4# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5#-----------------------------------------------------------------------
6# Copyright 2001-2004, ps2dev - http://www.ps2dev.org
7# Licenced under Academic Free License version 2.0
8# Review ps2sdk README & LICENSE files for further details.
9*/
10
11#include <ps2_osal.h>
12
13int *__keysUsed;
14
15int __maxTlsValues;
16
17pte_osMutexHandle __globalTlsLock;
18
19/* Structure used to emulate TLS on non-POSIX threads.
20 * This limits us to one non-POSIX thread that can
21 * call pthread functions. */
22void *__globalTls;
23
24pte_osResult pteTlsGlobalInit(int maxEntries)
25{
26 int i;
27 pte_osResult result;
28
29 pte_osMutexCreate(&__globalTlsLock);
30 __keysUsed = (int *)malloc(maxEntries * sizeof(int));
31
32 if (__keysUsed != NULL) {
33 for (i = 0; i < maxEntries; i++) {
34 __keysUsed[i] = 0;
35 }
36
37 __maxTlsValues = maxEntries;
38 result = PTE_OS_OK;
39 } else {
40 result = PTE_OS_NO_RESOURCES;
41 }
42
43 return result;
44}
45
46void *pteTlsThreadInit(void)
47{
48 void **pTlsStruct;
49 int i;
50
51 pTlsStruct = (void **)malloc(__maxTlsValues * sizeof(void *));
52
53 // PTE library assumes that keys are initialized to zero
54 for (i = 0; i < __maxTlsValues; i++) {
55 pTlsStruct[i] = 0;
56 }
57
58 return (void *)pTlsStruct;
59}
60
61pte_osResult __pteTlsAlloc(unsigned int *pKey)
62{
63 int i;
64 pte_osResult result = PTE_OS_NO_RESOURCES;
65
66 pte_osMutexLock(__globalTlsLock);
67
68 for (i = 0; i < __maxTlsValues; i++) {
69 if (__keysUsed[i] == 0) {
70 __keysUsed[i] = 1;
71
72 *pKey = i + 1;
73 result = PTE_OS_OK;
74 break;
75 }
76 }
77
78 pte_osMutexUnlock(__globalTlsLock);
79 return result;
80}
81
82void *pteTlsGetValue(void *pTlsThreadStruct, unsigned int index)
83{
84 void **pTls = (void **)pTlsThreadStruct;
85
86 if (__keysUsed[index - 1] && pTls != NULL) {
87 return pTls[index - 1];
88 } else {
89 return NULL;
90 }
91}
92
93pte_osResult __pteTlsSetValue(void *pTlsThreadStruct, unsigned int index, void *value)
94{
95 pte_osResult result;
96 void **pTls = (void **)pTlsThreadStruct;
97
98 if (pTls != NULL) {
99 pTls[index - 1] = value;
100 result = PTE_OS_OK;
101 } else {
102 result = PTE_OS_INVALID_PARAM;
103 }
104
105 return result;
106}
107
108void *__getTlsStructFromThread(s32 thid)
109{
110 struct OsalThreadInfo *threadInfo = &__threadInfo[thid];
111 void *pTls;
112
113 /* If we were called from a pthread, use the TLS allocated when the thread
114 * was created. Otherwise, we were called from a non-pthread, so use the
115 * "global". This is a pretty bad hack, but necessary due to lack of TLS on PS2.
116 */
117 if (threadInfo->tlsPtr) {
118 pTls = threadInfo->tlsPtr;
119 } else {
120 pTls = __globalTls;
121 }
122
123 return pTls;
124}
125
126pte_osResult pteTlsFree(unsigned int index)
127{
128 pte_osResult result;
129
130 if (__keysUsed != NULL) {
131 pte_osMutexLock(__globalTlsLock);
132 __keysUsed[index - 1] = 0;
133 pte_osMutexUnlock(__globalTlsLock);
134
135 result = PTE_OS_OK;
136 } else {
137 result = PTE_OS_GENERAL_FAILURE;
138 }
139
140 return result;
141}
142
143void pteTlsThreadDestroy(void *pTlsThreadStruct)
144{
145 free(pTlsThreadStruct);
146}
147
148void pteTlsGlobalDestroy(void)
149{
150 pte_osMutexDelete(__globalTlsLock);
151 free(__keysUsed);
152}
153
154pte_osResult pte_osTlsSetValue(unsigned int key, void * value)
155{
156 void *pTls;
157
158 pTls = __getTlsStructFromThread(GetThreadId());
159
160 return __pteTlsSetValue(pTls, key, value);
161}
162
163void * pte_osTlsGetValue(unsigned int index)
164{
165 void *pTls;
166
167 pTls = __getTlsStructFromThread(GetThreadId());
168
169 return (void *) pteTlsGetValue(pTls, index);
170
171}
172
173pte_osResult pte_osTlsAlloc(unsigned int *pKey)
174{
175 __getTlsStructFromThread(GetThreadId());
176
177 return __pteTlsAlloc(pKey);
178
179}
180
181pte_osResult pte_osTlsFree(unsigned int index)
182{
183 return pteTlsFree(index);
184}