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"
38 #include "un-namespace.h"
39 #include "thr_private.h"
42 struct pthread_key _thread_keytable[PTHREAD_KEYS_MAX];
44 __weak_reference(_pthread_key_create, pthread_key_create);
45 __weak_reference(_pthread_key_delete, pthread_key_delete);
46 __weak_reference(_pthread_getspecific, pthread_getspecific);
47 __weak_reference(_pthread_setspecific, pthread_setspecific);
51 _pthread_key_create(pthread_key_t *key, void (*destructor) (void *))
53 struct pthread *curthread;
56 if (_thr_initial == NULL)
57 _libpthread_init(NULL);
58 curthread = _get_curthread();
60 /* Lock the key table: */
61 THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
62 for (i = 0; i < PTHREAD_KEYS_MAX; i++) {
64 if (_thread_keytable[i].allocated == 0) {
65 _thread_keytable[i].allocated = 1;
66 _thread_keytable[i].destructor = destructor;
67 _thread_keytable[i].seqno++;
69 /* Unlock the key table: */
70 THR_LOCK_RELEASE(curthread, &_keytable_lock);
76 /* Unlock the key table: */
77 THR_LOCK_RELEASE(curthread, &_keytable_lock);
82 _pthread_key_delete(pthread_key_t key)
84 struct pthread *curthread = _get_curthread();
87 if ((unsigned int)key < PTHREAD_KEYS_MAX) {
88 /* Lock the key table: */
89 THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
91 if (_thread_keytable[key].allocated)
92 _thread_keytable[key].allocated = 0;
96 /* Unlock the key table: */
97 THR_LOCK_RELEASE(curthread, &_keytable_lock);
104 _thread_cleanupspecific(void)
106 struct pthread *curthread = _get_curthread();
107 const_key_destructor_t destructor;
108 const void *data = NULL;
112 if (curthread->specific == NULL)
115 /* Lock the key table: */
116 THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
117 for (i = 0; (i < PTHREAD_DESTRUCTOR_ITERATIONS) &&
118 (curthread->specific_data_count > 0); i++) {
119 for (key = 0; (key < PTHREAD_KEYS_MAX) &&
120 (curthread->specific_data_count > 0); key++) {
123 if (_thread_keytable[key].allocated &&
124 (curthread->specific[key].data != NULL)) {
125 if (curthread->specific[key].seqno ==
126 _thread_keytable[key].seqno) {
127 data = curthread->specific[key].data;
128 destructor = (const_key_destructor_t)
129 _thread_keytable[key].destructor;
131 curthread->specific[key].data = NULL;
132 curthread->specific_data_count--;
136 * If there is a destructore, call it
137 * with the key table entry unlocked:
139 if (destructor != NULL) {
141 * Don't hold the lock while calling the
144 THR_LOCK_RELEASE(curthread, &_keytable_lock);
146 THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
150 THR_LOCK_RELEASE(curthread, &_keytable_lock);
151 free(curthread->specific);
152 curthread->specific = NULL;
153 if (curthread->specific_data_count > 0)
154 stderr_debug("Thread %p has exited with leftover "
155 "thread-specific data after %d destructor iterations\n",
156 curthread, PTHREAD_DESTRUCTOR_ITERATIONS);
159 static inline struct pthread_specific_elem *
160 pthread_key_allocate_data(void)
162 struct pthread_specific_elem *new_data;
164 new_data = (struct pthread_specific_elem *)
165 malloc(sizeof(struct pthread_specific_elem) * PTHREAD_KEYS_MAX);
166 if (new_data != NULL) {
167 memset((void *) new_data, 0,
168 sizeof(struct pthread_specific_elem) * PTHREAD_KEYS_MAX);
174 _pthread_setspecific(pthread_key_t key, const void *value)
176 struct pthread *pthread;
179 /* Point to the running thread: */
180 pthread = _get_curthread();
182 if ((pthread->specific) ||
183 (pthread->specific = pthread_key_allocate_data())) {
184 if ((unsigned int)key < PTHREAD_KEYS_MAX) {
185 if (_thread_keytable[key].allocated) {
186 if (pthread->specific[key].data == NULL) {
188 pthread->specific_data_count++;
189 } else if (value == NULL)
190 pthread->specific_data_count--;
191 *(const void **)&pthread->specific[key].data = value;
192 pthread->specific[key].seqno =
193 _thread_keytable[key].seqno;
205 _pthread_getspecific(pthread_key_t key)
207 struct pthread *pthread;
210 /* Point to the running thread: */
211 pthread = _get_curthread();
213 /* Check if there is specific data: */
214 if (pthread->specific != NULL && (unsigned int)key < PTHREAD_KEYS_MAX) {
215 /* Check if this key has been used before: */
216 if (_thread_keytable[key].allocated &&
217 (pthread->specific[key].seqno == _thread_keytable[key].seqno)) {
218 /* Return the value: */
219 data = pthread->specific[key].data;
222 * This key has not been used before, so return NULL
228 /* No specific data has been created, so just return NULL: */