2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the author nor the names of any co-contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 #include "namespace.h"
40 #include "un-namespace.h"
41 #include "libc_private.h"
43 #include "thr_private.h"
45 /* Used in symbol lookup of libthread_db */
46 struct pthread_key _thread_keytable[PTHREAD_KEYS_MAX];
48 __weak_reference(_thr_key_create, pthread_key_create);
49 __weak_reference(_thr_key_create, _pthread_key_create);
50 __weak_reference(_thr_key_delete, pthread_key_delete);
51 __weak_reference(_thr_key_delete, _pthread_key_delete);
52 __weak_reference(_thr_getspecific, pthread_getspecific);
53 __weak_reference(_thr_getspecific, _pthread_getspecific);
54 __weak_reference(_thr_setspecific, pthread_setspecific);
55 __weak_reference(_thr_setspecific, _pthread_setspecific);
58 _thr_key_create(pthread_key_t *key, void (*destructor)(void *))
60 struct pthread *curthread;
65 curthread = _get_curthread();
67 THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
68 for (i = 0; i < PTHREAD_KEYS_MAX; i++) {
70 if (_thread_keytable[i].allocated == 0) {
71 _thread_keytable[i].allocated = 1;
72 _thread_keytable[i].destructor = destructor;
73 _thread_keytable[i].seqno++;
75 THR_LOCK_RELEASE(curthread, &_keytable_lock);
81 THR_LOCK_RELEASE(curthread, &_keytable_lock);
86 _thr_key_delete(pthread_key_t userkey)
88 struct pthread *curthread;
92 if ((unsigned int)key >= PTHREAD_KEYS_MAX)
94 curthread = _get_curthread();
95 THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
96 if (_thread_keytable[key].allocated) {
97 _thread_keytable[key].allocated = 0;
102 THR_LOCK_RELEASE(curthread, &_keytable_lock);
107 _thread_cleanupspecific(void)
109 struct pthread *curthread;
110 void (*destructor)(void *);
114 curthread = _get_curthread();
115 if (curthread->specific == NULL)
117 THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
118 for (i = 0; i < PTHREAD_DESTRUCTOR_ITERATIONS &&
119 curthread->specific_data_count > 0; i++) {
120 for (key = 0; key < PTHREAD_KEYS_MAX &&
121 curthread->specific_data_count > 0; key++) {
124 if (_thread_keytable[key].allocated &&
125 (curthread->specific[key].data != NULL)) {
126 if (curthread->specific[key].seqno ==
127 _thread_keytable[key].seqno) {
128 data = curthread->specific[key].data;
129 destructor = _thread_keytable[key].
132 curthread->specific[key].data = NULL;
133 curthread->specific_data_count--;
134 } else if (curthread->specific[key].data != NULL) {
136 * This can happen if the key is
137 * deleted via pthread_key_delete
138 * without first setting the value to
139 * NULL in all threads. POSIX says
140 * that the destructor is not invoked
143 curthread->specific[key].data = NULL;
144 curthread->specific_data_count--;
148 * If there is a destructor, call it with the
149 * key table entry unlocked.
151 if (destructor != NULL) {
152 THR_LOCK_RELEASE(curthread, &_keytable_lock);
153 destructor(__DECONST(void *, data));
154 THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
158 THR_LOCK_RELEASE(curthread, &_keytable_lock);
159 __thr_free(curthread->specific);
160 curthread->specific = NULL;
161 if (curthread->specific_data_count > 0) {
162 stderr_debug("Thread %p has exited with leftover "
163 "thread-specific data after %d destructor iterations\n",
164 curthread, PTHREAD_DESTRUCTOR_ITERATIONS);
169 _thr_setspecific(pthread_key_t userkey, const void *value)
171 struct pthread *pthread;
176 if ((unsigned int)key >= PTHREAD_KEYS_MAX ||
177 !_thread_keytable[key].allocated)
180 pthread = _get_curthread();
181 if (pthread->specific == NULL) {
182 tmp = __thr_calloc(PTHREAD_KEYS_MAX,
183 sizeof(struct pthread_specific_elem));
186 pthread->specific = tmp;
188 if (pthread->specific[key].data == NULL) {
190 pthread->specific_data_count++;
191 } else if (value == NULL)
192 pthread->specific_data_count--;
193 pthread->specific[key].data = value;
194 pthread->specific[key].seqno = _thread_keytable[key].seqno;
199 _thr_getspecific(pthread_key_t userkey)
201 struct pthread *pthread;
205 /* Check if there is specific data. */
207 if ((unsigned int)key >= PTHREAD_KEYS_MAX)
210 pthread = _get_curthread();
211 /* Check if this key has been used before. */
212 if (_thread_keytable[key].allocated && pthread->specific != NULL &&
213 pthread->specific[key].seqno == _thread_keytable[key].seqno) {
214 /* Return the value: */
215 data = pthread->specific[key].data;
218 * This key has not been used before, so return NULL
223 return (__DECONST(void *, data));
227 _thr_tsd_unload(struct dl_phdr_info *phdr_info)
229 struct pthread *curthread;
230 void (*destructor)(void *);
233 curthread = _get_curthread();
234 THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
235 for (key = 0; key < PTHREAD_KEYS_MAX; key++) {
236 if (!_thread_keytable[key].allocated)
238 destructor = _thread_keytable[key].destructor;
239 if (destructor == NULL)
241 if (__elf_phdr_match_addr(phdr_info, destructor))
242 _thread_keytable[key].destructor = NULL;
244 THR_LOCK_RELEASE(curthread, &_keytable_lock);