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