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