xref: /freebsd/lib/libthr/thread/thr_spec.c (revision 7750ad47a9a7dbc83f87158464170c8640723293)
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;
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 key)
85 {
86 	struct pthread *curthread = _get_curthread();
87 	int ret = 0;
88 
89 	if ((unsigned int)key < PTHREAD_KEYS_MAX) {
90 		/* Lock the key table: */
91 		THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
92 
93 		if (_thread_keytable[key].allocated)
94 			_thread_keytable[key].allocated = 0;
95 		else
96 			ret = EINVAL;
97 
98 		/* Unlock the key table: */
99 		THR_LOCK_RELEASE(curthread, &_keytable_lock);
100 	} else
101 		ret = EINVAL;
102 	return (ret);
103 }
104 
105 void
106 _thread_cleanupspecific(void)
107 {
108 	struct pthread	*curthread = _get_curthread();
109 	void		(*destructor)( void *);
110 	const void	*data = NULL;
111 	int		key;
112 	int		i;
113 
114 	if (curthread->specific == NULL)
115 		return;
116 
117 	/* Lock the key table: */
118 	THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
119 	for (i = 0; (i < PTHREAD_DESTRUCTOR_ITERATIONS) &&
120 	    (curthread->specific_data_count > 0); i++) {
121 		for (key = 0; (key < PTHREAD_KEYS_MAX) &&
122 		    (curthread->specific_data_count > 0); key++) {
123 			destructor = NULL;
124 
125 			if (_thread_keytable[key].allocated &&
126 			    (curthread->specific[key].data != NULL)) {
127 				if (curthread->specific[key].seqno ==
128 				    _thread_keytable[key].seqno) {
129 					data = curthread->specific[key].data;
130 					destructor = _thread_keytable[key].destructor;
131 				}
132 				curthread->specific[key].data = NULL;
133 				curthread->specific_data_count--;
134 			}
135 			else if (curthread->specific[key].data != NULL) {
136 				/*
137 				 * This can happen if the key is deleted via
138 				 * pthread_key_delete without first setting the value
139 				 * to NULL in all threads.  POSIX says that the
140 				 * destructor is not invoked 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
148 			 * with the key table entry unlocked:
149 			 */
150 			if (destructor != NULL) {
151 				/*
152 				 * Don't hold the lock while calling the
153 				 * destructor:
154 				 */
155 				THR_LOCK_RELEASE(curthread, &_keytable_lock);
156 				destructor(__DECONST(void *, data));
157 				THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
158 			}
159 		}
160 	}
161 	THR_LOCK_RELEASE(curthread, &_keytable_lock);
162 	free(curthread->specific);
163 	curthread->specific = NULL;
164 	if (curthread->specific_data_count > 0)
165 		stderr_debug("Thread %p has exited with leftover "
166 		    "thread-specific data after %d destructor iterations\n",
167 		    curthread, PTHREAD_DESTRUCTOR_ITERATIONS);
168 }
169 
170 static inline struct pthread_specific_elem *
171 pthread_key_allocate_data(void)
172 {
173 	struct pthread_specific_elem *new_data;
174 
175 	new_data = (struct pthread_specific_elem *)
176 	    calloc(1, sizeof(struct pthread_specific_elem) * PTHREAD_KEYS_MAX);
177 	return (new_data);
178 }
179 
180 int
181 _pthread_setspecific(pthread_key_t key, const void *value)
182 {
183 	struct pthread	*pthread;
184 	int		ret = 0;
185 
186 	/* Point to the running thread: */
187 	pthread = _get_curthread();
188 
189 	if ((pthread->specific) ||
190 	    (pthread->specific = pthread_key_allocate_data())) {
191 		if ((unsigned int)key < PTHREAD_KEYS_MAX) {
192 			if (_thread_keytable[key].allocated) {
193 				if (pthread->specific[key].data == NULL) {
194 					if (value != NULL)
195 						pthread->specific_data_count++;
196 				} else if (value == NULL)
197 					pthread->specific_data_count--;
198 				pthread->specific[key].data = value;
199 				pthread->specific[key].seqno =
200 				    _thread_keytable[key].seqno;
201 				ret = 0;
202 			} else
203 				ret = EINVAL;
204 		} else
205 			ret = EINVAL;
206 	} else
207 		ret = ENOMEM;
208 	return (ret);
209 }
210 
211 void *
212 _pthread_getspecific(pthread_key_t key)
213 {
214 	struct pthread	*pthread;
215 	const void	*data;
216 
217 	/* Point to the running thread: */
218 	pthread = _get_curthread();
219 
220 	/* Check if there is specific data: */
221 	if (pthread->specific != NULL && (unsigned int)key < PTHREAD_KEYS_MAX) {
222 		/* Check if this key has been used before: */
223 		if (_thread_keytable[key].allocated &&
224 		    (pthread->specific[key].seqno == _thread_keytable[key].seqno)) {
225 			/* Return the value: */
226 			data = pthread->specific[key].data;
227 		} else {
228 			/*
229 			 * This key has not been used before, so return NULL
230 			 * instead:
231 			 */
232 			data = NULL;
233 		}
234 	} else
235 		/* No specific data has been created, so just return NULL: */
236 		data = NULL;
237 	return (__DECONST(void *, data));
238 }
239 
240 void
241 _thr_tsd_unload(struct dl_phdr_info *phdr_info)
242 {
243 	struct pthread *curthread = _get_curthread();
244 	void (*destructor)(void *);
245 	int key;
246 
247 	THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
248 	for (key = 0; key < PTHREAD_KEYS_MAX; key++) {
249 		if (_thread_keytable[key].allocated) {
250 			destructor = _thread_keytable[key].destructor;
251 			if (destructor != NULL) {
252 				if (__elf_phdr_match_addr(phdr_info, destructor))
253 					_thread_keytable[key].destructor = NULL;
254 			}
255 		}
256 	}
257 	THR_LOCK_RELEASE(curthread, &_keytable_lock);
258 }
259