1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
/*
* tclThreadStorage.c --
*
* This file implements platform independent thread storage operations.
*
* Copyright (c) 2003-2004 by Joe Mistachkin
* Copyright (c) 2008 by George Peter Staplin
*
* The primary idea is that we create one platform-specific TSD slot, and
* use it for storing a table pointer.
*
* Each Tcl_ThreadDataKey has an offset into the table of TSD values.
*
* We don't use more than 1 platform-specific TSD slot, because there is
* a hard limit on the number of TSD slots.
*
* Valid key offsets are > 0. 0 is for the initialized Tcl_ThreadDataKey.
*
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* RCS: @(#) $Id: tclThreadStorage.c,v 1.16 2008/05/09 04:58:54 georgeps Exp $
*/
#include "tclInt.h"
#include <signal.h>
#if defined(TCL_THREADS)
static void *tclTsdKey = NULL;
static Tcl_Mutex tclTsdMutex;
static sig_atomic_t tclTsdCounter = 0;
typedef struct TSDTable {
sig_atomic_t allocated;
void **table;
} TSDTable;
typedef union {
void *ptr;
volatile sig_atomic_t offset;
} TSDUnion;
static TSDTable *TSDTableCreate(void);
static void TSDTableDelete(TSDTable *t);
static void TSDTableGrow(TSDTable *t, sig_atomic_t atLeast);
static TSDTable *
TSDTableCreate(void) {
TSDTable *t;
sig_atomic_t i;
t = TclpSysAlloc(sizeof *t, 0);
if (NULL == t) {
Tcl_Panic("unable to allocate TSDTable");
}
t->allocated = 8;
t->table = TclpSysAlloc(sizeof (*(t->table)) * t->allocated, 0);
if (NULL == t->table) {
Tcl_Panic("unable to allocate TSDTable");
}
for (i = 0; i < t->allocated; ++i) {
t->table[i] = NULL;
}
return t;
}
static void
TSDTableDelete(TSDTable *t) {
TclpSysFree(t->table);
TclpSysFree(t);
}
/*
*----------------------------------------------------------------------
*
* TSDTableGrow --
*
* This procedure makes the passed TSDTable grow to fit the atLeast value.
*
* Side effects: The table is enlarged.
*
*----------------------------------------------------------------------
*/
static void
TSDTableGrow(TSDTable *t, sig_atomic_t atLeast) {
sig_atomic_t newAllocated = t->allocated * 2;
void **newTablePtr;
sig_atomic_t i;
if (newAllocated <= atLeast) {
newAllocated = atLeast + 10;
}
newTablePtr = TclpSysRealloc(t->table, sizeof (*newTablePtr) * newAllocated);
if (NULL == newTablePtr) {
Tcl_Panic("unable to reallocate TSDTable");
}
for (i = t->allocated; i < newAllocated; ++i) {
newTablePtr[i] = NULL;
}
t->allocated = newAllocated;
t->table = newTablePtr;
}
/*
*----------------------------------------------------------------------
*
* TclThreadStorageKeyGet --
*
* This procedure gets the value associated with the passed key.
*
* Results: A pointer value associated with the Tcl_ThreadDataKey or NULL.
*
*----------------------------------------------------------------------
*/
void *
TclThreadStorageKeyGet(Tcl_ThreadDataKey *dataKeyPtr) {
TSDTable *t = TclpThreadGetMasterTSD(tclTsdKey);
void *resultPtr = NULL;
TSDUnion *keyPtr = (TSDUnion *)dataKeyPtr;
sig_atomic_t offset = keyPtr->offset;
if (t == NULL) {
return NULL;
}
if (offset && offset > 0 && offset < t->allocated) {
resultPtr = t->table[offset];
}
return resultPtr;
}
/*
*----------------------------------------------------------------------
*
* TclThreadStorageKeySet --
*
* This procedure set an association of value with the key passed.
* The associated value may be retrieved with TclThreadDataKeyGet().
*
* Side effects: The thread-specific table may be created or reallocated.
*
*----------------------------------------------------------------------
*/
void
TclThreadStorageKeySet(Tcl_ThreadDataKey *dataKeyPtr, void *value) {
TSDTable *t = TclpThreadGetMasterTSD(tclTsdKey);
TSDUnion *keyPtr = (TSDUnion *)dataKeyPtr;
if (NULL == t) {
t = TSDTableCreate();
TclpThreadSetMasterTSD(tclTsdKey, t);
}
Tcl_MutexLock(&tclTsdMutex);
if (0 == keyPtr->offset) {
/*
* The Tcl_ThreadDataKey hasn't been used yet.
*/
++tclTsdCounter;
if (tclTsdCounter >= t->allocated) {
TSDTableGrow(t, tclTsdCounter);
}
keyPtr->offset = tclTsdCounter;
t->table[tclTsdCounter] = value;
} else {
if (keyPtr->offset >= t->allocated) {
/*
* This is the first time this Tcl_ThreadDataKey has been
* used with the current thread.
*/
TSDTableGrow(t, keyPtr->offset);
}
t->table[keyPtr->offset] = value;
}
Tcl_MutexUnlock(&tclTsdMutex);
}
/*
*----------------------------------------------------------------------
*
* TclFinalizeThreadDataThread --
*
* This procedure finalizes the data for a single thread.
*
*
* Side effects: The TSDTable is deleted/freed.
*----------------------------------------------------------------------
*/
void
TclFinalizeThreadDataThread(void) {
TSDTable *t = TclpThreadGetMasterTSD(tclTsdKey);
if (NULL == t) {
return;
}
TSDTableDelete(t);
TclpThreadSetMasterTSD(tclTsdKey, NULL);
}
/*
*----------------------------------------------------------------------
*
* TclInitializeThreadStorage --
*
* This procedure initializes the TSD subsystem with per-platform
* code. This should be called before any Tcl threads are created.
*
*----------------------------------------------------------------------
*/
void
TclInitThreadStorage(void) {
tclTsdKey = TclpThreadCreateKey();
}
/*
*----------------------------------------------------------------------
*
* TclFinalizeThreadStorage --
*
* This procedure cleans up the thread storage data key for all
* threads.
*
* IMPORTANT: All Tcl threads must be finalized before calling this!
*
* Results:
* None.
*
* Side effects:
* Releases the thread data key.
*
*----------------------------------------------------------------------
*/
void
TclFinalizeThreadStorage(void) {
TclpThreadDeleteKey(tclTsdKey);
tclTsdKey = NULL;
}
#else /* !defined(TCL_THREADS) */
/*
* Stub functions for non-threaded builds
*/
void
TclInitThreadStorage(void)
{
}
void
TclFinalizeThreadDataThread(void)
{
}
void
TclFinalizeThreadStorage(void)
{
}
#endif /* defined(TCL_THREADS) && defined(USE_THREAD_STORAGE) */
/*
* Local Variables:
* mode: c
* c-basic-offset: 4
* fill-column: 78
* End:
*/
|