xref: /freebsd/lib/libthr/thread/thr_spec.c (revision 40f65a4df509c6b29b9e72407c7afefe28838844)
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. Neither the name of the author nor the names of any co-contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 #include "namespace.h"
33 #include <signal.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <errno.h>
37 #include <pthread.h>
38 #include "un-namespace.h"
39 #include "libc_private.h"
40 
41 #include "thr_private.h"
42 
43 /* Static variables: */
44 struct pthread_key _thread_keytable[PTHREAD_KEYS_MAX];
45 
46 __weak_reference(_pthread_key_create, pthread_key_create);
47 __weak_reference(_pthread_key_delete, pthread_key_delete);
48 __weak_reference(_pthread_getspecific, pthread_getspecific);
49 __weak_reference(_pthread_setspecific, pthread_setspecific);
50 
51 
52 int
53 _pthread_key_create(pthread_key_t *key, void (*destructor) (void *))
54 {
55 	struct pthread *curthread;
56 	int i;
57 
58 	_thr_check_init();
59 
60 	curthread = _get_curthread();
61 
62 	/* Lock the key table: */
63 	THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
64 	for (i = 0; i < PTHREAD_KEYS_MAX; i++) {
65 
66 		if (_thread_keytable[i].allocated == 0) {
67 			_thread_keytable[i].allocated = 1;
68 			_thread_keytable[i].destructor = destructor;
69 			_thread_keytable[i].seqno++;
70 
71 			/* Unlock the key table: */
72 			THR_LOCK_RELEASE(curthread, &_keytable_lock);
73 			*key = i + 1;
74 			return (0);
75 		}
76 
77 	}
78 	/* Unlock the key table: */
79 	THR_LOCK_RELEASE(curthread, &_keytable_lock);
80 	return (EAGAIN);
81 }
82 
83 int
84 _pthread_key_delete(pthread_key_t userkey)
85 {
86 	struct pthread *curthread = _get_curthread();
87 	int key = userkey - 1;
88 	int ret = 0;
89 
90 	if ((unsigned int)key < PTHREAD_KEYS_MAX) {
91 		/* Lock the key table: */
92 		THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
93 
94 		if (_thread_keytable[key].allocated)
95 			_thread_keytable[key].allocated = 0;
96 		else
97 			ret = EINVAL;
98 
99 		/* Unlock the key table: */
100 		THR_LOCK_RELEASE(curthread, &_keytable_lock);
101 	} else
102 		ret = EINVAL;
103 	return (ret);
104 }
105 
106 void
107 _thread_cleanupspecific(void)
108 {
109 	struct pthread	*curthread = _get_curthread();
110 	void		(*destructor)( void *);
111 	const void	*data = NULL;
112 	int		key;
113 	int		i;
114 
115 	if (curthread->specific == NULL)
116 		return;
117 
118 	/* Lock the key table: */
119 	THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
120 	for (i = 0; (i < PTHREAD_DESTRUCTOR_ITERATIONS) &&
121 	    (curthread->specific_data_count > 0); i++) {
122 		for (key = 0; (key < PTHREAD_KEYS_MAX) &&
123 		    (curthread->specific_data_count > 0); key++) {
124 			destructor = NULL;
125 
126 			if (_thread_keytable[key].allocated &&
127 			    (curthread->specific[key].data != NULL)) {
128 				if (curthread->specific[key].seqno ==
129 				    _thread_keytable[key].seqno) {
130 					data = curthread->specific[key].data;
131 					destructor = _thread_keytable[key].destructor;
132 				}
133 				curthread->specific[key].data = NULL;
134 				curthread->specific_data_count--;
135 			}
136 			else if (curthread->specific[key].data != NULL) {
137 				/*
138 				 * This can happen if the key is deleted via
139 				 * pthread_key_delete without first setting the value
140 				 * to NULL in all threads.  POSIX says that the
141 				 * destructor is not invoked in this case.
142 				 */
143 				curthread->specific[key].data = NULL;
144 				curthread->specific_data_count--;
145 			}
146 
147 			/*
148 			 * If there is a destructor, call it
149 			 * with the key table entry unlocked:
150 			 */
151 			if (destructor != NULL) {
152 				/*
153 				 * Don't hold the lock while calling the
154 				 * destructor:
155 				 */
156 				THR_LOCK_RELEASE(curthread, &_keytable_lock);
157 				destructor(__DECONST(void *, data));
158 				THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
159 			}
160 		}
161 	}
162 	THR_LOCK_RELEASE(curthread, &_keytable_lock);
163 	free(curthread->specific);
164 	curthread->specific = NULL;
165 	if (curthread->specific_data_count > 0)
166 		stderr_debug("Thread %p has exited with leftover "
167 		    "thread-specific data after %d destructor iterations\n",
168 		    curthread, PTHREAD_DESTRUCTOR_ITERATIONS);
169 }
170 
171 static inline struct pthread_specific_elem *
172 pthread_key_allocate_data(void)
173 {
174 	struct pthread_specific_elem *new_data;
175 
176 	new_data = (struct pthread_specific_elem *)
177 	    calloc(1, sizeof(struct pthread_specific_elem) * PTHREAD_KEYS_MAX);
178 	return (new_data);
179 }
180 
181 int
182 _pthread_setspecific(pthread_key_t userkey, const void *value)
183 {
184 	struct pthread	*pthread;
185 	pthread_key_t	key = userkey - 1;
186 	int		ret = 0;
187 
188 	/* Point to the running thread: */
189 	pthread = _get_curthread();
190 
191 	if ((pthread->specific) ||
192 	    (pthread->specific = pthread_key_allocate_data())) {
193 		if ((unsigned int)key < PTHREAD_KEYS_MAX) {
194 			if (_thread_keytable[key].allocated) {
195 				if (pthread->specific[key].data == NULL) {
196 					if (value != NULL)
197 						pthread->specific_data_count++;
198 				} else if (value == NULL)
199 					pthread->specific_data_count--;
200 				pthread->specific[key].data = value;
201 				pthread->specific[key].seqno =
202 				    _thread_keytable[key].seqno;
203 				ret = 0;
204 			} else
205 				ret = EINVAL;
206 		} else
207 			ret = EINVAL;
208 	} else
209 		ret = ENOMEM;
210 	return (ret);
211 }
212 
213 void *
214 _pthread_getspecific(pthread_key_t userkey)
215 {
216 	struct pthread	*pthread;
217 	pthread_key_t	key = userkey - 1;
218 	const void	*data;
219 
220 	/* Point to the running thread: */
221 	pthread = _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 void
244 _thr_tsd_unload(struct dl_phdr_info *phdr_info)
245 {
246 	struct pthread *curthread = _get_curthread();
247 	void (*destructor)(void *);
248 	int key;
249 
250 	THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
251 	for (key = 0; key < PTHREAD_KEYS_MAX; key++) {
252 		if (_thread_keytable[key].allocated) {
253 			destructor = _thread_keytable[key].destructor;
254 			if (destructor != NULL) {
255 				if (__elf_phdr_match_addr(phdr_info, destructor))
256 					_thread_keytable[key].destructor = NULL;
257 			}
258 		}
259 	}
260 	THR_LOCK_RELEASE(curthread, &_keytable_lock);
261 }
262