pthread_tsd.c revision 1.3.2.1 1 1.3.2.1 matt /* $NetBSD: pthread_tsd.c,v 1.3.2.1 2008/01/09 01:36:40 matt Exp $ */
2 1.1 nathanw
3 1.1 nathanw /*-
4 1.3 ad * Copyright (c) 2001, 2007 The NetBSD Foundation, Inc.
5 1.1 nathanw * All rights reserved.
6 1.1 nathanw *
7 1.1 nathanw * This code is derived from software contributed to The NetBSD Foundation
8 1.3.2.1 matt * by Nathan J. Williams, and by Andrew Doran.
9 1.1 nathanw *
10 1.1 nathanw * Redistribution and use in source and binary forms, with or without
11 1.1 nathanw * modification, are permitted provided that the following conditions
12 1.1 nathanw * are met:
13 1.1 nathanw * 1. Redistributions of source code must retain the above copyright
14 1.1 nathanw * notice, this list of conditions and the following disclaimer.
15 1.1 nathanw * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 nathanw * notice, this list of conditions and the following disclaimer in the
17 1.1 nathanw * documentation and/or other materials provided with the distribution.
18 1.1 nathanw * 3. All advertising materials mentioning features or use of this software
19 1.1 nathanw * must display the following acknowledgement:
20 1.1 nathanw * This product includes software developed by the NetBSD
21 1.1 nathanw * Foundation, Inc. and its contributors.
22 1.1 nathanw * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 nathanw * contributors may be used to endorse or promote products derived
24 1.1 nathanw * from this software without specific prior written permission.
25 1.1 nathanw *
26 1.1 nathanw * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 nathanw * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 nathanw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 nathanw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 nathanw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 nathanw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 nathanw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 nathanw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 nathanw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 nathanw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 nathanw * POSSIBILITY OF SUCH DAMAGE.
37 1.1 nathanw */
38 1.1 nathanw
39 1.1 nathanw #include <sys/cdefs.h>
40 1.3.2.1 matt __RCSID("$NetBSD: pthread_tsd.c,v 1.3.2.1 2008/01/09 01:36:40 matt Exp $");
41 1.1 nathanw
42 1.1 nathanw /* Functions and structures dealing with thread-specific data */
43 1.1 nathanw #include <errno.h>
44 1.1 nathanw
45 1.1 nathanw #include "pthread.h"
46 1.1 nathanw #include "pthread_int.h"
47 1.1 nathanw
48 1.1 nathanw static pthread_mutex_t tsd_mutex = PTHREAD_MUTEX_INITIALIZER;
49 1.1 nathanw static int nextkey;
50 1.1 nathanw int pthread__tsd_alloc[PTHREAD_KEYS_MAX];
51 1.1 nathanw void (*pthread__tsd_destructors[PTHREAD_KEYS_MAX])(void *);
52 1.1 nathanw
53 1.1 nathanw __strong_alias(__libc_thr_keycreate,pthread_key_create)
54 1.1 nathanw __strong_alias(__libc_thr_keydelete,pthread_key_delete)
55 1.1 nathanw
56 1.1 nathanw int
57 1.1 nathanw pthread_key_create(pthread_key_t *key, void (*destructor)(void *))
58 1.1 nathanw {
59 1.1 nathanw int i;
60 1.1 nathanw
61 1.1 nathanw /* Get a lock on the allocation list */
62 1.1 nathanw pthread_mutex_lock(&tsd_mutex);
63 1.1 nathanw
64 1.2 wiz /* Find an available slot */
65 1.1 nathanw /* 1. Search from "nextkey" to the end of the list. */
66 1.1 nathanw for (i = nextkey; i < PTHREAD_KEYS_MAX; i++)
67 1.1 nathanw if (pthread__tsd_alloc[i] == 0)
68 1.1 nathanw break;
69 1.1 nathanw
70 1.1 nathanw if (i == PTHREAD_KEYS_MAX) {
71 1.1 nathanw /* 2. If that didn't work, search from the start
72 1.1 nathanw * of the list back to "nextkey".
73 1.1 nathanw */
74 1.1 nathanw for (i = 0; i < nextkey; i++)
75 1.1 nathanw if (pthread__tsd_alloc[i] == 0)
76 1.1 nathanw break;
77 1.1 nathanw
78 1.1 nathanw if (i == nextkey) {
79 1.1 nathanw /* If we didn't find one here, there isn't one
80 1.1 nathanw * to be found.
81 1.1 nathanw */
82 1.1 nathanw pthread_mutex_unlock(&tsd_mutex);
83 1.1 nathanw return EAGAIN;
84 1.1 nathanw }
85 1.1 nathanw }
86 1.1 nathanw
87 1.1 nathanw /* Got one. */
88 1.1 nathanw pthread__tsd_alloc[i] = 1;
89 1.1 nathanw nextkey = (i + 1) % PTHREAD_KEYS_MAX;
90 1.1 nathanw pthread__tsd_destructors[i] = destructor;
91 1.1 nathanw pthread_mutex_unlock(&tsd_mutex);
92 1.1 nathanw *key = i;
93 1.1 nathanw
94 1.1 nathanw return 0;
95 1.1 nathanw }
96 1.1 nathanw
97 1.1 nathanw int
98 1.1 nathanw pthread_key_delete(pthread_key_t key)
99 1.1 nathanw {
100 1.1 nathanw
101 1.1 nathanw /*
102 1.1 nathanw * This is tricky. The standard says of pthread_key_create()
103 1.1 nathanw * that new keys have the value NULL associated with them in
104 1.1 nathanw * all threads. According to people who were present at the
105 1.1 nathanw * standardization meeting, that requirement was written
106 1.1 nathanw * before pthread_key_delete() was introduced, and not
107 1.1 nathanw * reconsidered when it was.
108 1.1 nathanw *
109 1.1 nathanw * See David Butenhof's article in comp.programming.threads:
110 1.1 nathanw * Subject: Re: TSD key reusing issue
111 1.1 nathanw * Message-ID: <u97d8.29$fL6.200 (at) news.cpqcorp.net>
112 1.1 nathanw * Date: Thu, 21 Feb 2002 09:06:17 -0500
113 1.1 nathanw * http://groups.google.com/groups?hl=en&selm=u97d8.29%24fL6.200%40news.cpqcorp.net
114 1.1 nathanw *
115 1.1 nathanw * Given:
116 1.1 nathanw *
117 1.1 nathanw * 1: Applications are not required to clear keys in all
118 1.1 nathanw * threads before calling pthread_key_delete().
119 1.1 nathanw * 2: Clearing pointers without running destructors is a
120 1.1 nathanw * memory leak.
121 1.1 nathanw * 3: The pthread_key_delete() function is expressly forbidden
122 1.1 nathanw * to run any destructors.
123 1.1 nathanw *
124 1.1 nathanw * Option 1: Make this function effectively a no-op and
125 1.1 nathanw * prohibit key reuse. This is a possible resource-exhaustion
126 1.1 nathanw * problem given that we have a static storage area for keys,
127 1.1 nathanw * but having a non-static storage area would make
128 1.1 nathanw * pthread_setspecific() expensive (might need to realloc the
129 1.1 nathanw * TSD array).
130 1.1 nathanw *
131 1.1 nathanw * Option 2: Ignore the specified behavior of
132 1.1 nathanw * pthread_key_create() and leave the old values. If an
133 1.1 nathanw * application deletes a key that still has non-NULL values in
134 1.1 nathanw * some threads... it's probably a memory leak and hence
135 1.1 nathanw * incorrect anyway, and we're within our rights to let the
136 1.1 nathanw * application lose. However, it's possible (if unlikely) that
137 1.1 nathanw * the application is storing pointers to non-heap data, or
138 1.1 nathanw * non-pointers that have been wedged into a void pointer, so
139 1.1 nathanw * we can't entirely write off such applications as incorrect.
140 1.1 nathanw * This could also lead to running (new) destructors on old
141 1.1 nathanw * data that was never supposed to be associated with that
142 1.1 nathanw * destructor.
143 1.1 nathanw *
144 1.1 nathanw * Option 3: Follow the specified behavior of
145 1.1 nathanw * pthread_key_create(). Either pthread_key_create() or
146 1.1 nathanw * pthread_key_delete() would then have to clear the values in
147 1.1 nathanw * every thread's slot for that key. In order to guarantee the
148 1.1 nathanw * visibility of the NULL value in other threads, there would
149 1.1 nathanw * have to be synchronization operations in both the clearer
150 1.1 nathanw * and pthread_getspecific(). Putting synchronization in
151 1.1 nathanw * pthread_getspecific() is a big performance lose. But in
152 1.1 nathanw * reality, only (buggy) reuse of an old key would require
153 1.1 nathanw * this synchronization; for a new key, there has to be a
154 1.1 nathanw * memory-visibility propagating event between the call to
155 1.1 nathanw * pthread_key_create() and pthread_getspecific() with that
156 1.1 nathanw * key, so setting the entries to NULL without synchronization
157 1.1 nathanw * will work, subject to problem (2) above. However, it's kind
158 1.1 nathanw * of slow.
159 1.1 nathanw *
160 1.1 nathanw * Note that the argument in option 3 only applies because we
161 1.1 nathanw * keep TSD in ordinary memory which follows the pthreads
162 1.1 nathanw * visibility rules. The visibility rules are not required by
163 1.1 nathanw * the standard to apply to TSD, so the argument doesn't
164 1.1 nathanw * apply in general, just to this implementation.
165 1.1 nathanw */
166 1.1 nathanw
167 1.1 nathanw /* For the momemt, we're going with option 1. */
168 1.1 nathanw pthread_mutex_lock(&tsd_mutex);
169 1.1 nathanw pthread__tsd_destructors[key] = NULL;
170 1.1 nathanw pthread_mutex_unlock(&tsd_mutex);
171 1.1 nathanw
172 1.1 nathanw return 0;
173 1.1 nathanw }
174 1.1 nathanw
175 1.1 nathanw /* Perform thread-exit-time destruction of thread-specific data. */
176 1.1 nathanw void
177 1.1 nathanw pthread__destroy_tsd(pthread_t self)
178 1.1 nathanw {
179 1.1 nathanw int i, done, iterations;
180 1.1 nathanw void *val;
181 1.1 nathanw void (*destructor)(void *);
182 1.1 nathanw
183 1.3 ad if (!self->pt_havespecific)
184 1.3 ad return;
185 1.3.2.1 matt pthread_mutex_unlock(&self->pt_lock);
186 1.3 ad
187 1.1 nathanw /* Butenhof, section 5.4.2 (page 167):
188 1.1 nathanw *
189 1.1 nathanw * ``Also, Pthreads sets the thread-specific data value for a
190 1.1 nathanw * key to NULL before calling that key's destructor (passing
191 1.1 nathanw * the previous value of the key) when a thread terminates [*].
192 1.1 nathanw * ...
193 1.1 nathanw * [*] That is, unfortunately, not what the standard
194 1.1 nathanw * says. This is one of the problems with formal standards -
195 1.1 nathanw * they say what they say, not what they were intended to
196 1.1 nathanw * say. Somehow, an error crept in, and the sentence
197 1.1 nathanw * specifying that "the implementation clears the
198 1.1 nathanw * thread-specific data value before calling the destructor"
199 1.1 nathanw * was deleted. Nobody noticed, and the standard was approved
200 1.1 nathanw * with the error. So the standard says (by omission) that if
201 1.1 nathanw * you want to write a portable application using
202 1.1 nathanw * thread-specific data, that will not hang on thread
203 1.1 nathanw * termination, you must call pthread_setspecific within your
204 1.1 nathanw * destructor function to change the value to NULL. This would
205 1.1 nathanw * be silly, and any serious implementation of Pthreads will
206 1.1 nathanw * violate the standard in this respect. Of course, the
207 1.1 nathanw * standard will be fixed, probably by the 1003.1n amendment
208 1.1 nathanw * (assorted corrections to 1003.1c-1995), but that will take
209 1.1 nathanw * a while.''
210 1.1 nathanw */
211 1.1 nathanw
212 1.1 nathanw iterations = 4; /* We're not required to try very hard */
213 1.1 nathanw do {
214 1.1 nathanw done = 1;
215 1.1 nathanw for (i = 0; i < PTHREAD_KEYS_MAX; i++) {
216 1.1 nathanw if (self->pt_specific[i] != NULL) {
217 1.1 nathanw pthread_mutex_lock(&tsd_mutex);
218 1.1 nathanw destructor = pthread__tsd_destructors[i];
219 1.1 nathanw pthread_mutex_unlock(&tsd_mutex);
220 1.1 nathanw if (destructor != NULL) {
221 1.1 nathanw done = 0;
222 1.1 nathanw val = self->pt_specific[i];
223 1.1 nathanw self->pt_specific[i] = NULL; /* see above */
224 1.1 nathanw (*destructor)(val);
225 1.1 nathanw }
226 1.1 nathanw }
227 1.1 nathanw }
228 1.1 nathanw } while (!done && iterations--);
229 1.3 ad
230 1.3 ad self->pt_havespecific = 0;
231 1.3.2.1 matt pthread_mutex_lock(&self->pt_lock);
232 1.1 nathanw }
233