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