xref: /freebsd/lib/libthr/thread/thr_spec.c (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
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 <sys/cdefs.h>
33 #include "namespace.h"
34 #include <sys/mman.h>
35 #include <signal.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <pthread.h>
40 #include "un-namespace.h"
41 #include "libc_private.h"
42 
43 #include "thr_private.h"
44 
45 /* Used in symbol lookup of libthread_db */
46 struct pthread_key _thread_keytable[PTHREAD_KEYS_MAX];
47 
48 __weak_reference(_thr_key_create, pthread_key_create);
49 __weak_reference(_thr_key_create, _pthread_key_create);
50 __weak_reference(_thr_key_delete, pthread_key_delete);
51 __weak_reference(_thr_key_delete, _pthread_key_delete);
52 __weak_reference(_thr_getspecific, pthread_getspecific);
53 __weak_reference(_thr_getspecific, _pthread_getspecific);
54 __weak_reference(_thr_setspecific, pthread_setspecific);
55 __weak_reference(_thr_setspecific, _pthread_setspecific);
56 
57 int
58 _thr_key_create(pthread_key_t *key, void (*destructor)(void *))
59 {
60 	struct pthread *curthread;
61 	int i;
62 
63 	_thr_check_init();
64 
65 	curthread = _get_curthread();
66 
67 	THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
68 	for (i = 0; i < PTHREAD_KEYS_MAX; i++) {
69 
70 		if (_thread_keytable[i].allocated == 0) {
71 			_thread_keytable[i].allocated = 1;
72 			_thread_keytable[i].destructor = destructor;
73 			_thread_keytable[i].seqno++;
74 
75 			THR_LOCK_RELEASE(curthread, &_keytable_lock);
76 			*key = i + 1;
77 			return (0);
78 		}
79 
80 	}
81 	THR_LOCK_RELEASE(curthread, &_keytable_lock);
82 	return (EAGAIN);
83 }
84 
85 int
86 _thr_key_delete(pthread_key_t userkey)
87 {
88 	struct pthread *curthread;
89 	int key, ret;
90 
91 	key = userkey - 1;
92 	if ((unsigned int)key >= PTHREAD_KEYS_MAX)
93 		return (EINVAL);
94 	curthread = _get_curthread();
95 	THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
96 	if (_thread_keytable[key].allocated) {
97 		_thread_keytable[key].allocated = 0;
98 		ret = 0;
99 	} else {
100 		ret = EINVAL;
101 	}
102 	THR_LOCK_RELEASE(curthread, &_keytable_lock);
103 	return (ret);
104 }
105 
106 void
107 _thread_cleanupspecific(void)
108 {
109 	struct pthread *curthread;
110 	void (*destructor)(void *);
111 	const void *data;
112 	int i, key;
113 
114 	curthread = _get_curthread();
115 	if (curthread->specific == NULL)
116 		return;
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 = curthread->specific[key].data;
129 					destructor = _thread_keytable[key].
130 					    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
137 				 * deleted via pthread_key_delete
138 				 * without first setting the value to
139 				 * NULL in all threads.  POSIX says
140 				 * that the destructor is not invoked
141 				 * 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 with the
149 			 * key table entry unlocked.
150 			 */
151 			if (destructor != NULL) {
152 				THR_LOCK_RELEASE(curthread, &_keytable_lock);
153 				destructor(__DECONST(void *, data));
154 				THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
155 			}
156 		}
157 	}
158 	THR_LOCK_RELEASE(curthread, &_keytable_lock);
159 	__thr_free(curthread->specific);
160 	curthread->specific = NULL;
161 	if (curthread->specific_data_count > 0) {
162 		stderr_debug("Thread %p has exited with leftover "
163 		    "thread-specific data after %d destructor iterations\n",
164 		    curthread, PTHREAD_DESTRUCTOR_ITERATIONS);
165 	}
166 }
167 
168 int
169 _thr_setspecific(pthread_key_t userkey, const void *value)
170 {
171 	struct pthread *pthread;
172 	void *tmp;
173 	pthread_key_t key;
174 
175 	key = userkey - 1;
176 	if ((unsigned int)key >= PTHREAD_KEYS_MAX ||
177 	    !_thread_keytable[key].allocated)
178 		return (EINVAL);
179 
180 	pthread = _get_curthread();
181 	if (pthread->specific == NULL) {
182 		tmp = __thr_calloc(PTHREAD_KEYS_MAX,
183 		    sizeof(struct pthread_specific_elem));
184 		if (tmp == NULL)
185 			return (ENOMEM);
186 		pthread->specific = tmp;
187 	}
188 	if (pthread->specific[key].data == NULL) {
189 		if (value != NULL)
190 			pthread->specific_data_count++;
191 	} else if (value == NULL)
192 		pthread->specific_data_count--;
193 	pthread->specific[key].data = value;
194 	pthread->specific[key].seqno = _thread_keytable[key].seqno;
195 	return (0);
196 }
197 
198 void *
199 _thr_getspecific(pthread_key_t userkey)
200 {
201 	struct pthread *pthread;
202 	const void *data;
203 	pthread_key_t key;
204 
205 	/* Check if there is specific data. */
206 	key = userkey - 1;
207 	if ((unsigned int)key >= PTHREAD_KEYS_MAX)
208 		return (NULL);
209 
210 	pthread = _get_curthread();
211 	/* Check if this key has been used before. */
212 	if (_thread_keytable[key].allocated && pthread->specific != NULL &&
213 	    pthread->specific[key].seqno == _thread_keytable[key].seqno) {
214 		/* Return the value: */
215 		data = pthread->specific[key].data;
216 	} else {
217 		/*
218 		 * This key has not been used before, so return NULL
219 		 * instead.
220 		 */
221 		data = NULL;
222 	}
223 	return (__DECONST(void *, data));
224 }
225 
226 void
227 _thr_tsd_unload(struct dl_phdr_info *phdr_info)
228 {
229 	struct pthread *curthread;
230 	void (*destructor)(void *);
231 	int key;
232 
233 	curthread = _get_curthread();
234 	THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
235 	for (key = 0; key < PTHREAD_KEYS_MAX; key++) {
236 		if (!_thread_keytable[key].allocated)
237 			continue;
238 		destructor = _thread_keytable[key].destructor;
239 		if (destructor == NULL)
240 			continue;
241 		if (__elf_phdr_match_addr(phdr_info, destructor))
242 			_thread_keytable[key].destructor = NULL;
243 	}
244 	THR_LOCK_RELEASE(curthread, &_keytable_lock);
245 }
246