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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include "lint.h"
#include "thr_uberdata.h"
#include <stddef.h>
/*
* These symbols should not be exported from libc, but
* /lib/libm.so.2 references them. libm needs to be fixed.
* Also, some older versions of the Studio compiler/debugger
* components reference them. These need to be fixed, too.
*/
#pragma weak _thr_getspecific = thr_getspecific
#pragma weak _thr_keycreate = thr_keycreate
#pragma weak _thr_setspecific = thr_setspecific
/*
* 128 million keys should be enough for anyone.
* This allocates half a gigabyte of memory for the keys themselves and
* half a gigabyte of memory for each thread that uses the largest key.
*/
#define MAX_KEYS 0x08000000U
int
thr_keycreate(thread_key_t *pkey, void (*destructor)(void *))
{
tsd_metadata_t *tsdm = &curthread->ul_uberdata->tsd_metadata;
void (**old_data)(void *) = NULL;
void (**new_data)(void *);
uint_t old_nkeys;
uint_t new_nkeys;
lmutex_lock(&tsdm->tsdm_lock);
/*
* Unfortunately, pthread_getspecific() specifies that a
* pthread_getspecific() on an allocated key upon which the
* calling thread has not performed a pthread_setspecifc()
* must return NULL. Consider the following sequence:
*
* pthread_key_create(&key);
* pthread_setspecific(key, datum);
* pthread_key_delete(&key);
* pthread_key_create(&key);
* val = pthread_getspecific(key);
*
* According to POSIX, if the deleted key is reused for the new
* key returned by the second pthread_key_create(), then the
* pthread_getspecific() in the above example must return NULL
* (and not the stale datum). The implementation is thus left
* with two alternatives:
*
* (1) Reuse deleted keys. If this is to be implemented optimally,
* it requires that pthread_key_create() somehow associate
* the value NULL with the new (reused) key for each thread.
* Keeping the hot path fast and lock-free induces substantial
* complexity on the implementation.
*
* (2) Never reuse deleted keys. This allows the pthread_getspecific()
* implementation to simply perform a check against the number
* of keys set by the calling thread, returning NULL if the
* specified key is larger than the highest set key. This has
* the disadvantage of wasting memory (a program which simply
* loops calling pthread_key_create()/pthread_key_delete()
* will ultimately run out of memory), but permits an optimal
* pthread_getspecific() while allowing for simple key creation
* and deletion.
*
* All Solaris implementations have opted for (2). Given the
* ~10 years that this has been in the field, it is safe to assume
* that applications don't loop creating and destroying keys; we
* stick with (2).
*/
if (tsdm->tsdm_nused == (old_nkeys = tsdm->tsdm_nkeys)) {
/*
* We need to allocate or double the number of keys.
* tsdm->tsdm_nused must always be a power of two.
*/
if ((new_nkeys = (old_nkeys << 1)) == 0)
new_nkeys = 8;
if (new_nkeys > MAX_KEYS) {
lmutex_unlock(&tsdm->tsdm_lock);
return (EAGAIN);
}
if ((new_data = lmalloc(new_nkeys * sizeof (void *))) == NULL) {
lmutex_unlock(&tsdm->tsdm_lock);
return (ENOMEM);
}
if ((old_data = tsdm->tsdm_destro) == NULL) {
/* key == 0 is always invalid */
new_data[0] = TSD_UNALLOCATED;
tsdm->tsdm_nused = 1;
} else {
(void) memcpy(new_data, old_data,
old_nkeys * sizeof (void *));
}
tsdm->tsdm_destro = new_data;
tsdm->tsdm_nkeys = new_nkeys;
}
*pkey = tsdm->tsdm_nused;
tsdm->tsdm_destro[tsdm->tsdm_nused++] = destructor;
lmutex_unlock(&tsdm->tsdm_lock);
if (old_data != NULL)
lfree(old_data, old_nkeys * sizeof (void *));
return (0);
}
#pragma weak _pthread_key_create = pthread_key_create
int
pthread_key_create(pthread_key_t *pkey, void (*destructor)(void *))
{
return (thr_keycreate(pkey, destructor));
}
/*
* Same as thr_keycreate(), above, except that the key creation
* is performed only once. This relies upon the fact that a key
* value of THR_ONCE_KEY is invalid, and requires that the key be
* allocated with a value of THR_ONCE_KEY before calling here.
* THR_ONCE_KEY and PTHREAD_ONCE_KEY_NP, defined in <thread.h>
* and <pthread.h> respectively, must have the same value.
* Example:
*
* static pthread_key_t key = PTHREAD_ONCE_KEY_NP;
* ...
* pthread_key_create_once_np(&key, destructor);
*/
#pragma weak pthread_key_create_once_np = thr_keycreate_once
int
thr_keycreate_once(thread_key_t *keyp, void (*destructor)(void *))
{
static mutex_t key_lock = DEFAULTMUTEX;
thread_key_t key;
int error;
if (*keyp == THR_ONCE_KEY) {
lmutex_lock(&key_lock);
if (*keyp == THR_ONCE_KEY) {
error = thr_keycreate(&key, destructor);
if (error) {
lmutex_unlock(&key_lock);
return (error);
}
membar_producer();
*keyp = key;
}
lmutex_unlock(&key_lock);
}
membar_consumer();
return (0);
}
int
pthread_key_delete(pthread_key_t key)
{
tsd_metadata_t *tsdm = &curthread->ul_uberdata->tsd_metadata;
lmutex_lock(&tsdm->tsdm_lock);
if (key >= tsdm->tsdm_nused ||
tsdm->tsdm_destro[key] == TSD_UNALLOCATED) {
lmutex_unlock(&tsdm->tsdm_lock);
return (EINVAL);
}
tsdm->tsdm_destro[key] = TSD_UNALLOCATED;
lmutex_unlock(&tsdm->tsdm_lock);
return (0);
}
/*
* Blessedly, the pthread_getspecific() interface is much better than the
* thr_getspecific() interface in that it cannot return an error status.
* Thus, if the key specified is bogus, pthread_getspecific()'s behavior
* is undefined. As an added bonus (and as an artificat of not returning
* an error code), the requested datum is returned rather than stored
* through a parameter -- thereby avoiding the unnecessary store/load pair
* incurred by thr_getspecific(). Every once in a while, the Standards
* get it right -- but usually by accident.
*/
void *
pthread_getspecific(pthread_key_t key)
{
tsd_t *stsd;
/*
* We are cycle-shaving in this function because some
* applications make heavy use of it and one machine cycle
* can make a measurable difference in performance. This
* is why we waste a little memory and allocate a NULL value
* for the invalid key == 0 in curthread->ul_ftsd[0] rather
* than adjusting the key by subtracting one.
*/
if (key < TSD_NFAST)
return (curthread->ul_ftsd[key]);
if ((stsd = curthread->ul_stsd) != NULL && key < stsd->tsd_nalloc)
return (stsd->tsd_data[key]);
return (NULL);
}
int
thr_getspecific(thread_key_t key, void **valuep)
{
tsd_t *stsd;
/*
* Amazingly, some application code (and worse, some particularly
* fugly Solaris library code) _relies_ on the fact that 0 is always
* an invalid key. To preserve this semantic, 0 is never returned
* as a key from thr_/pthread_key_create(); we explicitly check
* for it here and return EINVAL.
*/
if (key == 0)
return (EINVAL);
if (key < TSD_NFAST)
*valuep = curthread->ul_ftsd[key];
else if ((stsd = curthread->ul_stsd) != NULL && key < stsd->tsd_nalloc)
*valuep = stsd->tsd_data[key];
else
*valuep = NULL;
return (0);
}
/*
* We call thr_setspecific_slow() when the key specified
* is beyond the current thread's currently allocated range.
* This case is in a separate function because we want
* the compiler to optimize for the common case.
*/
static int
thr_setspecific_slow(thread_key_t key, void *value)
{
ulwp_t *self = curthread;
tsd_metadata_t *tsdm = &self->ul_uberdata->tsd_metadata;
tsd_t *stsd;
tsd_t *ntsd;
uint_t nkeys;
/*
* It isn't necessary to grab locks in this path;
* tsdm->tsdm_nused can only increase.
*/
if (key >= tsdm->tsdm_nused)
return (EINVAL);
/*
* We would like to test (tsdm->tsdm_destro[key] == TSD_UNALLOCATED)
* here but that would require acquiring tsdm->tsdm_lock and we
* want to avoid locks in this path.
*
* We have a key which is (or at least _was_) valid. If this key
* is later deleted (or indeed, is deleted before we set the value),
* we don't care; such a condition would indicate an application
* race for which POSIX thankfully leaves the behavior unspecified.
*
* First, determine our new size. To avoid allocating more than we
* have to, continue doubling our size only until the new key fits.
* stsd->tsd_nalloc must always be a power of two.
*/
nkeys = ((stsd = self->ul_stsd) != NULL)? stsd->tsd_nalloc : 8;
for (; key >= nkeys; nkeys <<= 1)
continue;
/*
* Allocate the new TSD.
*/
if ((ntsd = lmalloc(nkeys * sizeof (void *))) == NULL)
return (ENOMEM);
if (stsd != NULL) {
/*
* Copy the old TSD across to the new.
*/
(void) memcpy(ntsd, stsd, stsd->tsd_nalloc * sizeof (void *));
lfree(stsd, stsd->tsd_nalloc * sizeof (void *));
}
ntsd->tsd_nalloc = nkeys;
ntsd->tsd_data[key] = value;
self->ul_stsd = ntsd;
return (0);
}
int
thr_setspecific(thread_key_t key, void *value)
{
tsd_t *stsd;
int ret;
ulwp_t *self = curthread;
/*
* See the comment in thr_getspecific(), above.
*/
if (key == 0)
return (EINVAL);
if (key < TSD_NFAST) {
curthread->ul_ftsd[key] = value;
return (0);
}
if ((stsd = curthread->ul_stsd) != NULL && key < stsd->tsd_nalloc) {
stsd->tsd_data[key] = value;
return (0);
}
/*
* This is a critical region since we are dealing with memory
* allocation and free. Similar protection required in tsd_free().
*/
enter_critical(self);
ret = thr_setspecific_slow(key, value);
exit_critical(self);
return (ret);
}
int
pthread_setspecific(pthread_key_t key, const void *value)
{
return (thr_setspecific(key, (void *)value));
}
/*
* Contract-private interface for java. See PSARC/2003/159
*
* If the key falls within the TSD_NFAST range, return a non-negative
* offset that can be used by the caller to fetch the TSD data value
* directly out of the thread structure using %g7 (sparc) or %gs (x86).
* With the advent of TLS, %g7 and %gs are part of the ABI, even though
* the definition of the thread structure itself (ulwp_t) is private.
*
* We guarantee that the offset returned on sparc will fit within
* a SIMM13 field (that is, it is less than 2048).
*
* On failure (key is not in the TSD_NFAST range), return -1.
*/
ptrdiff_t
_thr_slot_offset(thread_key_t key)
{
if (key != 0 && key < TSD_NFAST)
return ((ptrdiff_t)offsetof(ulwp_t, ul_ftsd[key]));
return (-1);
}
/*
* This is called by _thrp_exit() to apply destructors to the thread's tsd.
*/
void
tsd_exit()
{
ulwp_t *self = curthread;
tsd_metadata_t *tsdm = &self->ul_uberdata->tsd_metadata;
thread_key_t key;
int recheck;
void *val;
void (*func)(void *);
lmutex_lock(&tsdm->tsdm_lock);
do {
recheck = 0;
for (key = 1; key < TSD_NFAST &&
key < tsdm->tsdm_nused; key++) {
if ((func = tsdm->tsdm_destro[key]) != NULL &&
func != TSD_UNALLOCATED &&
(val = self->ul_ftsd[key]) != NULL) {
self->ul_ftsd[key] = NULL;
lmutex_unlock(&tsdm->tsdm_lock);
(*func)(val);
lmutex_lock(&tsdm->tsdm_lock);
recheck = 1;
}
}
if (self->ul_stsd == NULL)
continue;
/*
* Any of these destructors could cause us to grow the number
* TSD keys in the slow TSD; we cannot cache the slow TSD
* pointer through this loop.
*/
for (; key < self->ul_stsd->tsd_nalloc &&
key < tsdm->tsdm_nused; key++) {
if ((func = tsdm->tsdm_destro[key]) != NULL &&
func != TSD_UNALLOCATED &&
(val = self->ul_stsd->tsd_data[key]) != NULL) {
self->ul_stsd->tsd_data[key] = NULL;
lmutex_unlock(&tsdm->tsdm_lock);
(*func)(val);
lmutex_lock(&tsdm->tsdm_lock);
recheck = 1;
}
}
} while (recheck);
lmutex_unlock(&tsdm->tsdm_lock);
/*
* We're done; if we have slow TSD, we need to free it.
*/
tsd_free(self);
}
void
tsd_free(ulwp_t *ulwp)
{
tsd_t *stsd;
ulwp_t *self = curthread;
enter_critical(self);
if ((stsd = ulwp->ul_stsd) != NULL)
lfree(stsd, stsd->tsd_nalloc * sizeof (void *));
ulwp->ul_stsd = NULL;
exit_critical(self);
}
|