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