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. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by John Birrell.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $FreeBSD: src/lib/libpthread/thread/thr_spec.c,v 1.22 2004/07/13 22:49:58 davidxu Exp $
33  * $DragonFly: src/lib/libthread_xu/thread/thr_spec.c,v 1.4 2006/04/05 12:12:23 davidxu Exp $
34  */
35 
36 #include <machine/tls.h>
37 
38 #include <signal.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <errno.h>
42 #include <pthread.h>
43 #include "thr_private.h"
44 
45 struct pthread_key _thread_keytable[PTHREAD_KEYS_MAX];
46 umtx_t	_keytable_lock;
47 
48 int
49 _pthread_key_create(pthread_key_t *key, void (*destructor) (void *))
50 {
51 	struct pthread *curthread = tls_get_curthread();
52 	int i;
53 
54 	/* Lock the key table: */
55 	THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
56 	for (i = 0; i < PTHREAD_KEYS_MAX; i++) {
57 
58 		if (_thread_keytable[i].allocated == 0) {
59 			_thread_keytable[i].allocated = 1;
60 			_thread_keytable[i].destructor = destructor;
61 			_thread_keytable[i].seqno++;
62 
63 			/* Unlock the key table: */
64 			THR_LOCK_RELEASE(curthread, &_keytable_lock);
65 			*key = i;
66 			return (0);
67 		}
68 
69 	}
70 	/* Unlock the key table: */
71 	THR_LOCK_RELEASE(curthread, &_keytable_lock);
72 	return (EAGAIN);
73 }
74 
75 int
76 _pthread_key_delete(pthread_key_t key)
77 {
78 	struct pthread *curthread = tls_get_curthread();
79 	int ret = 0;
80 
81 	if ((unsigned int)key < PTHREAD_KEYS_MAX) {
82 		/* Lock the key table: */
83 		THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
84 
85 		if (_thread_keytable[key].allocated)
86 			_thread_keytable[key].allocated = 0;
87 		else
88 			ret = EINVAL;
89 
90 		/* Unlock the key table: */
91 		THR_LOCK_RELEASE(curthread, &_keytable_lock);
92 	} else
93 		ret = EINVAL;
94 	return (ret);
95 }
96 
97 void
98 _thread_cleanupspecific(void)
99 {
100 	struct pthread	*curthread = tls_get_curthread();
101 	void		(*destructor)( void *);
102 	void		*data = NULL;
103 	int		key;
104 	int		i;
105 
106 	if (curthread->specific == NULL)
107 		return;
108 
109 	/* Lock the key table: */
110 	THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
111 	for (i = 0; (i < PTHREAD_DESTRUCTOR_ITERATIONS) &&
112 	    (curthread->specific_data_count > 0); i++) {
113 		for (key = 0; (key < PTHREAD_KEYS_MAX) &&
114 		    (curthread->specific_data_count > 0); key++) {
115 			destructor = NULL;
116 
117 			if (_thread_keytable[key].allocated &&
118 			    (curthread->specific[key].data != NULL)) {
119 				if (curthread->specific[key].seqno ==
120 				    _thread_keytable[key].seqno) {
121 					data = (void *)
122 					    curthread->specific[key].data;
123 					destructor = _thread_keytable[key].destructor;
124 				}
125 				curthread->specific[key].data = NULL;
126 				curthread->specific_data_count--;
127 			}
128 
129 			/*
130 			 * If there is a destructore, call it
131 			 * with the key table entry unlocked:
132 			 */
133 			if (destructor != NULL) {
134 				/*
135 				 * Don't hold the lock while calling the
136 				 * destructor:
137 				 */
138 				THR_LOCK_RELEASE(curthread, &_keytable_lock);
139 				destructor(data);
140 				THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
141 			}
142 		}
143 	}
144 	THR_LOCK_RELEASE(curthread, &_keytable_lock);
145 	free(curthread->specific);
146 	curthread->specific = NULL;
147 	if (curthread->specific_data_count > 0)
148 		stderr_debug("Thread %p has exited with leftover "
149 		    "thread-specific data after %d destructor iterations\n",
150 		    curthread, PTHREAD_DESTRUCTOR_ITERATIONS);
151 }
152 
153 static inline struct pthread_specific_elem *
154 pthread_key_allocate_data(void)
155 {
156 	struct pthread_specific_elem *new_data;
157 
158 	new_data = (struct pthread_specific_elem *)
159 	    malloc(sizeof(struct pthread_specific_elem) * PTHREAD_KEYS_MAX);
160 	if (new_data != NULL) {
161 		memset((void *) new_data, 0,
162 		    sizeof(struct pthread_specific_elem) * PTHREAD_KEYS_MAX);
163 	}
164 	return (new_data);
165 }
166 
167 int
168 _pthread_setspecific(pthread_key_t key, const void *value)
169 {
170 	struct pthread	*pthread;
171 	int		ret = 0;
172 
173 	/* Point to the running thread: */
174 	pthread = tls_get_curthread();
175 
176 	if ((pthread->specific) ||
177 	    (pthread->specific = pthread_key_allocate_data())) {
178 		if ((unsigned int)key < PTHREAD_KEYS_MAX) {
179 			if (_thread_keytable[key].allocated) {
180 				if (pthread->specific[key].data == NULL) {
181 					if (value != NULL)
182 						pthread->specific_data_count++;
183 				} else if (value == NULL)
184 					pthread->specific_data_count--;
185 				pthread->specific[key].data = value;
186 				pthread->specific[key].seqno =
187 				    _thread_keytable[key].seqno;
188 				ret = 0;
189 			} else
190 				ret = EINVAL;
191 		} else
192 			ret = EINVAL;
193 	} else
194 		ret = ENOMEM;
195 	return (ret);
196 }
197 
198 void *
199 _pthread_getspecific(pthread_key_t key)
200 {
201 	struct pthread	*pthread;
202 	void		*data;
203 
204 	/* Point to the running thread: */
205 	pthread = tls_get_curthread();
206 
207 	/* Check if there is specific data: */
208 	if (pthread->specific != NULL && (unsigned int)key < PTHREAD_KEYS_MAX) {
209 		/* Check if this key has been used before: */
210 		if (_thread_keytable[key].allocated &&
211 		    (pthread->specific[key].seqno == _thread_keytable[key].seqno)) {
212 			/* Return the value: */
213 			data = (void *) pthread->specific[key].data;
214 		} else {
215 			/*
216 			 * This key has not been used before, so return NULL
217 			 * instead:
218 			 */
219 			data = NULL;
220 		}
221 	} else
222 		/* No specific data has been created, so just return NULL: */
223 		data = NULL;
224 	return (data);
225 }
226 
227 __strong_reference(_pthread_key_create, pthread_key_create);
228 __strong_reference(_pthread_key_delete, pthread_key_delete);
229 __strong_reference(_pthread_getspecific, pthread_getspecific);
230 __strong_reference(_pthread_setspecific, pthread_setspecific);
231 
232