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
32 #include "namespace.h"
39 #include "un-namespace.h"
40 #include "libc_private.h"
42 #include "thr_private.h"
44 struct pthread_key _thread_keytable[PTHREAD_KEYS_MAX];
46 __weak_reference(_pthread_key_create, pthread_key_create);
47 __weak_reference(_pthread_key_delete, pthread_key_delete);
48 __weak_reference(_pthread_getspecific, pthread_getspecific);
49 __weak_reference(_pthread_setspecific, pthread_setspecific);
53 _pthread_key_create(pthread_key_t *key, void (*destructor)(void *))
55 struct pthread *curthread;
60 curthread = _get_curthread();
62 THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
63 for (i = 0; i < PTHREAD_KEYS_MAX; i++) {
65 if (_thread_keytable[i].allocated == 0) {
66 _thread_keytable[i].allocated = 1;
67 _thread_keytable[i].destructor = destructor;
68 _thread_keytable[i].seqno++;
70 THR_LOCK_RELEASE(curthread, &_keytable_lock);
76 THR_LOCK_RELEASE(curthread, &_keytable_lock);
81 _pthread_key_delete(pthread_key_t userkey)
83 struct pthread *curthread;
87 if ((unsigned int)key >= PTHREAD_KEYS_MAX)
89 curthread = _get_curthread();
90 THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
91 if (_thread_keytable[key].allocated) {
92 _thread_keytable[key].allocated = 0;
97 THR_LOCK_RELEASE(curthread, &_keytable_lock);
102 _thread_cleanupspecific(void)
104 struct pthread *curthread;
105 void (*destructor)(void *);
109 curthread = _get_curthread();
110 if (curthread->specific == NULL)
112 THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
113 for (i = 0; i < PTHREAD_DESTRUCTOR_ITERATIONS &&
114 curthread->specific_data_count > 0; i++) {
115 for (key = 0; key < PTHREAD_KEYS_MAX &&
116 curthread->specific_data_count > 0; key++) {
119 if (_thread_keytable[key].allocated &&
120 (curthread->specific[key].data != NULL)) {
121 if (curthread->specific[key].seqno ==
122 _thread_keytable[key].seqno) {
123 data = curthread->specific[key].data;
124 destructor = _thread_keytable[key].
127 curthread->specific[key].data = NULL;
128 curthread->specific_data_count--;
129 } else if (curthread->specific[key].data != NULL) {
131 * This can happen if the key is
132 * deleted via pthread_key_delete
133 * without first setting the value to
134 * NULL in all threads. POSIX says
135 * that the destructor is not invoked
138 curthread->specific[key].data = NULL;
139 curthread->specific_data_count--;
143 * If there is a destructor, call it with the
144 * key table entry unlocked.
146 if (destructor != NULL) {
147 THR_LOCK_RELEASE(curthread, &_keytable_lock);
148 destructor(__DECONST(void *, data));
149 THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
153 THR_LOCK_RELEASE(curthread, &_keytable_lock);
154 munmap(curthread->specific, PTHREAD_KEYS_MAX * sizeof(struct
155 pthread_specific_elem));
156 curthread->specific = NULL;
157 if (curthread->specific_data_count > 0) {
158 stderr_debug("Thread %p has exited with leftover "
159 "thread-specific data after %d destructor iterations\n",
160 curthread, PTHREAD_DESTRUCTOR_ITERATIONS);
165 _pthread_setspecific(pthread_key_t userkey, const void *value)
167 struct pthread *pthread;
172 if ((unsigned int)key >= PTHREAD_KEYS_MAX ||
173 !_thread_keytable[key].allocated)
176 pthread = _get_curthread();
177 if (pthread->specific == NULL) {
178 tmp = mmap(NULL, PTHREAD_KEYS_MAX *
179 sizeof(struct pthread_specific_elem),
180 PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
181 if (tmp == MAP_FAILED)
183 pthread->specific = tmp;
185 if (pthread->specific[key].data == NULL) {
187 pthread->specific_data_count++;
188 } else if (value == NULL)
189 pthread->specific_data_count--;
190 pthread->specific[key].data = value;
191 pthread->specific[key].seqno = _thread_keytable[key].seqno;
196 _pthread_getspecific(pthread_key_t userkey)
198 struct pthread *pthread;
202 /* Check if there is specific data. */
204 if ((unsigned int)key >= PTHREAD_KEYS_MAX)
207 pthread = _get_curthread();
208 /* Check if this key has been used before. */
209 if (_thread_keytable[key].allocated && pthread->specific != NULL &&
210 pthread->specific[key].seqno == _thread_keytable[key].seqno) {
211 /* Return the value: */
212 data = pthread->specific[key].data;
215 * This key has not been used before, so return NULL
220 return (__DECONST(void *, data));
224 _thr_tsd_unload(struct dl_phdr_info *phdr_info)
226 struct pthread *curthread;
227 void (*destructor)(void *);
230 curthread = _get_curthread();
231 THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
232 for (key = 0; key < PTHREAD_KEYS_MAX; key++) {
233 if (!_thread_keytable[key].allocated)
235 destructor = _thread_keytable[key].destructor;
236 if (destructor == NULL)
238 if (__elf_phdr_match_addr(phdr_info, destructor))
239 _thread_keytable[key].destructor = NULL;
241 THR_LOCK_RELEASE(curthread, &_keytable_lock);