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