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