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 
45 int
46 _pthread_key_create(pthread_key_t *key, void (*destructor) (void *))
47 {
48 	struct pthread *curthread;
49 	int i;
50 
51 	/* User program might be preparing to call pthread_create() */
52 	_thr_check_init();
53 
54 	curthread = tls_get_curthread();
55 
56 	/* Lock the key table: */
57 	THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
58 	for (i = 0; i < PTHREAD_KEYS_MAX; i++) {
59 
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
78 _pthread_key_delete(pthread_key_t key)
79 {
80 	struct pthread *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
100 _thread_cleanupspecific(void)
101 {
102 	struct pthread	*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 	free(curthread->specific);
156 	curthread->specific = NULL;
157 	if (curthread->specific_data_count > 0)
158 		stderr_debug("Thread %p has exited with leftover "
159 		    "thread-specific data after %d destructor iterations\n",
160 		    curthread, PTHREAD_DESTRUCTOR_ITERATIONS);
161 }
162 
163 static inline struct pthread_specific_elem *
164 pthread_key_allocate_data(void)
165 {
166 	struct pthread_specific_elem *new_data;
167 
168 	new_data = (struct pthread_specific_elem *)
169 	    malloc(sizeof(struct pthread_specific_elem) * PTHREAD_KEYS_MAX);
170 	if (new_data != NULL) {
171 		memset((void *) new_data, 0,
172 		    sizeof(struct pthread_specific_elem) * PTHREAD_KEYS_MAX);
173 	}
174 	return (new_data);
175 }
176 
177 int
178 _pthread_setspecific(pthread_key_t key, const void *value)
179 {
180 	struct pthread	*pthread;
181 	int		ret = 0;
182 
183 	/* Point to the running thread: */
184 	pthread = tls_get_curthread();
185 
186 	if ((pthread->specific) ||
187 	    (pthread->specific = pthread_key_allocate_data())) {
188 		if ((unsigned int)key < PTHREAD_KEYS_MAX) {
189 			if (_thread_keytable[key].allocated) {
190 				if (pthread->specific[key].data == NULL) {
191 					if (value != NULL)
192 						pthread->specific_data_count++;
193 				} else if (value == NULL)
194 					pthread->specific_data_count--;
195 				pthread->specific[key].data = value;
196 				pthread->specific[key].seqno =
197 				    _thread_keytable[key].seqno;
198 				ret = 0;
199 			} else
200 				ret = EINVAL;
201 		} else
202 			ret = EINVAL;
203 	} else
204 		ret = ENOMEM;
205 	return (ret);
206 }
207 
208 void *
209 _pthread_getspecific(pthread_key_t key)
210 {
211 	struct pthread	*pthread;
212 	const void	*data;
213 
214 	/* Point to the running thread: */
215 	pthread = tls_get_curthread();
216 
217 	/* Check if there is specific data: */
218 	if (pthread->specific != NULL && (unsigned int)key < PTHREAD_KEYS_MAX) {
219 		/* Check if this key has been used before: */
220 		if (_thread_keytable[key].allocated &&
221 		    (pthread->specific[key].seqno == _thread_keytable[key].seqno)) {
222 			/* Return the value: */
223 			data = pthread->specific[key].data;
224 		} else {
225 			/*
226 			 * This key has not been used before, so return NULL
227 			 * instead.
228 			 */
229 			data = NULL;
230 		}
231 	} else
232 		/* No specific data has been created, so just return NULL: */
233 		data = NULL;
234 	return __DECONST(void *, data);
235 }
236 
237 __strong_reference(_pthread_key_create, pthread_key_create);
238 __strong_reference(_pthread_key_delete, pthread_key_delete);
239 __strong_reference(_pthread_getspecific, pthread_getspecific);
240 __strong_reference(_pthread_setspecific, pthread_setspecific);
241