xref: /freebsd/lib/libthr/thread/thr_spec.c (revision d056fa046c6a91b90cd98165face0e42a33a5173)
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$
33  */
34 
35 #include "namespace.h"
36 #include <signal.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <errno.h>
40 #include <pthread.h>
41 #include "un-namespace.h"
42 
43 #include "thr_private.h"
44 
45 /* Static variables: */
46 struct pthread_key _thread_keytable[PTHREAD_KEYS_MAX];
47 
48 __weak_reference(_pthread_key_create, pthread_key_create);
49 __weak_reference(_pthread_key_delete, pthread_key_delete);
50 __weak_reference(_pthread_getspecific, pthread_getspecific);
51 __weak_reference(_pthread_setspecific, pthread_setspecific);
52 
53 
54 int
55 _pthread_key_create(pthread_key_t *key, void (*destructor) (void *))
56 {
57 	struct pthread *curthread = _get_curthread();
58 	int i;
59 
60 	/* Lock the key table: */
61 	THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
62 	for (i = 0; i < PTHREAD_KEYS_MAX; i++) {
63 
64 		if (_thread_keytable[i].allocated == 0) {
65 			_thread_keytable[i].allocated = 1;
66 			_thread_keytable[i].destructor = destructor;
67 			_thread_keytable[i].seqno++;
68 
69 			/* Unlock the key table: */
70 			THR_LOCK_RELEASE(curthread, &_keytable_lock);
71 			*key = i;
72 			return (0);
73 		}
74 
75 	}
76 	/* Unlock the key table: */
77 	THR_LOCK_RELEASE(curthread, &_keytable_lock);
78 	return (EAGAIN);
79 }
80 
81 int
82 _pthread_key_delete(pthread_key_t key)
83 {
84 	struct pthread *curthread = _get_curthread();
85 	int ret = 0;
86 
87 	if ((unsigned int)key < PTHREAD_KEYS_MAX) {
88 		/* Lock the key table: */
89 		THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
90 
91 		if (_thread_keytable[key].allocated)
92 			_thread_keytable[key].allocated = 0;
93 		else
94 			ret = EINVAL;
95 
96 		/* Unlock the key table: */
97 		THR_LOCK_RELEASE(curthread, &_keytable_lock);
98 	} else
99 		ret = EINVAL;
100 	return (ret);
101 }
102 
103 void
104 _thread_cleanupspecific(void)
105 {
106 	struct pthread	*curthread = _get_curthread();
107 	void		(*destructor)( void *);
108 	const void	*data = NULL;
109 	int		key;
110 	int		i;
111 
112 	if (curthread->specific == NULL)
113 		return;
114 
115 	/* Lock the key table: */
116 	THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
117 	for (i = 0; (i < PTHREAD_DESTRUCTOR_ITERATIONS) &&
118 	    (curthread->specific_data_count > 0); i++) {
119 		for (key = 0; (key < PTHREAD_KEYS_MAX) &&
120 		    (curthread->specific_data_count > 0); key++) {
121 			destructor = NULL;
122 
123 			if (_thread_keytable[key].allocated &&
124 			    (curthread->specific[key].data != NULL)) {
125 				if (curthread->specific[key].seqno ==
126 				    _thread_keytable[key].seqno) {
127 					data = curthread->specific[key].data;
128 					destructor = _thread_keytable[key].destructor;
129 				}
130 				curthread->specific[key].data = NULL;
131 				curthread->specific_data_count--;
132 			}
133 
134 			/*
135 			 * If there is a destructore, call it
136 			 * with the key table entry unlocked:
137 			 */
138 			if (destructor != NULL) {
139 				/*
140 				 * Don't hold the lock while calling the
141 				 * destructor:
142 				 */
143 				THR_LOCK_RELEASE(curthread, &_keytable_lock);
144 				destructor(__DECONST(void *, data));
145 				THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
146 			}
147 		}
148 	}
149 	THR_LOCK_RELEASE(curthread, &_keytable_lock);
150 	free(curthread->specific);
151 	curthread->specific = NULL;
152 	if (curthread->specific_data_count > 0)
153 		stderr_debug("Thread %p has exited with leftover "
154 		    "thread-specific data after %d destructor iterations\n",
155 		    curthread, PTHREAD_DESTRUCTOR_ITERATIONS);
156 }
157 
158 static inline struct pthread_specific_elem *
159 pthread_key_allocate_data(void)
160 {
161 	struct pthread_specific_elem *new_data;
162 
163 	new_data = (struct pthread_specific_elem *)
164 	    calloc(1, sizeof(struct pthread_specific_elem) * PTHREAD_KEYS_MAX);
165 	return (new_data);
166 }
167 
168 int
169 _pthread_setspecific(pthread_key_t key, const void *value)
170 {
171 	struct pthread	*pthread;
172 	int		ret = 0;
173 
174 	/* Point to the running thread: */
175 	pthread = _get_curthread();
176 
177 	if ((pthread->specific) ||
178 	    (pthread->specific = pthread_key_allocate_data())) {
179 		if ((unsigned int)key < PTHREAD_KEYS_MAX) {
180 			if (_thread_keytable[key].allocated) {
181 				if (pthread->specific[key].data == NULL) {
182 					if (value != NULL)
183 						pthread->specific_data_count++;
184 				} else if (value == NULL)
185 					pthread->specific_data_count--;
186 				pthread->specific[key].data = value;
187 				pthread->specific[key].seqno =
188 				    _thread_keytable[key].seqno;
189 				ret = 0;
190 			} else
191 				ret = EINVAL;
192 		} else
193 			ret = EINVAL;
194 	} else
195 		ret = ENOMEM;
196 	return (ret);
197 }
198 
199 void *
200 _pthread_getspecific(pthread_key_t key)
201 {
202 	struct pthread	*pthread;
203 	const void	*data;
204 
205 	/* Point to the running thread: */
206 	pthread = _get_curthread();
207 
208 	/* Check if there is specific data: */
209 	if (pthread->specific != NULL && (unsigned int)key < PTHREAD_KEYS_MAX) {
210 		/* Check if this key has been used before: */
211 		if (_thread_keytable[key].allocated &&
212 		    (pthread->specific[key].seqno == _thread_keytable[key].seqno)) {
213 			/* Return the value: */
214 			data = pthread->specific[key].data;
215 		} else {
216 			/*
217 			 * This key has not been used before, so return NULL
218 			 * instead:
219 			 */
220 			data = NULL;
221 		}
222 	} else
223 		/* No specific data has been created, so just return NULL: */
224 		data = NULL;
225 	return (__DECONST(void *, data));
226 }
227