2 * Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include "namespace.h"
40 #include "un-namespace.h"
41 #include "libc_private.h"
43 #include "thr_private.h"
45 struct pthread_key _thread_keytable[PTHREAD_KEYS_MAX];
47 __weak_reference(_pthread_key_create, pthread_key_create);
48 __weak_reference(_pthread_key_delete, pthread_key_delete);
49 __weak_reference(_pthread_getspecific, pthread_getspecific);
50 __weak_reference(_pthread_setspecific, pthread_setspecific);
54 _pthread_key_create(pthread_key_t *key, void (*destructor)(void *))
56 struct pthread *curthread;
61 curthread = _get_curthread();
63 THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
64 for (i = 0; i < PTHREAD_KEYS_MAX; i++) {
66 if (_thread_keytable[i].allocated == 0) {
67 _thread_keytable[i].allocated = 1;
68 _thread_keytable[i].destructor = destructor;
69 _thread_keytable[i].seqno++;
71 THR_LOCK_RELEASE(curthread, &_keytable_lock);
77 THR_LOCK_RELEASE(curthread, &_keytable_lock);
82 _pthread_key_delete(pthread_key_t userkey)
84 struct pthread *curthread;
88 if ((unsigned int)key >= PTHREAD_KEYS_MAX)
90 curthread = _get_curthread();
91 THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
92 if (_thread_keytable[key].allocated) {
93 _thread_keytable[key].allocated = 0;
98 THR_LOCK_RELEASE(curthread, &_keytable_lock);
103 _thread_cleanupspecific(void)
105 struct pthread *curthread;
106 void (*destructor)(void *);
110 curthread = _get_curthread();
111 if (curthread->specific == NULL)
113 THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
114 for (i = 0; i < PTHREAD_DESTRUCTOR_ITERATIONS &&
115 curthread->specific_data_count > 0; i++) {
116 for (key = 0; key < PTHREAD_KEYS_MAX &&
117 curthread->specific_data_count > 0; key++) {
120 if (_thread_keytable[key].allocated &&
121 (curthread->specific[key].data != NULL)) {
122 if (curthread->specific[key].seqno ==
123 _thread_keytable[key].seqno) {
124 data = curthread->specific[key].data;
125 destructor = _thread_keytable[key].
128 curthread->specific[key].data = NULL;
129 curthread->specific_data_count--;
130 } else if (curthread->specific[key].data != NULL) {
132 * This can happen if the key is
133 * deleted via pthread_key_delete
134 * without first setting the value to
135 * NULL in all threads. POSIX says
136 * that the destructor is not invoked
139 curthread->specific[key].data = NULL;
140 curthread->specific_data_count--;
144 * If there is a destructor, call it with the
145 * key table entry unlocked.
147 if (destructor != NULL) {
148 THR_LOCK_RELEASE(curthread, &_keytable_lock);
149 destructor(__DECONST(void *, data));
150 THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
154 THR_LOCK_RELEASE(curthread, &_keytable_lock);
155 munmap(curthread->specific, PTHREAD_KEYS_MAX * sizeof(struct
156 pthread_specific_elem));
157 curthread->specific = NULL;
158 if (curthread->specific_data_count > 0) {
159 stderr_debug("Thread %p has exited with leftover "
160 "thread-specific data after %d destructor iterations\n",
161 curthread, PTHREAD_DESTRUCTOR_ITERATIONS);
166 _pthread_setspecific(pthread_key_t userkey, const void *value)
168 struct pthread *pthread;
173 if ((unsigned int)key >= PTHREAD_KEYS_MAX ||
174 !_thread_keytable[key].allocated)
177 pthread = _get_curthread();
178 if (pthread->specific == NULL) {
179 tmp = mmap(NULL, PTHREAD_KEYS_MAX *
180 sizeof(struct pthread_specific_elem),
181 PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
182 if (tmp == MAP_FAILED)
184 pthread->specific = tmp;
186 if (pthread->specific[key].data == NULL) {
188 pthread->specific_data_count++;
189 } else if (value == NULL)
190 pthread->specific_data_count--;
191 pthread->specific[key].data = value;
192 pthread->specific[key].seqno = _thread_keytable[key].seqno;
197 _pthread_getspecific(pthread_key_t userkey)
199 struct pthread *pthread;
203 /* Check if there is specific data. */
205 if ((unsigned int)key >= PTHREAD_KEYS_MAX)
208 pthread = _get_curthread();
209 /* Check if this key has been used before. */
210 if (_thread_keytable[key].allocated && pthread->specific != NULL &&
211 pthread->specific[key].seqno == _thread_keytable[key].seqno) {
212 /* Return the value: */
213 data = pthread->specific[key].data;
216 * This key has not been used before, so return NULL
221 return (__DECONST(void *, data));
225 _thr_tsd_unload(struct dl_phdr_info *phdr_info)
227 struct pthread *curthread;
228 void (*destructor)(void *);
231 curthread = _get_curthread();
232 THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
233 for (key = 0; key < PTHREAD_KEYS_MAX; key++) {
234 if (!_thread_keytable[key].allocated)
236 destructor = _thread_keytable[key].destructor;
237 if (destructor == NULL)
239 if (__elf_phdr_match_addr(phdr_info, destructor))
240 _thread_keytable[key].destructor = NULL;
242 THR_LOCK_RELEASE(curthread, &_keytable_lock);