xref: /titanic_44/usr/src/lib/libc/port/threads/tsd.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "lint.h"
30 #include "thr_uberdata.h"
31 #include <stddef.h>
32 
33 /*
34  * 128 million keys should be enough for anyone.
35  * This allocates half a gigabyte of memory for the keys themselves and
36  * half a gigabyte of memory for each thread that uses the largest key.
37  */
38 #define	MAX_KEYS	0x08000000U
39 
40 #pragma weak thr_keycreate = _thr_keycreate
41 #pragma weak pthread_key_create = _thr_keycreate
42 #pragma weak _pthread_key_create = _thr_keycreate
43 int
44 _thr_keycreate(thread_key_t *pkey, void (*destructor)(void *))
45 {
46 	tsd_metadata_t *tsdm = &curthread->ul_uberdata->tsd_metadata;
47 	void (**old_data)(void *) = NULL;
48 	void (**new_data)(void *);
49 	uint_t old_nkeys;
50 	uint_t new_nkeys;
51 
52 	lmutex_lock(&tsdm->tsdm_lock);
53 
54 	/*
55 	 * Unfortunately, pthread_getspecific() specifies that a
56 	 * pthread_getspecific() on an allocated key upon which the
57 	 * calling thread has not performed a pthread_setspecifc()
58 	 * must return NULL.  Consider the following sequence:
59 	 *
60 	 *	pthread_key_create(&key);
61 	 *	pthread_setspecific(key, datum);
62 	 *	pthread_key_delete(&key);
63 	 *	pthread_key_create(&key);
64 	 *	val = pthread_getspecific(key);
65 	 *
66 	 * According to POSIX, if the deleted key is reused for the new
67 	 * key returned by the second pthread_key_create(), then the
68 	 * pthread_getspecific() in the above example must return NULL
69 	 * (and not the stale datum).  The implementation is thus left
70 	 * with two alternatives:
71 	 *
72 	 *  (1)	Reuse deleted keys.  If this is to be implemented optimally,
73 	 *	it requires that pthread_key_create() somehow associate
74 	 *	the value NULL with the new (reused) key for each thread.
75 	 *	Keeping the hot path fast and lock-free induces substantial
76 	 *	complexity on the implementation.
77 	 *
78 	 *  (2)	Never reuse deleted keys. This allows the pthread_getspecific()
79 	 *	implementation to simply perform a check against the number
80 	 *	of keys set by the calling thread, returning NULL if the
81 	 *	specified key is larger than the highest set key.  This has
82 	 *	the disadvantage of wasting memory (a program which simply
83 	 *	loops calling pthread_key_create()/pthread_key_delete()
84 	 *	will ultimately run out of memory), but permits an optimal
85 	 *	pthread_getspecific() while allowing for simple key creation
86 	 *	and deletion.
87 	 *
88 	 * All Solaris implementations have opted for (2).  Given the
89 	 * ~10 years that this has been in the field, it is safe to assume
90 	 * that applications don't loop creating and destroying keys; we
91 	 * stick with (2).
92 	 */
93 	if (tsdm->tsdm_nused == (old_nkeys = tsdm->tsdm_nkeys)) {
94 		/*
95 		 * We need to allocate or double the number of keys.
96 		 * tsdm->tsdm_nused must always be a power of two.
97 		 */
98 		if ((new_nkeys = (old_nkeys << 1)) == 0)
99 			new_nkeys = 8;
100 
101 		if (new_nkeys > MAX_KEYS) {
102 			lmutex_unlock(&tsdm->tsdm_lock);
103 			return (EAGAIN);
104 		}
105 		if ((new_data = lmalloc(new_nkeys * sizeof (void *))) == NULL) {
106 			lmutex_unlock(&tsdm->tsdm_lock);
107 			return (ENOMEM);
108 		}
109 		if ((old_data = tsdm->tsdm_destro) == NULL) {
110 			/* key == 0 is always invalid */
111 			new_data[0] = TSD_UNALLOCATED;
112 			tsdm->tsdm_nused = 1;
113 		} else {
114 			(void) _private_memcpy(new_data, old_data,
115 				old_nkeys * sizeof (void *));
116 		}
117 		tsdm->tsdm_destro = new_data;
118 		tsdm->tsdm_nkeys = new_nkeys;
119 	}
120 
121 	*pkey = tsdm->tsdm_nused;
122 	tsdm->tsdm_destro[tsdm->tsdm_nused++] = destructor;
123 	lmutex_unlock(&tsdm->tsdm_lock);
124 
125 	if (old_data != NULL)
126 		lfree(old_data, old_nkeys * sizeof (void *));
127 
128 	return (0);
129 }
130 
131 #pragma weak pthread_key_delete = _thr_key_delete
132 #pragma weak _pthread_key_delete = _thr_key_delete
133 int
134 _thr_key_delete(thread_key_t key)
135 {
136 	tsd_metadata_t *tsdm = &curthread->ul_uberdata->tsd_metadata;
137 
138 	lmutex_lock(&tsdm->tsdm_lock);
139 
140 	if (key >= tsdm->tsdm_nused ||
141 	    tsdm->tsdm_destro[key] == TSD_UNALLOCATED) {
142 		lmutex_unlock(&tsdm->tsdm_lock);
143 		return (EINVAL);
144 	}
145 
146 	tsdm->tsdm_destro[key] = TSD_UNALLOCATED;
147 	lmutex_unlock(&tsdm->tsdm_lock);
148 
149 	return (0);
150 }
151 
152 /*
153  * Blessedly, the pthread_getspecific() interface is much better than the
154  * thr_getspecific() interface in that it cannot return an error status.
155  * Thus, if the key specified is bogus, pthread_getspecific()'s behavior
156  * is undefined.  As an added bonus (and as an artificat of not returning
157  * an error code), the requested datum is returned rather than stored
158  * through a parameter -- thereby avoiding the unnecessary store/load pair
159  * incurred by thr_getspecific().  Every once in a while, the Standards
160  * get it right -- but usually by accident.
161  */
162 #pragma weak	pthread_getspecific	= _pthread_getspecific
163 void *
164 _pthread_getspecific(pthread_key_t key)
165 {
166 	tsd_t *stsd;
167 
168 	/*
169 	 * We are cycle-shaving in this function because some
170 	 * applications make heavy use of it and one machine cycle
171 	 * can make a measurable difference in performance.  This
172 	 * is why we waste a little memory and allocate a NULL value
173 	 * for the invalid key == 0 in curthread->ul_ftsd[0] rather
174 	 * than adjusting the key by subtracting one.
175 	 */
176 	if (key < TSD_NFAST)
177 		return (curthread->ul_ftsd[key]);
178 
179 	if ((stsd = curthread->ul_stsd) != NULL && key < stsd->tsd_nalloc)
180 		return (stsd->tsd_data[key]);
181 
182 	return (NULL);
183 }
184 
185 #pragma weak thr_getspecific = _thr_getspecific
186 int
187 _thr_getspecific(thread_key_t key, void **valuep)
188 {
189 	tsd_t *stsd;
190 
191 	/*
192 	 * Amazingly, some application code (and worse, some particularly
193 	 * fugly Solaris library code) _relies_ on the fact that 0 is always
194 	 * an invalid key.  To preserve this semantic, 0 is never returned
195 	 * as a key from thr_/pthread_key_create(); we explicitly check
196 	 * for it here and return EINVAL.
197 	 */
198 	if (key == 0)
199 		return (EINVAL);
200 
201 	if (key < TSD_NFAST)
202 		*valuep = curthread->ul_ftsd[key];
203 	else if ((stsd = curthread->ul_stsd) != NULL && key < stsd->tsd_nalloc)
204 		*valuep = stsd->tsd_data[key];
205 	else
206 		*valuep = NULL;
207 
208 	return (0);
209 }
210 
211 /*
212  * We call _thr_setspecific_slow() when the key specified
213  * is beyond the current thread's currently allocated range.
214  * This case is in a separate function because we want
215  * the compiler to optimize for the common case.
216  */
217 static int
218 _thr_setspecific_slow(thread_key_t key, void *value)
219 {
220 	ulwp_t *self = curthread;
221 	tsd_metadata_t *tsdm = &self->ul_uberdata->tsd_metadata;
222 	tsd_t *stsd;
223 	tsd_t *ntsd;
224 	uint_t nkeys;
225 
226 	/*
227 	 * It isn't necessary to grab locks in this path;
228 	 * tsdm->tsdm_nused can only increase.
229 	 */
230 	if (key >= tsdm->tsdm_nused)
231 		return (EINVAL);
232 
233 	/*
234 	 * We would like to test (tsdm->tsdm_destro[key] == TSD_UNALLOCATED)
235 	 * here but that would require acquiring tsdm->tsdm_lock and we
236 	 * want to avoid locks in this path.
237 	 *
238 	 * We have a key which is (or at least _was_) valid.  If this key
239 	 * is later deleted (or indeed, is deleted before we set the value),
240 	 * we don't care; such a condition would indicate an application
241 	 * race for which POSIX thankfully leaves the behavior unspecified.
242 	 *
243 	 * First, determine our new size.  To avoid allocating more than we
244 	 * have to, continue doubling our size only until the new key fits.
245 	 * stsd->tsd_nalloc must always be a power of two.
246 	 */
247 	nkeys = ((stsd = self->ul_stsd) != NULL)? stsd->tsd_nalloc : 8;
248 	for (; key >= nkeys; nkeys <<= 1)
249 		continue;
250 
251 	/*
252 	 * Allocate the new TSD.
253 	 */
254 	if ((ntsd = lmalloc(nkeys * sizeof (void *))) == NULL)
255 		return (ENOMEM);
256 
257 	if (stsd != NULL) {
258 		/*
259 		 * Copy the old TSD across to the new.
260 		 */
261 		(void) _private_memcpy(ntsd, stsd,
262 			stsd->tsd_nalloc * sizeof (void *));
263 		lfree(stsd, stsd->tsd_nalloc * sizeof (void *));
264 	}
265 
266 	ntsd->tsd_nalloc = nkeys;
267 	ntsd->tsd_data[key] = value;
268 	self->ul_stsd = ntsd;
269 
270 	return (0);
271 }
272 
273 #pragma weak thr_setspecific = _thr_setspecific
274 #pragma weak pthread_setspecific = _thr_setspecific
275 #pragma weak _pthread_setspecific = _thr_setspecific
276 int
277 _thr_setspecific(thread_key_t key, void *value)
278 {
279 	tsd_t *stsd;
280 	int ret;
281 	ulwp_t *self = curthread;
282 
283 	/*
284 	 * See the comment in _thr_getspecific(), above.
285 	 */
286 	if (key == 0)
287 		return (EINVAL);
288 
289 	if (key < TSD_NFAST) {
290 		curthread->ul_ftsd[key] = value;
291 		return (0);
292 	}
293 
294 	if ((stsd = curthread->ul_stsd) != NULL && key < stsd->tsd_nalloc) {
295 		stsd->tsd_data[key] = value;
296 		return (0);
297 	}
298 
299 	/*
300 	 * This is a critical region since we are dealing with memory
301 	 * allocation and free. Similar protection required in tsd_free().
302 	 */
303 	enter_critical(self);
304 	ret = _thr_setspecific_slow(key, value);
305 	exit_critical(self);
306 	return (ret);
307 }
308 
309 /*
310  * Contract-private interface for java.  See PSARC/2003/159
311  *
312  * If the key falls within the TSD_NFAST range, return a non-negative
313  * offset that can be used by the caller to fetch the TSD data value
314  * directly out of the thread structure using %g7 (sparc) or %gs (x86).
315  * With the advent of TLS, %g7 and %gs are part of the ABI, even though
316  * the definition of the thread structure itself (ulwp_t) is private.
317  *
318  * We guarantee that the offset returned on sparc will fit within
319  * a SIMM13 field (that is, it is less than 2048).
320  *
321  * On failure (key is not in the TSD_NFAST range), return -1.
322  */
323 ptrdiff_t
324 _thr_slot_offset(thread_key_t key)
325 {
326 	if (key != 0 && key < TSD_NFAST)
327 		return ((ptrdiff_t)offsetof(ulwp_t, ul_ftsd[key]));
328 	return (-1);
329 }
330 
331 /*
332  * This is called by _thrp_exit() to apply destructors to the thread's tsd.
333  */
334 void
335 tsd_exit()
336 {
337 	ulwp_t *self = curthread;
338 	tsd_metadata_t *tsdm = &self->ul_uberdata->tsd_metadata;
339 	thread_key_t key;
340 	int recheck;
341 	void *val;
342 	void (*func)(void *);
343 
344 	lmutex_lock(&tsdm->tsdm_lock);
345 
346 	do {
347 		recheck = 0;
348 
349 		for (key = 1; key < TSD_NFAST &&
350 		    key < tsdm->tsdm_nused; key++) {
351 			if ((func = tsdm->tsdm_destro[key]) != NULL &&
352 			    func != TSD_UNALLOCATED &&
353 			    (val = self->ul_ftsd[key]) != NULL) {
354 				self->ul_ftsd[key] = NULL;
355 				lmutex_unlock(&tsdm->tsdm_lock);
356 				(*func)(val);
357 				lmutex_lock(&tsdm->tsdm_lock);
358 				recheck = 1;
359 			}
360 		}
361 
362 		if (self->ul_stsd == NULL)
363 			continue;
364 
365 		/*
366 		 * Any of these destructors could cause us to grow the number
367 		 * TSD keys in the slow TSD; we cannot cache the slow TSD
368 		 * pointer through this loop.
369 		 */
370 		for (; key < self->ul_stsd->tsd_nalloc &&
371 		    key < tsdm->tsdm_nused; key++) {
372 			if ((func = tsdm->tsdm_destro[key]) != NULL &&
373 			    func != TSD_UNALLOCATED &&
374 			    (val = self->ul_stsd->tsd_data[key]) != NULL) {
375 				self->ul_stsd->tsd_data[key] = NULL;
376 				lmutex_unlock(&tsdm->tsdm_lock);
377 				(*func)(val);
378 				lmutex_lock(&tsdm->tsdm_lock);
379 				recheck = 1;
380 			}
381 		}
382 	} while (recheck);
383 
384 	lmutex_unlock(&tsdm->tsdm_lock);
385 
386 	/*
387 	 * We're done; if we have slow TSD, we need to free it.
388 	 */
389 	tsd_free(self);
390 }
391 
392 void
393 tsd_free(ulwp_t *ulwp)
394 {
395 	tsd_t *stsd;
396 	ulwp_t *self = curthread;
397 
398 	enter_critical(self);
399 	if ((stsd = ulwp->ul_stsd) != NULL)
400 		lfree(stsd, stsd->tsd_nalloc * sizeof (void *));
401 	ulwp->ul_stsd = NULL;
402 	exit_critical(self);
403 }
404