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