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