• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* ------------------------------------------------------------------
2  * Copyright (C) 1998-2009 PacketVideo
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13  * express or implied.
14  * See the License for the specific language governing permissions
15  * and limitations under the License.
16  * -------------------------------------------------------------------
17  */
18 
19 #include "oscl_base.h"
20 
21 #include "oscl_tls.h"
22 #include "oscl_assert.h"
23 
24 //Use a magic number to help detect un-initialized TLS.
25 #define OSCL_TLS_MAGIC_NUMBER 0x8765abcd
26 #define OSCL_TLS_REGISTRY_VALID(reg) (reg!=0 && reg[OSCL_TLS_ID_MAGICNUM]==(OsclAny*)OSCL_TLS_MAGIC_NUMBER)
27 
save_registry(TOsclTlsKey * key,OsclAny * ptr,int32 & aError)28 OSCL_EXPORT_REF void TLSStorageOps::save_registry(TOsclTlsKey* key, OsclAny* ptr, int32 &aError)
29 {
30     aError = 0;
31 #if (OSCL_TLS_IS_KEYED)
32     OSCL_ASSERT(key);
33     if (!OSCL_TLS_STORE_FUNC(*key, ptr))
34     {
35         aError = EPVErrorBaseSystemCallFailed;
36         return;
37     }
38 #else
39     OSCL_UNUSED_ARG(key);
40     if (!OSCL_TLS_STORE_FUNC(ptr))
41     {
42         aError = EPVErrorBaseSystemCallFailed;
43         return;
44     }
45 #endif
46 }
47 
get_registry(TOsclTlsKey * key)48 OSCL_EXPORT_REF OsclAny* TLSStorageOps::get_registry(TOsclTlsKey* key)
49 {
50 #if (OSCL_TLS_IS_KEYED)
51     OSCL_ASSERT(key);
52     return OSCL_TLS_GET_FUNC(*key);
53 #else
54     OSCL_UNUSED_ARG(key);
55     return OSCL_TLS_GET_FUNC();
56 #endif
57 }
58 
59 #if (OSCL_TLS_IS_KEYED)
60 
61 //Global var for Tls Key.
62 OsclTLSRegistry::TlsKey* OsclTLSRegistry::iTlsKey = NULL;
63 
64 
65 #endif //OSCL_TLS_IS_KEYED
66 
67 _OsclBasicLock OsclTLSRegistry::sLock;
68 
initialize(Oscl_DefAlloc & alloc,int32 & aError)69 OSCL_EXPORT_REF void OsclTLSRegistry::initialize(Oscl_DefAlloc &alloc, int32 &aError)
70 {
71     TOsclTlsKey* pkey = NULL;
72     aError = 0;
73     sLock.Lock();
74 
75 #if ( OSCL_TLS_IS_KEYED)
76     //Allocate the table on the first init call.
77     //Note there's some risk of thread contention here, since
78     //the thread lock is not available until after this step.
79     if (!iTlsKey)
80     {
81         OsclAny* table = alloc.allocate(sizeof(TlsKey));
82         if (!table)
83         {
84             aError = EPVErrorBaseOutOfMemory;
85             sLock.Unlock();
86             return;
87         }
88 
89         //allocate space for key
90         pkey = (TOsclTlsKey*)alloc.allocate(sizeof(TOsclTlsKey));
91         if (!pkey)
92         {
93             aError = EPVErrorBaseOutOfMemory;
94             alloc.deallocate(table);
95             sLock.Unlock();
96             return;
97         }
98 
99         //create key for this thread.
100         if (!OSCL_TLS_KEY_CREATE_FUNC(*pkey))
101         {
102             aError = EPVErrorBaseSystemCallFailed;
103             alloc.deallocate(pkey);
104             alloc.deallocate(table);
105             sLock.Unlock();
106             return;
107         }
108 
109         iTlsKey = new(table) TlsKey();
110         iTlsKey->iRefCnt++;
111         iTlsKey->iOsclTlsKey = pkey;
112     }
113     else
114     {
115         iTlsKey->iRefCnt++;
116         pkey = iTlsKey->iOsclTlsKey;
117     }
118 
119 #endif
120 
121     // allocate the space and save the pointer
122     registry_pointer_type registry = OSCL_STATIC_CAST(registry_pointer_type,
123                                      alloc.allocate(sizeof(registry_type) * OSCL_TLS_MAX_SLOTS));
124     if (registry == 0)
125     {
126         aError = EPVErrorBaseOutOfMemory;
127         sLock.Unlock();
128         return;
129     }
130 
131     // initialize all TLSs to 0
132     for (uint32 ii = 0; ii < OSCL_TLS_MAX_SLOTS; ii++)
133         registry[ii] = 0;
134     // initialize the magic number
135     registry[OSCL_TLS_ID_MAGICNUM] = (OsclAny*)OSCL_TLS_MAGIC_NUMBER;
136 
137     // save it away
138     TLSStorageOps::save_registry(pkey, registry, aError);
139     sLock.Unlock();
140 }
141 
cleanup(Oscl_DefAlloc & alloc,int32 & aError)142 OSCL_EXPORT_REF void OsclTLSRegistry::cleanup(Oscl_DefAlloc &alloc, int32 &aError)
143 {
144     TOsclTlsKey* pkey = NULL;
145     aError = 0;
146     sLock.Lock();
147 
148 #if (OSCL_TLS_IS_KEYED)
149     if (!iTlsKey)
150     {
151         aError = EPVErrorBaseNotInstalled;//No key!
152         sLock.Unlock();
153         return;
154     }
155     pkey = iTlsKey->iOsclTlsKey;
156 #endif
157 
158     //Cleanup this thread's registry
159     registry_pointer_type registry = OSCL_STATIC_CAST(registry_pointer_type , TLSStorageOps::get_registry(pkey));
160     if (!OSCL_TLS_REGISTRY_VALID(registry))
161     {
162         aError = EPVErrorBaseNotInstalled;//No registry!
163         sLock.Unlock();
164         return;
165     }
166     alloc.deallocate(registry);
167 
168     TLSStorageOps::save_registry(pkey, NULL, aError);
169     if (aError)
170     {
171         sLock.Unlock();
172         return;
173     }
174 
175 #if (OSCL_TLS_IS_KEYED)
176     //Remove Tls key
177     iTlsKey->iRefCnt--;
178     if (iTlsKey->iRefCnt == 0)
179     {
180         //Deallocate key.
181         OSCL_TLS_KEY_DELETE_FUNC(*pkey);
182         alloc.deallocate(pkey);
183         iTlsKey->~TlsKey();
184         alloc.deallocate(iTlsKey);
185         iTlsKey = NULL;
186     }
187 #endif
188 
189     sLock.Unlock();
190 }
191 
getInstance(uint32 ID,int32 & aError)192 OSCL_EXPORT_REF OsclAny* OsclTLSRegistry::getInstance(uint32 ID, int32 &aError)
193 {
194     OSCL_ASSERT(ID < OSCL_TLS_MAX_SLOTS);
195 
196     aError = 0;
197     TOsclTlsKey* pkey = NULL;
198     registry_type id = 0;
199 
200     sLock.Lock();
201 
202 #if (OSCL_TLS_IS_KEYED)
203     if (!iTlsKey)
204     {
205         aError = EPVErrorBaseNotInstalled;//No table!
206         sLock.Unlock();
207         return NULL;
208     }
209     pkey = iTlsKey->iOsclTlsKey;
210 #endif
211 
212     registry_pointer_type registry = OSCL_STATIC_CAST(registry_pointer_type , TLSStorageOps::get_registry(pkey));
213     if (!OSCL_TLS_REGISTRY_VALID(registry))
214     {
215         aError = EPVErrorBaseNotInstalled;//No registry!
216         sLock.Unlock();
217         return NULL;
218     }
219     id = registry[ID];
220     sLock.Unlock();
221 
222     return id;
223 }
224 
registerInstance(OsclAny * ptr,uint32 ID,int32 & aError)225 OSCL_EXPORT_REF void OsclTLSRegistry::registerInstance(OsclAny* ptr, uint32 ID, int32 &aError)
226 {
227     OSCL_ASSERT(ID < OSCL_TLS_MAX_SLOTS);
228 
229     aError = 0;
230     TOsclTlsKey *pkey = NULL;
231     sLock.Lock();
232 
233 #if (OSCL_TLS_IS_KEYED)
234     if (!iTlsKey)
235     {
236         aError = EPVErrorBaseNotInstalled;//No table!
237         sLock.Unlock();
238         return ;
239     }
240     pkey = iTlsKey->iOsclTlsKey;
241 #endif
242 
243     registry_pointer_type registry = OSCL_STATIC_CAST(registry_pointer_type , TLSStorageOps::get_registry(pkey));
244     if (!OSCL_TLS_REGISTRY_VALID(registry))
245     {
246         aError = EPVErrorBaseNotInstalled;//no registry!
247         sLock.Unlock();
248         return;
249     }
250     registry[ID] = ptr;
251     sLock.Unlock();
252 }
253 
254 
255 
256