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