thr_spec.c revision 36697
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 REGENTS 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 */ 33#include <signal.h> 34#include <stdlib.h> 35#include <string.h> 36#include <errno.h> 37#ifdef _THREAD_SAFE 38#include <pthread.h> 39#include "pthread_private.h" 40 41/* Static variables: */ 42static struct pthread_key key_table[PTHREAD_KEYS_MAX]; 43 44int 45pthread_key_create(pthread_key_t * key, void (*destructor) (void *)) 46{ 47 for ((*key) = 0; (*key) < PTHREAD_KEYS_MAX; (*key)++) { 48 /* Lock the key table entry: */ 49 _spinlock(&key_table[*key].access_lock); 50 51 if (key_table[(*key)].allocated == 0) { 52 key_table[(*key)].allocated = 1; 53 key_table[(*key)].destructor = destructor; 54 55 /* Unlock the key table entry: */ 56 _atomic_unlock(&key_table[*key].access_lock); 57 return (0); 58 } 59 60 /* Unlock the key table entry: */ 61 _atomic_unlock(&key_table[*key].access_lock); 62 } 63 return (EAGAIN); 64} 65 66int 67pthread_key_delete(pthread_key_t key) 68{ 69 int ret = 0; 70 71 if (key < PTHREAD_KEYS_MAX) { 72 /* Lock the key table entry: */ 73 _spinlock(&key_table[key].access_lock); 74 75 if (key_table[key].allocated) 76 key_table[key].allocated = 0; 77 else 78 ret = EINVAL; 79 80 /* Unlock the key table entry: */ 81 _atomic_unlock(&key_table[key].access_lock); 82 } else 83 ret = EINVAL; 84 return (ret); 85} 86 87void 88_thread_cleanupspecific(void) 89{ 90 void *data; 91 int key; 92 int itr; 93 94 for (itr = 0; itr < PTHREAD_DESTRUCTOR_ITERATIONS; itr++) { 95 for (key = 0; key < PTHREAD_KEYS_MAX; key++) { 96 if (_thread_run->specific_data_count) { 97 /* Lock the key table entry: */ 98 _spinlock(&key_table[key].access_lock); 99 100 if (key_table[key].allocated) { 101 if (_thread_run->specific_data[key]) { 102 data = (void *) _thread_run->specific_data[key]; 103 _thread_run->specific_data[key] = NULL; 104 _thread_run->specific_data_count--; 105 if (key_table[key].destructor) 106 key_table[key].destructor(data); 107 } 108 } 109 110 /* Unlock the key table entry: */ 111 _atomic_unlock(&key_table[key].access_lock); 112 } else { 113 free(_thread_run->specific_data); 114 return; 115 } 116 } 117 } 118 free(_thread_run->specific_data); 119} 120 121static inline const void ** 122pthread_key_allocate_data(void) 123{ 124 const void **new_data; 125 if ((new_data = (const void **) malloc(sizeof(void *) * PTHREAD_KEYS_MAX)) != NULL) { 126 memset((void *) new_data, 0, sizeof(void *) * PTHREAD_KEYS_MAX); 127 } 128 return (new_data); 129} 130 131int 132pthread_setspecific(pthread_key_t key, const void *value) 133{ 134 pthread_t pthread; 135 int ret = 0; 136 137 /* Point to the running thread: */ 138 pthread = _thread_run; 139 140 if ((pthread->specific_data) || 141 (pthread->specific_data = pthread_key_allocate_data())) { 142 if (key < PTHREAD_KEYS_MAX) { 143 /* Lock the key table entry: */ 144 _spinlock(&key_table[key].access_lock); 145 146 if (key_table[key].allocated) { 147 if (pthread->specific_data[key] == NULL) { 148 if (value != NULL) 149 pthread->specific_data_count++; 150 } else { 151 if (value == NULL) 152 pthread->specific_data_count--; 153 } 154 pthread->specific_data[key] = value; 155 ret = 0; 156 } else 157 ret = EINVAL; 158 159 /* Unlock the key table entry: */ 160 _atomic_unlock(&key_table[key].access_lock); 161 162 } else 163 ret = EINVAL; 164 } else 165 ret = ENOMEM; 166 return (ret); 167} 168 169void * 170pthread_getspecific(pthread_key_t key) 171{ 172 pthread_t pthread; 173 void *data; 174 175 /* Point to the running thread: */ 176 pthread = _thread_run; 177 178 /* Check if there is specific data: */ 179 if (pthread->specific_data != NULL && key < PTHREAD_KEYS_MAX) { 180 /* Lock the key table entry: */ 181 _spinlock(&key_table[key].access_lock); 182 183 /* Check if this key has been used before: */ 184 if (key_table[key].allocated) { 185 /* Return the value: */ 186 data = (void *) pthread->specific_data[key]; 187 } else { 188 /* 189 * This key has not been used before, so return NULL 190 * instead: 191 */ 192 data = NULL; 193 } 194 195 /* Unlock the key table entry: */ 196 _atomic_unlock(&key_table[key].access_lock); 197 } else 198 /* No specific data has been created, so just return NULL: */ 199 data = NULL; 200 return (data); 201} 202#endif 203