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  * $DragonFly: src/lib/libthread_xu/thread/thr_spec.c,v 1.5 2006/04/06 13:03:09 davidxu Exp $
33  */
34 
35 #include "namespace.h"
36 #include <machine/tls.h>
37 
38 #include <signal.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <errno.h>
42 #include <pthread.h>
43 #include "un-namespace.h"
44 
45 #include "thr_private.h"
46 
47 struct pthread_key _thread_keytable[PTHREAD_KEYS_MAX];
48 umtx_t	_keytable_lock;
49 
50 int
51 _pthread_key_create(pthread_key_t *key, void (*destructor) (void *))
52 {
53 	struct pthread *curthread = tls_get_curthread();
54 	int i;
55 
56 	/* Lock the key table: */
57 	THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
58 	for (i = 0; i < PTHREAD_KEYS_MAX; i++) {
59 
60 		if (_thread_keytable[i].allocated == 0) {
61 			_thread_keytable[i].allocated = 1;
62 			_thread_keytable[i].destructor = destructor;
63 			_thread_keytable[i].seqno++;
64 
65 			/* Unlock the key table: */
66 			THR_LOCK_RELEASE(curthread, &_keytable_lock);
67 			*key = i;
68 			return (0);
69 		}
70 
71 	}
72 	/* Unlock the key table: */
73 	THR_LOCK_RELEASE(curthread, &_keytable_lock);
74 	return (EAGAIN);
75 }
76 
77 int
78 _pthread_key_delete(pthread_key_t key)
79 {
80 	struct pthread *curthread = tls_get_curthread();
81 	int ret = 0;
82 
83 	if ((unsigned int)key < PTHREAD_KEYS_MAX) {
84 		/* Lock the key table: */
85 		THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
86 
87 		if (_thread_keytable[key].allocated)
88 			_thread_keytable[key].allocated = 0;
89 		else
90 			ret = EINVAL;
91 
92 		/* Unlock the key table: */
93 		THR_LOCK_RELEASE(curthread, &_keytable_lock);
94 	} else
95 		ret = EINVAL;
96 	return (ret);
97 }
98 
99 void
100 _thread_cleanupspecific(void)
101 {
102 	struct pthread	*curthread = tls_get_curthread();
103 	void		(*destructor)( void *);
104 	const void	*data = NULL;
105 	int		key;
106 	int		i;
107 
108 	if (curthread->specific == NULL)
109 		return;
110 
111 	/* Lock the key table: */
112 	THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
113 	for (i = 0; (i < PTHREAD_DESTRUCTOR_ITERATIONS) &&
114 	    (curthread->specific_data_count > 0); i++) {
115 		for (key = 0; (key < PTHREAD_KEYS_MAX) &&
116 		    (curthread->specific_data_count > 0); key++) {
117 			destructor = NULL;
118 
119 			if (_thread_keytable[key].allocated &&
120 			    (curthread->specific[key].data != NULL)) {
121 				if (curthread->specific[key].seqno ==
122 				    _thread_keytable[key].seqno) {
123 					data =
124 					    curthread->specific[key].data;
125 					destructor = _thread_keytable[key].destructor;
126 				}
127 				curthread->specific[key].data = NULL;
128 				curthread->specific_data_count--;
129 			} else if (curthread->specific[key].data != NULL) {
130 				/*
131 				 * This can happen if the key is deleted via
132 				 * pthread_key_delete without first setting the value
133 	 			 * to NULL in all threads. POSIX says that the
134 	 			 * destructor is not invoked in this case.
135 				 */
136 				curthread->specific[key].data = NULL;
137 				curthread->specific_data_count--;
138 			}
139 
140 			/*
141 			 * If there is a destructor, call it
142 			 * with the key table entry unlocked:
143 			 */
144 			if (destructor != NULL) {
145 				/*
146 				 * Don't hold the lock while calling the
147 				 * destructor:
148 				 */
149 				THR_LOCK_RELEASE(curthread, &_keytable_lock);
150 				destructor(__DECONST(void *, data));
151 				THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
152 			}
153 		}
154 	}
155 	THR_LOCK_RELEASE(curthread, &_keytable_lock);
156 	free(curthread->specific);
157 	curthread->specific = NULL;
158 	if (curthread->specific_data_count > 0)
159 		stderr_debug("Thread %p has exited with leftover "
160 		    "thread-specific data after %d destructor iterations\n",
161 		    curthread, PTHREAD_DESTRUCTOR_ITERATIONS);
162 }
163 
164 static inline struct pthread_specific_elem *
165 pthread_key_allocate_data(void)
166 {
167 	struct pthread_specific_elem *new_data;
168 
169 	new_data = (struct pthread_specific_elem *)
170 	    malloc(sizeof(struct pthread_specific_elem) * PTHREAD_KEYS_MAX);
171 	if (new_data != NULL) {
172 		memset((void *) new_data, 0,
173 		    sizeof(struct pthread_specific_elem) * PTHREAD_KEYS_MAX);
174 	}
175 	return (new_data);
176 }
177 
178 int
179 _pthread_setspecific(pthread_key_t key, const void *value)
180 {
181 	struct pthread	*pthread;
182 	int		ret = 0;
183 
184 	/* Point to the running thread: */
185 	pthread = tls_get_curthread();
186 
187 	if ((pthread->specific) ||
188 	    (pthread->specific = pthread_key_allocate_data())) {
189 		if ((unsigned int)key < PTHREAD_KEYS_MAX) {
190 			if (_thread_keytable[key].allocated) {
191 				if (pthread->specific[key].data == NULL) {
192 					if (value != NULL)
193 						pthread->specific_data_count++;
194 				} else if (value == NULL)
195 					pthread->specific_data_count--;
196 				pthread->specific[key].data = value;
197 				pthread->specific[key].seqno =
198 				    _thread_keytable[key].seqno;
199 				ret = 0;
200 			} else
201 				ret = EINVAL;
202 		} else
203 			ret = EINVAL;
204 	} else
205 		ret = ENOMEM;
206 	return (ret);
207 }
208 
209 void *
210 _pthread_getspecific(pthread_key_t key)
211 {
212 	struct pthread	*pthread;
213 	const void	*data;
214 
215 	/* Point to the running thread: */
216 	pthread = tls_get_curthread();
217 
218 	/* Check if there is specific data: */
219 	if (pthread->specific != NULL && (unsigned int)key < PTHREAD_KEYS_MAX) {
220 		/* Check if this key has been used before: */
221 		if (_thread_keytable[key].allocated &&
222 		    (pthread->specific[key].seqno == _thread_keytable[key].seqno)) {
223 			/* Return the value: */
224 			data = pthread->specific[key].data;
225 		} else {
226 			/*
227 			 * This key has not been used before, so return NULL
228 			 * instead:
229 			 */
230 			data = NULL;
231 		}
232 	} else
233 		/* No specific data has been created, so just return NULL: */
234 		data = NULL;
235 	return __DECONST(void *, data);
236 }
237 
238 __strong_reference(_pthread_key_create, pthread_key_create);
239 __strong_reference(_pthread_key_delete, pthread_key_delete);
240 __strong_reference(_pthread_getspecific, pthread_getspecific);
241 __strong_reference(_pthread_setspecific, pthread_setspecific);
242 
243