xref: /freebsd/lib/libthr/thread/thr_spec.c (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the author nor the names of any co-contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include "namespace.h"
33 #include <sys/mman.h>
34 #include <signal.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <errno.h>
38 #include <pthread.h>
39 #include "un-namespace.h"
40 #include "libc_private.h"
41 
42 #include "thr_private.h"
43 
44 /* Used in symbol lookup of libthread_db */
45 struct pthread_key _thread_keytable[PTHREAD_KEYS_MAX];
46 
47 __weak_reference(_thr_key_create, pthread_key_create);
48 __weak_reference(_thr_key_create, _pthread_key_create);
49 __weak_reference(_thr_key_delete, pthread_key_delete);
50 __weak_reference(_thr_key_delete, _pthread_key_delete);
51 __weak_reference(_thr_getspecific, pthread_getspecific);
52 __weak_reference(_thr_getspecific, _pthread_getspecific);
53 __weak_reference(_thr_setspecific, pthread_setspecific);
54 __weak_reference(_thr_setspecific, _pthread_setspecific);
55 
56 int
57 _thr_key_create(pthread_key_t *key, void (*destructor)(void *))
58 {
59 	struct pthread *curthread;
60 	int i;
61 
62 	_thr_check_init();
63 
64 	curthread = _get_curthread();
65 
66 	THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
67 	for (i = 0; i < PTHREAD_KEYS_MAX; i++) {
68 
69 		if (_thread_keytable[i].allocated == 0) {
70 			_thread_keytable[i].allocated = 1;
71 			_thread_keytable[i].destructor = destructor;
72 			_thread_keytable[i].seqno++;
73 
74 			THR_LOCK_RELEASE(curthread, &_keytable_lock);
75 			*key = i + 1;
76 			return (0);
77 		}
78 
79 	}
80 	THR_LOCK_RELEASE(curthread, &_keytable_lock);
81 	return (EAGAIN);
82 }
83 
84 int
85 _thr_key_delete(pthread_key_t userkey)
86 {
87 	struct pthread *curthread;
88 	int key, ret;
89 
90 	key = userkey - 1;
91 	if ((unsigned int)key >= PTHREAD_KEYS_MAX)
92 		return (EINVAL);
93 	curthread = _get_curthread();
94 	THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
95 	if (_thread_keytable[key].allocated) {
96 		_thread_keytable[key].allocated = 0;
97 		ret = 0;
98 	} else {
99 		ret = EINVAL;
100 	}
101 	THR_LOCK_RELEASE(curthread, &_keytable_lock);
102 	return (ret);
103 }
104 
105 void
106 _thread_cleanupspecific(void)
107 {
108 	struct pthread *curthread;
109 	void (*destructor)(void *);
110 	const void *data;
111 	int i, key;
112 
113 	curthread = _get_curthread();
114 	if (curthread->specific == NULL)
115 		return;
116 	THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
117 	for (i = 0; i < PTHREAD_DESTRUCTOR_ITERATIONS &&
118 	    curthread->specific_data_count > 0; i++) {
119 		for (key = 0; key < PTHREAD_KEYS_MAX &&
120 		    curthread->specific_data_count > 0; key++) {
121 			destructor = NULL;
122 
123 			if (_thread_keytable[key].allocated &&
124 			    (curthread->specific[key].data != NULL)) {
125 				if (curthread->specific[key].seqno ==
126 				    _thread_keytable[key].seqno) {
127 					data = curthread->specific[key].data;
128 					destructor = _thread_keytable[key].
129 					    destructor;
130 				}
131 				curthread->specific[key].data = NULL;
132 				curthread->specific_data_count--;
133 			} else if (curthread->specific[key].data != NULL) {
134 				/*
135 				 * This can happen if the key is
136 				 * deleted via pthread_key_delete
137 				 * without first setting the value to
138 				 * NULL in all threads.  POSIX says
139 				 * that the destructor is not invoked
140 				 * in this case.
141 				 */
142 				curthread->specific[key].data = NULL;
143 				curthread->specific_data_count--;
144 			}
145 
146 			/*
147 			 * If there is a destructor, call it with the
148 			 * key table entry unlocked.
149 			 */
150 			if (destructor != NULL) {
151 				THR_LOCK_RELEASE(curthread, &_keytable_lock);
152 				destructor(__DECONST(void *, data));
153 				THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
154 			}
155 		}
156 	}
157 	THR_LOCK_RELEASE(curthread, &_keytable_lock);
158 	__thr_free(curthread->specific);
159 	curthread->specific = NULL;
160 	if (curthread->specific_data_count > 0) {
161 		stderr_debug("Thread %p has exited with leftover "
162 		    "thread-specific data after %d destructor iterations\n",
163 		    curthread, PTHREAD_DESTRUCTOR_ITERATIONS);
164 	}
165 }
166 
167 int
168 _thr_setspecific(pthread_key_t userkey, const void *value)
169 {
170 	struct pthread *pthread;
171 	void *tmp;
172 	pthread_key_t key;
173 
174 	key = userkey - 1;
175 	if ((unsigned int)key >= PTHREAD_KEYS_MAX ||
176 	    !_thread_keytable[key].allocated)
177 		return (EINVAL);
178 
179 	pthread = _get_curthread();
180 	if (pthread->specific == NULL) {
181 		tmp = __thr_calloc(PTHREAD_KEYS_MAX,
182 		    sizeof(struct pthread_specific_elem));
183 		if (tmp == NULL)
184 			return (ENOMEM);
185 		pthread->specific = tmp;
186 	}
187 	if (pthread->specific[key].data == NULL) {
188 		if (value != NULL)
189 			pthread->specific_data_count++;
190 	} else if (value == NULL)
191 		pthread->specific_data_count--;
192 	pthread->specific[key].data = value;
193 	pthread->specific[key].seqno = _thread_keytable[key].seqno;
194 	return (0);
195 }
196 
197 void *
198 _thr_getspecific(pthread_key_t userkey)
199 {
200 	struct pthread *pthread;
201 	const void *data;
202 	pthread_key_t key;
203 
204 	/* Check if there is specific data. */
205 	key = userkey - 1;
206 	if ((unsigned int)key >= PTHREAD_KEYS_MAX)
207 		return (NULL);
208 
209 	pthread = _get_curthread();
210 	/* Check if this key has been used before. */
211 	if (_thread_keytable[key].allocated && pthread->specific != NULL &&
212 	    pthread->specific[key].seqno == _thread_keytable[key].seqno) {
213 		/* Return the value: */
214 		data = pthread->specific[key].data;
215 	} else {
216 		/*
217 		 * This key has not been used before, so return NULL
218 		 * instead.
219 		 */
220 		data = NULL;
221 	}
222 	return (__DECONST(void *, data));
223 }
224 
225 void
226 _thr_tsd_unload(struct dl_phdr_info *phdr_info)
227 {
228 	struct pthread *curthread;
229 	void (*destructor)(void *);
230 	int key;
231 
232 	curthread = _get_curthread();
233 	THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
234 	for (key = 0; key < PTHREAD_KEYS_MAX; key++) {
235 		if (!_thread_keytable[key].allocated)
236 			continue;
237 		destructor = _thread_keytable[key].destructor;
238 		if (destructor == NULL)
239 			continue;
240 		if (__elf_phdr_match_addr(phdr_info, destructor))
241 			_thread_keytable[key].destructor = NULL;
242 	}
243 	THR_LOCK_RELEASE(curthread, &_keytable_lock);
244 }
245