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