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: releng/10.2/lib/libthr/thread/thr_spec.c 283690 2015-05-29 07:48:30Z kib $
30 */
31
32#include "namespace.h"
33#include <sys/mman.h>
34#include <signal.h>
35#include <stdlib.h>
36#include <string.h>
37#include <errno.h>
38#include <pthread.h>
39#include "un-namespace.h"
40#include "libc_private.h"
41
42#include "thr_private.h"
43
44struct 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
52int
53_pthread_key_create(pthread_key_t *key, void (*destructor)(void *))
54{
55	struct pthread *curthread;
56	int i;
57
58	_thr_check_init();
59
60	curthread = _get_curthread();
61
62	THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
63	for (i = 0; i < PTHREAD_KEYS_MAX; i++) {
64
65		if (_thread_keytable[i].allocated == 0) {
66			_thread_keytable[i].allocated = 1;
67			_thread_keytable[i].destructor = destructor;
68			_thread_keytable[i].seqno++;
69
70			THR_LOCK_RELEASE(curthread, &_keytable_lock);
71			*key = i + 1;
72			return (0);
73		}
74
75	}
76	THR_LOCK_RELEASE(curthread, &_keytable_lock);
77	return (EAGAIN);
78}
79
80int
81_pthread_key_delete(pthread_key_t userkey)
82{
83	struct pthread *curthread;
84	int key, ret;
85
86	key = userkey - 1;
87	if ((unsigned int)key >= PTHREAD_KEYS_MAX)
88		return (EINVAL);
89	curthread = _get_curthread();
90	THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
91	if (_thread_keytable[key].allocated) {
92		_thread_keytable[key].allocated = 0;
93		ret = 0;
94	} else {
95		ret = EINVAL;
96	}
97	THR_LOCK_RELEASE(curthread, &_keytable_lock);
98	return (ret);
99}
100
101void
102_thread_cleanupspecific(void)
103{
104	struct pthread *curthread;
105	void (*destructor)(void *);
106	const void *data;
107	int i, key;
108
109	curthread = _get_curthread();
110	if (curthread->specific == NULL)
111		return;
112	THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
113	for (i = 0; i < PTHREAD_DESTRUCTOR_ITERATIONS &&
114	    curthread->specific_data_count > 0; i++) {
115		for (key = 0; key < PTHREAD_KEYS_MAX &&
116		    curthread->specific_data_count > 0; key++) {
117			destructor = NULL;
118
119			if (_thread_keytable[key].allocated &&
120			    (curthread->specific[key].data != NULL)) {
121				if (curthread->specific[key].seqno ==
122				    _thread_keytable[key].seqno) {
123					data = curthread->specific[key].data;
124					destructor = _thread_keytable[key].
125					    destructor;
126				}
127				curthread->specific[key].data = NULL;
128				curthread->specific_data_count--;
129			} else if (curthread->specific[key].data != NULL) {
130				/*
131				 * This can happen if the key is
132				 * deleted via pthread_key_delete
133				 * without first setting the value to
134				 * NULL in all threads.  POSIX says
135				 * that the destructor is not invoked
136				 * in this case.
137				 */
138				curthread->specific[key].data = NULL;
139				curthread->specific_data_count--;
140			}
141
142			/*
143			 * If there is a destructor, call it with the
144			 * key table entry unlocked.
145			 */
146			if (destructor != NULL) {
147				THR_LOCK_RELEASE(curthread, &_keytable_lock);
148				destructor(__DECONST(void *, data));
149				THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
150			}
151		}
152	}
153	THR_LOCK_RELEASE(curthread, &_keytable_lock);
154	munmap(curthread->specific, PTHREAD_KEYS_MAX * sizeof(struct
155	    pthread_specific_elem));
156	curthread->specific = NULL;
157	if (curthread->specific_data_count > 0) {
158		stderr_debug("Thread %p has exited with leftover "
159		    "thread-specific data after %d destructor iterations\n",
160		    curthread, PTHREAD_DESTRUCTOR_ITERATIONS);
161	}
162}
163
164int
165_pthread_setspecific(pthread_key_t userkey, const void *value)
166{
167	struct pthread *pthread;
168	void *tmp;
169	pthread_key_t key;
170
171	key = userkey - 1;
172	if ((unsigned int)key >= PTHREAD_KEYS_MAX ||
173	    !_thread_keytable[key].allocated)
174		return (EINVAL);
175
176	pthread = _get_curthread();
177	if (pthread->specific == NULL) {
178		tmp = mmap(NULL, PTHREAD_KEYS_MAX *
179		    sizeof(struct pthread_specific_elem),
180		    PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
181		if (tmp == MAP_FAILED)
182			return (ENOMEM);
183		pthread->specific = tmp;
184	}
185	if (pthread->specific[key].data == NULL) {
186		if (value != NULL)
187			pthread->specific_data_count++;
188	} else if (value == NULL)
189		pthread->specific_data_count--;
190	pthread->specific[key].data = value;
191	pthread->specific[key].seqno = _thread_keytable[key].seqno;
192	return (0);
193}
194
195void *
196_pthread_getspecific(pthread_key_t userkey)
197{
198	struct pthread *pthread;
199	const void *data;
200	pthread_key_t key;
201
202	/* Check if there is specific data. */
203	key = userkey - 1;
204	if ((unsigned int)key >= PTHREAD_KEYS_MAX)
205		return (NULL);
206
207	pthread = _get_curthread();
208	/* Check if this key has been used before. */
209	if (_thread_keytable[key].allocated && pthread->specific != NULL &&
210	    pthread->specific[key].seqno == _thread_keytable[key].seqno) {
211		/* Return the value: */
212		data = pthread->specific[key].data;
213	} else {
214		/*
215		 * This key has not been used before, so return NULL
216		 * instead.
217		 */
218		data = NULL;
219	}
220	return (__DECONST(void *, data));
221}
222
223void
224_thr_tsd_unload(struct dl_phdr_info *phdr_info)
225{
226	struct pthread *curthread;
227	void (*destructor)(void *);
228	int key;
229
230	curthread = _get_curthread();
231	THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
232	for (key = 0; key < PTHREAD_KEYS_MAX; key++) {
233		if (!_thread_keytable[key].allocated)
234			continue;
235		destructor = _thread_keytable[key].destructor;
236		if (destructor == NULL)
237			continue;
238		if (__elf_phdr_match_addr(phdr_info, destructor))
239			_thread_keytable[key].destructor = NULL;
240	}
241	THR_LOCK_RELEASE(curthread, &_keytable_lock);
242}
243