Debian Codegraph @

generic/tclThreadStorage.c

raw
1 /*
2 * tclThreadStorage.c --
3 *
4 * This file implements platform independent thread storage operations to
5 * work around system limits on the number of thread-specific variables.
6 *
7 * Copyright (c) 2003-2004 by Joe Mistachkin
8 * Copyright (c) 2008 by George Peter Staplin
9 *
10 * See the file "license.terms" for information on usage and redistribution of
11 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
12 */
13
14 #include "tclInt.h"
15
16 #ifdef TCL_THREADS
17 #include <signal.h>
18
19 /*
20 * IMPLEMENTATION NOTES:
21 *
22 * The primary idea is that we create one platform-specific TSD slot, and use
23 * it for storing a table pointer. Each Tcl_ThreadDataKey has an offset into
24 * the table of TSD values. We don't use more than 1 platform-specific TSD
25 * slot, because there is a hard limit on the number of TSD slots. Valid key
26 * offsets are greater than 0; 0 is for the initialized Tcl_ThreadDataKey.
27 */
28
29 /*
30 * The global collection of information about TSDs. This is shared across the
31 * whole process, and includes the mutex used to protect it.
32 */
33
34 static struct {
35 void *key; /* Key into the system TSD structure. The
36 * collection of Tcl TSD values for a
37 * particular thread will hang off the
38 * back-end of this. */
39 sig_atomic_t counter; /* The number of different Tcl TSDs used
40 * across *all* threads. This is a strictly
41 * increasing value. */
42 Tcl_Mutex mutex; /* Protection for the rest of this structure,
43 * which holds per-process data. */
44 } tsdGlobal = { NULL, 0, NULL };
45
46 /*
47 * The type of the data held per thread in a system TSD.
48 */
49
50 typedef struct {
51 ClientData *tablePtr; /* The table of Tcl TSDs. */
52 sig_atomic_t allocated; /* The size of the table in the current
53 * thread. */
54 } TSDTable;
55
56 /*
57 * The actual type of Tcl_ThreadDataKey.
58 */
59
60 typedef union {
61 volatile sig_atomic_t offset;
62 /* The type is really an offset into the
63 * thread-local table of TSDs, which is this
64 * field. */
65 void *ptr; /* For alignment purposes only. Not actually
66 * accessed through this. */
67 } TSDUnion;
68
69 /*
70 * Forward declarations of functions in this file.
71 */
72
73 static TSDTable * TSDTableCreate(void);
74 static void TSDTableDelete(TSDTable *tsdTablePtr);
75 static void TSDTableGrow(TSDTable *tsdTablePtr,
76 sig_atomic_t atLeast);
77
78 /*
79 * Allocator and deallocator for a TSDTable structure.
80 */
81
82 static TSDTable *
83 TSDTableCreate(void)
84 {
85 TSDTable *tsdTablePtr;
86 sig_atomic_t i;
87
88 tsdTablePtr = TclpSysAlloc(sizeof(TSDTable), 0);
89 if (tsdTablePtr == NULL) {
90 Tcl_Panic("unable to allocate TSDTable");
91 }
92
93 tsdTablePtr->allocated = 8;
94 tsdTablePtr->tablePtr =
95 TclpSysAlloc(sizeof(void *) * tsdTablePtr->allocated, 0);
96 if (tsdTablePtr->tablePtr == NULL) {
97 Tcl_Panic("unable to allocate TSDTable");
98 }
99
100 for (i = 0; i < tsdTablePtr->allocated; ++i) {
101 tsdTablePtr->tablePtr[i] = NULL;
102 }
103
104 return tsdTablePtr;
105 }
106
107 static void
108 TSDTableDelete(
109 TSDTable *tsdTablePtr)
110 {
111 sig_atomic_t i;
112
113 for (i=0 ; i<tsdTablePtr->allocated ; i++) {
114 if (tsdTablePtr->tablePtr[i] != NULL) {
115 /*
116 * These values were allocated in Tcl_GetThreadData in tclThread.c
117 * and must now be deallocated or they will leak.
118 */
119
120 ckfree(tsdTablePtr->tablePtr[i]);
121 }
122 }
123
124 TclpSysFree(tsdTablePtr->tablePtr);
125 TclpSysFree(tsdTablePtr);
126 }
127
128 /*
129 *----------------------------------------------------------------------
130 *
131 * TSDTableGrow --
132 *
133 * This procedure makes the passed TSDTable grow to fit the atLeast
134 * value.
135 *
136 * Results:
137 * None.
138 *
139 * Side effects:
140 * The table is enlarged.
141 *
142 *----------------------------------------------------------------------
143 */
144
145 static void
146 TSDTableGrow(
147 TSDTable *tsdTablePtr,
148 sig_atomic_t atLeast)
149 {
150 sig_atomic_t newAllocated = tsdTablePtr->allocated * 2;
151 ClientData *newTablePtr;
152 sig_atomic_t i;
153
154 if (newAllocated <= atLeast) {
155 newAllocated = atLeast + 10;
156 }
157
158 newTablePtr = TclpSysRealloc(tsdTablePtr->tablePtr,
159 sizeof(ClientData) * newAllocated);
160 if (newTablePtr == NULL) {
161 Tcl_Panic("unable to reallocate TSDTable");
162 }
163
164 for (i = tsdTablePtr->allocated; i < newAllocated; ++i) {
165 newTablePtr[i] = NULL;
166 }
167
168 tsdTablePtr->allocated = newAllocated;
169 tsdTablePtr->tablePtr = newTablePtr;
170 }
171
172 /*
173 *----------------------------------------------------------------------
174 *
175 * TclThreadStorageKeyGet --
176 *
177 * This procedure gets the value associated with the passed key.
178 *
179 * Results:
180 * A pointer value associated with the Tcl_ThreadDataKey or NULL.
181 *
182 * Side effects:
183 * None.
184 *
185 *----------------------------------------------------------------------
186 */
187
188 void *
189 TclThreadStorageKeyGet(
190 Tcl_ThreadDataKey *dataKeyPtr)
191 {
192 TSDTable *tsdTablePtr = TclpThreadGetGlobalTSD(tsdGlobal.key);
193 ClientData resultPtr = NULL;
194 TSDUnion *keyPtr = (TSDUnion *) dataKeyPtr;
195 sig_atomic_t offset = keyPtr->offset;
196
197 if ((tsdTablePtr != NULL) && (offset > 0)
198 && (offset < tsdTablePtr->allocated)) {
199 resultPtr = tsdTablePtr->tablePtr[offset];
200 }
201 return resultPtr;
202 }
203
204 /*
205 *----------------------------------------------------------------------
206 *
207 * TclThreadStorageKeySet --
208 *
209 * This procedure set an association of value with the key passed. The
210 * associated value may be retrieved with TclThreadDataKeyGet().
211 *
212 * Results:
213 * None.
214 *
215 * Side effects:
216 * The thread-specific table may be created or reallocated.
217 *
218 *----------------------------------------------------------------------
219 */
220
221 void
222 TclThreadStorageKeySet(
223 Tcl_ThreadDataKey *dataKeyPtr,
224 void *value)
225 {
226 TSDTable *tsdTablePtr = TclpThreadGetGlobalTSD(tsdGlobal.key);
227 TSDUnion *keyPtr = (TSDUnion *) dataKeyPtr;
228
229 if (tsdTablePtr == NULL) {
230 tsdTablePtr = TSDTableCreate();
231 TclpThreadSetGlobalTSD(tsdGlobal.key, tsdTablePtr);
232 }
233
234 /*
235 * Get the lock while we check if this TSD is new or not. Note that this
236 * is the only place where Tcl_ThreadDataKey values are set. We use a
237 * double-checked lock to try to avoid having to grab this lock a lot,
238 * since it is on quite a few critical paths and will only get set once in
239 * each location.
240 */
241
242 if (keyPtr->offset == 0) {
243 Tcl_MutexLock(&tsdGlobal.mutex);
244 if (keyPtr->offset == 0) {
245 /*
246 * The Tcl_ThreadDataKey hasn't been used yet. Make a new one.
247 */
248
249 keyPtr->offset = ++tsdGlobal.counter;
250 }
251 Tcl_MutexUnlock(&tsdGlobal.mutex);
252 }
253
254 /*
255 * Check if this is the first time this Tcl_ThreadDataKey has been used
256 * with the current thread. Note that we don't need to hold a lock when
257 * doing this, as we are *definitely* the only point accessing this
258 * tsdTablePtr right now; it's thread-local.
259 */
260
261 if (keyPtr->offset >= tsdTablePtr->allocated) {
262 TSDTableGrow(tsdTablePtr, keyPtr->offset);
263 }
264
265 /*
266 * Set the value in the Tcl thread-local variable.
267 */
268
269 tsdTablePtr->tablePtr[keyPtr->offset] = value;
270 }
271
272 /*
273 *----------------------------------------------------------------------
274 *
275 * TclFinalizeThreadDataThread --
276 *
277 * This procedure finalizes the data for a single thread.
278 *
279 * Results:
280 * None.
281 *
282 * Side effects:
283 * The TSDTable is deleted/freed.
284 *
285 *----------------------------------------------------------------------
286 */
287
288 void
289 TclFinalizeThreadDataThread(void)
290 {
291 TSDTable *tsdTablePtr = TclpThreadGetGlobalTSD(tsdGlobal.key);
292
293 if (tsdTablePtr != NULL) {
294 TSDTableDelete(tsdTablePtr);
295 TclpThreadSetGlobalTSD(tsdGlobal.key, NULL);
296 }
297 }
298
299 /*
300 *----------------------------------------------------------------------
301 *
302 * TclInitializeThreadStorage --
303 *
304 * This procedure initializes the TSD subsystem with per-platform code.
305 * This should be called before any Tcl threads are created.
306 *
307 * Results:
308 * None.
309 *
310 * Side effects:
311 * Allocates a system TSD.
312 *
313 *----------------------------------------------------------------------
314 */
315
316 void
317 TclInitThreadStorage(void)
318 {
319 tsdGlobal.key = TclpThreadCreateKey();
320 }
321
322 /*
323 *----------------------------------------------------------------------
324 *
325 * TclFinalizeThreadStorage --
326 *
327 * This procedure cleans up the thread storage data key for all threads.
328 * IMPORTANT: All Tcl threads must be finalized before calling this!
329 *
330 * Results:
331 * None.
332 *
333 * Side effects:
334 * Releases the thread data key.
335 *
336 *----------------------------------------------------------------------
337 */
338
339 void
340 TclFinalizeThreadStorage(void)
341 {
342 TclpThreadDeleteKey(tsdGlobal.key);
343 tsdGlobal.key = NULL;
344 }
345
346 #else /* !TCL_THREADS */
347 /*
348 * Stub functions for non-threaded builds
349 */
350
351 void
352 TclInitThreadStorage(void)
353 {
354 }
355
356 void
357 TclFinalizeThreadDataThread(void)
358 {
359 }
360
361 void
362 TclFinalizeThreadStorage(void)
363 {
364 }
365 #endif /* TCL_THREADS */
366
367 /*
368 * Local Variables:
369 * mode: c
370 * c-basic-offset: 4
371 * fill-column: 78
372 * End:
373 */