1 /* 2 * Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by John Birrell. 16 * 4. Neither the name of the author nor the names of any co-contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * $FreeBSD$ 33 */ 34 #include <signal.h> 35 #include <stdlib.h> 36 #include <string.h> 37 #include <errno.h> 38 #include <pthread.h> 39 #include "thr_private.h" 40 41 struct pthread_key { 42 spinlock_t lock; 43 volatile int allocated; 44 volatile int count; 45 int seqno; 46 void (*destructor) (); 47 }; 48 49 /* Static variables: */ 50 static struct pthread_key key_table[PTHREAD_KEYS_MAX]; 51 52 __weak_reference(_pthread_key_create, pthread_key_create); 53 __weak_reference(_pthread_key_delete, pthread_key_delete); 54 __weak_reference(_pthread_getspecific, pthread_getspecific); 55 __weak_reference(_pthread_setspecific, pthread_setspecific); 56 57 58 int 59 _pthread_key_create(pthread_key_t * key, void (*destructor) (void *)) 60 { 61 for ((*key) = 0; (*key) < PTHREAD_KEYS_MAX; (*key)++) { 62 /* Lock the key table entry: */ 63 _SPINLOCK(&key_table[*key].lock); 64 65 if (key_table[(*key)].allocated == 0) { 66 key_table[(*key)].allocated = 1; 67 key_table[(*key)].destructor = destructor; 68 key_table[(*key)].seqno++; 69 70 /* Unlock the key table entry: */ 71 _SPINUNLOCK(&key_table[*key].lock); 72 return (0); 73 } 74 75 /* Unlock the key table entry: */ 76 _SPINUNLOCK(&key_table[*key].lock); 77 } 78 return (EAGAIN); 79 } 80 81 int 82 _pthread_key_delete(pthread_key_t key) 83 { 84 int ret = 0; 85 86 if (key < PTHREAD_KEYS_MAX) { 87 /* Lock the key table entry: */ 88 _SPINLOCK(&key_table[key].lock); 89 90 if (key_table[key].allocated) 91 key_table[key].allocated = 0; 92 else 93 ret = EINVAL; 94 95 /* Unlock the key table entry: */ 96 _SPINUNLOCK(&key_table[key].lock); 97 } else 98 ret = EINVAL; 99 return (ret); 100 } 101 102 void 103 _thread_cleanupspecific(void) 104 { 105 struct pthread *curthread = _get_curthread(); 106 void *data = NULL; 107 int key; 108 int itr; 109 void (*destructor)( void *); 110 111 for (itr = 0; itr < PTHREAD_DESTRUCTOR_ITERATIONS; itr++) { 112 for (key = 0; key < PTHREAD_KEYS_MAX; key++) { 113 if (curthread->specific_data_count > 0) { 114 /* Lock the key table entry: */ 115 _SPINLOCK(&key_table[key].lock); 116 destructor = NULL; 117 118 if (key_table[key].allocated && 119 (curthread->specific[key].data != NULL)) { 120 if (curthread->specific[key].seqno == 121 key_table[key].seqno) { 122 data = (void *) curthread->specific[key].data; 123 destructor = key_table[key].destructor; 124 } 125 curthread->specific[key].data = NULL; 126 curthread->specific_data_count--; 127 } 128 129 /* Unlock the key table entry: */ 130 _SPINUNLOCK(&key_table[key].lock); 131 132 /* 133 * If there is a destructore, call it 134 * with the key table entry unlocked: 135 */ 136 if (destructor) 137 destructor(data); 138 } else { 139 free(curthread->specific); 140 curthread->specific = NULL; 141 return; 142 } 143 } 144 } 145 if (curthread->specific != NULL) { 146 free(curthread->specific); 147 curthread->specific = NULL; 148 } 149 } 150 151 static inline struct pthread_specific_elem * 152 pthread_key_allocate_data(void) 153 { 154 struct pthread_specific_elem *new_data; 155 156 new_data = (struct pthread_specific_elem *) 157 malloc(sizeof(struct pthread_specific_elem) * PTHREAD_KEYS_MAX); 158 if (new_data != NULL) { 159 memset((void *) new_data, 0, 160 sizeof(struct pthread_specific_elem) * PTHREAD_KEYS_MAX); 161 } 162 return (new_data); 163 } 164 165 int 166 _pthread_setspecific(pthread_key_t key, const void *value) 167 { 168 struct pthread *pthread; 169 int ret = 0; 170 171 /* Point to the running thread: */ 172 pthread = _get_curthread(); 173 174 if ((pthread->specific) || 175 (pthread->specific = pthread_key_allocate_data())) { 176 if (key < PTHREAD_KEYS_MAX) { 177 if (key_table[key].allocated) { 178 if (pthread->specific[key].data == NULL) { 179 if (value != NULL) 180 pthread->specific_data_count++; 181 } else { 182 if (value == NULL) 183 pthread->specific_data_count--; 184 } 185 pthread->specific[key].data = value; 186 pthread->specific[key].seqno = 187 key_table[key].seqno; 188 ret = 0; 189 } else 190 ret = EINVAL; 191 } else 192 ret = EINVAL; 193 } else 194 ret = ENOMEM; 195 return (ret); 196 } 197 198 void * 199 _pthread_getspecific(pthread_key_t key) 200 { 201 struct pthread *pthread; 202 void *data; 203 204 /* Point to the running thread: */ 205 pthread = _get_curthread(); 206 207 /* Check if there is specific data: */ 208 if (pthread->specific != NULL && key < PTHREAD_KEYS_MAX) { 209 /* Check if this key has been used before: */ 210 if (key_table[key].allocated && 211 (pthread->specific[key].seqno == key_table[key].seqno)) { 212 /* Return the value: */ 213 data = (void *) pthread->specific[key].data; 214 } else { 215 /* 216 * This key has not been used before, so return NULL 217 * instead: 218 */ 219 data = NULL; 220 } 221 } else 222 /* No specific data has been created, so just return NULL: */ 223 data = NULL; 224 return (data); 225 } 226