xref: /freebsd/lib/libthr/thread/thr_spec.c (revision 2b743a9e9ddc6736208dc8ca1ce06ce64ad20a19)
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 = _get_curthread();
55 	int i;
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 	struct pthread *curthread = _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 	struct pthread	*curthread = _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 			}
130 
131 			/*
132 			 * If there is a destructore, call it
133 			 * with the key table entry unlocked:
134 			 */
135 			if (destructor != NULL) {
136 				/*
137 				 * Don't hold the lock while calling the
138 				 * destructor:
139 				 */
140 				THR_LOCK_RELEASE(curthread, &_keytable_lock);
141 				destructor(__DECONST(void *, data));
142 				THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
143 			}
144 		}
145 	}
146 	THR_LOCK_RELEASE(curthread, &_keytable_lock);
147 	free(curthread->specific);
148 	curthread->specific = NULL;
149 	if (curthread->specific_data_count > 0)
150 		stderr_debug("Thread %p has exited with leftover "
151 		    "thread-specific data after %d destructor iterations\n",
152 		    curthread, PTHREAD_DESTRUCTOR_ITERATIONS);
153 }
154 
155 static inline struct pthread_specific_elem *
156 pthread_key_allocate_data(void)
157 {
158 	struct pthread_specific_elem *new_data;
159 
160 	new_data = (struct pthread_specific_elem *)
161 	    calloc(1, sizeof(struct pthread_specific_elem) * PTHREAD_KEYS_MAX);
162 	return (new_data);
163 }
164 
165 int
166 _pthread_setspecific(pthread_key_t key, const void *value)
167 {
168 	struct pthread	*pthread;
169 	int		ret = 0;
170 
171 	/* Point to the running thread: */
172 	pthread = _get_curthread();
173 
174 	if ((pthread->specific) ||
175 	    (pthread->specific = pthread_key_allocate_data())) {
176 		if ((unsigned int)key < PTHREAD_KEYS_MAX) {
177 			if (_thread_keytable[key].allocated) {
178 				if (pthread->specific[key].data == NULL) {
179 					if (value != NULL)
180 						pthread->specific_data_count++;
181 				} else if (value == NULL)
182 					pthread->specific_data_count--;
183 				pthread->specific[key].data = value;
184 				pthread->specific[key].seqno =
185 				    _thread_keytable[key].seqno;
186 				ret = 0;
187 			} else
188 				ret = EINVAL;
189 		} else
190 			ret = EINVAL;
191 	} else
192 		ret = ENOMEM;
193 	return (ret);
194 }
195 
196 void *
197 _pthread_getspecific(pthread_key_t key)
198 {
199 	struct pthread	*pthread;
200 	const void	*data;
201 
202 	/* Point to the running thread: */
203 	pthread = _get_curthread();
204 
205 	/* Check if there is specific data: */
206 	if (pthread->specific != NULL && (unsigned int)key < PTHREAD_KEYS_MAX) {
207 		/* Check if this key has been used before: */
208 		if (_thread_keytable[key].allocated &&
209 		    (pthread->specific[key].seqno == _thread_keytable[key].seqno)) {
210 			/* Return the value: */
211 			data = pthread->specific[key].data;
212 		} else {
213 			/*
214 			 * This key has not been used before, so return NULL
215 			 * instead:
216 			 */
217 			data = NULL;
218 		}
219 	} else
220 		/* No specific data has been created, so just return NULL: */
221 		data = NULL;
222 	return (__DECONST(void *, data));
223 }
224