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
36 #include "pthread_private.h"
40 volatile int allocated;
43 void (*destructor) ();
46 /* Static variables: */
47 static struct pthread_key key_table[PTHREAD_KEYS_MAX];
49 __weak_reference(_pthread_key_create, pthread_key_create);
50 __weak_reference(_pthread_key_delete, pthread_key_delete);
51 __weak_reference(_pthread_getspecific, pthread_getspecific);
52 __weak_reference(_pthread_setspecific, pthread_setspecific);
56 _pthread_key_create(pthread_key_t * key, void (*destructor) (void *))
58 for ((*key) = 0; (*key) < PTHREAD_KEYS_MAX; (*key)++) {
59 /* Lock the key table entry: */
60 _SPINLOCK(&key_table[*key].lock);
62 if (key_table[(*key)].allocated == 0) {
63 key_table[(*key)].allocated = 1;
64 key_table[(*key)].destructor = destructor;
65 key_table[(*key)].seqno++;
67 /* Unlock the key table entry: */
68 _SPINUNLOCK(&key_table[*key].lock);
72 /* Unlock the key table entry: */
73 _SPINUNLOCK(&key_table[*key].lock);
79 _pthread_key_delete(pthread_key_t key)
83 if (key < PTHREAD_KEYS_MAX) {
84 /* Lock the key table entry: */
85 _SPINLOCK(&key_table[key].lock);
87 if (key_table[key].allocated)
88 key_table[key].allocated = 0;
92 /* Unlock the key table entry: */
93 _SPINUNLOCK(&key_table[key].lock);
100 _thread_cleanupspecific(void)
102 struct pthread *curthread = _get_curthread();
106 void (*destructor)( void *);
108 for (itr = 0; itr < PTHREAD_DESTRUCTOR_ITERATIONS; itr++) {
109 for (key = 0; key < PTHREAD_KEYS_MAX; key++) {
110 if (curthread->specific_data_count > 0) {
111 /* Lock the key table entry: */
112 _SPINLOCK(&key_table[key].lock);
115 if (key_table[key].allocated &&
116 (curthread->specific[key].data != NULL)) {
117 if (curthread->specific[key].seqno ==
118 key_table[key].seqno) {
119 data = (void *) curthread->specific[key].data;
120 destructor = key_table[key].destructor;
122 curthread->specific[key].data = NULL;
123 curthread->specific_data_count--;
126 /* Unlock the key table entry: */
127 _SPINUNLOCK(&key_table[key].lock);
130 * If there is a destructore, call it
131 * with the key table entry unlocked:
136 free(curthread->specific);
137 curthread->specific = NULL;
142 if (curthread->specific != NULL) {
143 free(curthread->specific);
144 curthread->specific = NULL;
148 static inline struct pthread_specific_elem *
149 pthread_key_allocate_data(void)
151 struct pthread_specific_elem *new_data;
153 new_data = (struct pthread_specific_elem *)
154 malloc(sizeof(struct pthread_specific_elem) * PTHREAD_KEYS_MAX);
155 if (new_data != NULL) {
156 memset((void *) new_data, 0,
157 sizeof(struct pthread_specific_elem) * PTHREAD_KEYS_MAX);
163 _pthread_setspecific(pthread_key_t key, const void *value)
165 struct pthread *pthread;
168 /* Point to the running thread: */
169 pthread = _get_curthread();
171 if ((pthread->specific) ||
172 (pthread->specific = pthread_key_allocate_data())) {
173 if (key < PTHREAD_KEYS_MAX) {
174 if (key_table[key].allocated) {
175 if (pthread->specific[key].data == NULL) {
177 pthread->specific_data_count++;
180 pthread->specific_data_count--;
182 pthread->specific[key].data = value;
183 pthread->specific[key].seqno =
184 key_table[key].seqno;
196 _pthread_getspecific(pthread_key_t key)
198 struct pthread *pthread;
201 /* Point to the running thread: */
202 pthread = _get_curthread();
204 /* Check if there is specific data: */
205 if (pthread->specific != NULL && key < PTHREAD_KEYS_MAX) {
206 /* Check if this key has been used before: */
207 if (key_table[key].allocated &&
208 (pthread->specific[key].seqno == key_table[key].seqno)) {
209 /* Return the value: */
210 data = (void *) pthread->specific[key].data;
213 * This key has not been used before, so return NULL
219 /* No specific data has been created, so just return NULL: */