18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
4bb535300SJeff Roberson * Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>.
5bb535300SJeff Roberson * All rights reserved.
6bb535300SJeff Roberson *
7bb535300SJeff Roberson * Redistribution and use in source and binary forms, with or without
8bb535300SJeff Roberson * modification, are permitted provided that the following conditions
9bb535300SJeff Roberson * are met:
10bb535300SJeff Roberson * 1. Redistributions of source code must retain the above copyright
11bb535300SJeff Roberson * notice, this list of conditions and the following disclaimer.
12bb535300SJeff Roberson * 2. Redistributions in binary form must reproduce the above copyright
13bb535300SJeff Roberson * notice, this list of conditions and the following disclaimer in the
14bb535300SJeff Roberson * documentation and/or other materials provided with the distribution.
15fed32d75SWarner Losh * 3. Neither the name of the author nor the names of any co-contributors
16bb535300SJeff Roberson * may be used to endorse or promote products derived from this software
17bb535300SJeff Roberson * without specific prior written permission.
18bb535300SJeff Roberson *
19bb535300SJeff Roberson * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
20bb535300SJeff Roberson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21bb535300SJeff Roberson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22bb535300SJeff Roberson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23bb535300SJeff Roberson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24bb535300SJeff Roberson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25bb535300SJeff Roberson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26bb535300SJeff Roberson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27bb535300SJeff Roberson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28bb535300SJeff Roberson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29bb535300SJeff Roberson * SUCH DAMAGE.
30bb535300SJeff Roberson */
31a091d823SDavid Xu
3237a6356bSDavid Xu #include "namespace.h"
339be6046aSKonstantin Belousov #include <sys/mman.h>
34bb535300SJeff Roberson #include <signal.h>
35bb535300SJeff Roberson #include <stdlib.h>
36bb535300SJeff Roberson #include <string.h>
37bb535300SJeff Roberson #include <errno.h>
38bb535300SJeff Roberson #include <pthread.h>
3937a6356bSDavid Xu #include "un-namespace.h"
40ed0ee6afSDavid Xu #include "libc_private.h"
41a091d823SDavid Xu
42bb535300SJeff Roberson #include "thr_private.h"
43bb535300SJeff Roberson
4437a6f461SMichael Zhilin /* Used in symbol lookup of libthread_db */
4537a6f461SMichael Zhilin struct pthread_key _thread_keytable[PTHREAD_KEYS_MAX];
46bb535300SJeff Roberson
47*0ab1bfc7SKonstantin Belousov __weak_reference(_thr_key_create, pthread_key_create);
48*0ab1bfc7SKonstantin Belousov __weak_reference(_thr_key_create, _pthread_key_create);
49*0ab1bfc7SKonstantin Belousov __weak_reference(_thr_key_delete, pthread_key_delete);
50*0ab1bfc7SKonstantin Belousov __weak_reference(_thr_key_delete, _pthread_key_delete);
51*0ab1bfc7SKonstantin Belousov __weak_reference(_thr_getspecific, pthread_getspecific);
52*0ab1bfc7SKonstantin Belousov __weak_reference(_thr_getspecific, _pthread_getspecific);
53*0ab1bfc7SKonstantin Belousov __weak_reference(_thr_setspecific, pthread_setspecific);
54*0ab1bfc7SKonstantin Belousov __weak_reference(_thr_setspecific, _pthread_setspecific);
55bb535300SJeff Roberson
56bb535300SJeff Roberson int
_thr_key_create(pthread_key_t * key,void (* destructor)(void *))57*0ab1bfc7SKonstantin Belousov _thr_key_create(pthread_key_t *key, void (*destructor)(void *))
58bb535300SJeff Roberson {
599a2706abSMarius Strobl struct pthread *curthread;
60a091d823SDavid Xu int i;
61bb535300SJeff Roberson
629a2706abSMarius Strobl _thr_check_init();
639a2706abSMarius Strobl
649a2706abSMarius Strobl curthread = _get_curthread();
659a2706abSMarius Strobl
66a091d823SDavid Xu THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
67a091d823SDavid Xu for (i = 0; i < PTHREAD_KEYS_MAX; i++) {
68bb535300SJeff Roberson
69a091d823SDavid Xu if (_thread_keytable[i].allocated == 0) {
70a091d823SDavid Xu _thread_keytable[i].allocated = 1;
71a091d823SDavid Xu _thread_keytable[i].destructor = destructor;
72a091d823SDavid Xu _thread_keytable[i].seqno++;
73a091d823SDavid Xu
74a091d823SDavid Xu THR_LOCK_RELEASE(curthread, &_keytable_lock);
7580969150SDavid Xu *key = i + 1;
76bb535300SJeff Roberson return (0);
77bb535300SJeff Roberson }
78bb535300SJeff Roberson
79bb535300SJeff Roberson }
80a091d823SDavid Xu THR_LOCK_RELEASE(curthread, &_keytable_lock);
81bb535300SJeff Roberson return (EAGAIN);
82bb535300SJeff Roberson }
83bb535300SJeff Roberson
84bb535300SJeff Roberson int
_thr_key_delete(pthread_key_t userkey)85*0ab1bfc7SKonstantin Belousov _thr_key_delete(pthread_key_t userkey)
86bb535300SJeff Roberson {
879be6046aSKonstantin Belousov struct pthread *curthread;
889be6046aSKonstantin Belousov int key, ret;
89bb535300SJeff Roberson
909be6046aSKonstantin Belousov key = userkey - 1;
919be6046aSKonstantin Belousov if ((unsigned int)key >= PTHREAD_KEYS_MAX)
929be6046aSKonstantin Belousov return (EINVAL);
939be6046aSKonstantin Belousov curthread = _get_curthread();
94a091d823SDavid Xu THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
959be6046aSKonstantin Belousov if (_thread_keytable[key].allocated) {
96a091d823SDavid Xu _thread_keytable[key].allocated = 0;
979be6046aSKonstantin Belousov ret = 0;
989be6046aSKonstantin Belousov } else {
99bb535300SJeff Roberson ret = EINVAL;
1009be6046aSKonstantin Belousov }
101a091d823SDavid Xu THR_LOCK_RELEASE(curthread, &_keytable_lock);
102bb535300SJeff Roberson return (ret);
103bb535300SJeff Roberson }
104bb535300SJeff Roberson
105bb535300SJeff Roberson void
_thread_cleanupspecific(void)106bb535300SJeff Roberson _thread_cleanupspecific(void)
107bb535300SJeff Roberson {
1089be6046aSKonstantin Belousov struct pthread *curthread;
109a091d823SDavid Xu void (*destructor)(void *);
1109be6046aSKonstantin Belousov const void *data;
1119be6046aSKonstantin Belousov int i, key;
112bb535300SJeff Roberson
1139be6046aSKonstantin Belousov curthread = _get_curthread();
114a091d823SDavid Xu if (curthread->specific == NULL)
115a091d823SDavid Xu return;
116a091d823SDavid Xu THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
1179be6046aSKonstantin Belousov for (i = 0; i < PTHREAD_DESTRUCTOR_ITERATIONS &&
1189be6046aSKonstantin Belousov curthread->specific_data_count > 0; i++) {
1199be6046aSKonstantin Belousov for (key = 0; key < PTHREAD_KEYS_MAX &&
1209be6046aSKonstantin Belousov curthread->specific_data_count > 0; key++) {
121bb535300SJeff Roberson destructor = NULL;
122bb535300SJeff Roberson
123a091d823SDavid Xu if (_thread_keytable[key].allocated &&
124bb535300SJeff Roberson (curthread->specific[key].data != NULL)) {
125bb535300SJeff Roberson if (curthread->specific[key].seqno ==
126a091d823SDavid Xu _thread_keytable[key].seqno) {
12737a6356bSDavid Xu data = curthread->specific[key].data;
1289be6046aSKonstantin Belousov destructor = _thread_keytable[key].
1299be6046aSKonstantin Belousov destructor;
130bb535300SJeff Roberson }
131bb535300SJeff Roberson curthread->specific[key].data = NULL;
132bb535300SJeff Roberson curthread->specific_data_count--;
1339be6046aSKonstantin Belousov } else if (curthread->specific[key].data != NULL) {
134daf3ced7SDavid Xu /*
1359be6046aSKonstantin Belousov * This can happen if the key is
1369be6046aSKonstantin Belousov * deleted via pthread_key_delete
1379be6046aSKonstantin Belousov * without first setting the value to
1389be6046aSKonstantin Belousov * NULL in all threads. POSIX says
1399be6046aSKonstantin Belousov * that the destructor is not invoked
1409be6046aSKonstantin Belousov * in this case.
141daf3ced7SDavid Xu */
142daf3ced7SDavid Xu curthread->specific[key].data = NULL;
143daf3ced7SDavid Xu curthread->specific_data_count--;
144daf3ced7SDavid Xu }
145bb535300SJeff Roberson
146bb535300SJeff Roberson /*
1479be6046aSKonstantin Belousov * If there is a destructor, call it with the
1489be6046aSKonstantin Belousov * key table entry unlocked.
149bb535300SJeff Roberson */
150a091d823SDavid Xu if (destructor != NULL) {
151a091d823SDavid Xu THR_LOCK_RELEASE(curthread, &_keytable_lock);
15237a6356bSDavid Xu destructor(__DECONST(void *, data));
153a091d823SDavid Xu THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
154a091d823SDavid Xu }
155a091d823SDavid Xu }
156a091d823SDavid Xu }
157a091d823SDavid Xu THR_LOCK_RELEASE(curthread, &_keytable_lock);
158381c2d2eSKonstantin Belousov __thr_free(curthread->specific);
159bb535300SJeff Roberson curthread->specific = NULL;
1609be6046aSKonstantin Belousov if (curthread->specific_data_count > 0) {
161a091d823SDavid Xu stderr_debug("Thread %p has exited with leftover "
162a091d823SDavid Xu "thread-specific data after %d destructor iterations\n",
163a091d823SDavid Xu curthread, PTHREAD_DESTRUCTOR_ITERATIONS);
164bb535300SJeff Roberson }
165bb535300SJeff Roberson }
166bb535300SJeff Roberson
167bb535300SJeff Roberson int
_thr_setspecific(pthread_key_t userkey,const void * value)168*0ab1bfc7SKonstantin Belousov _thr_setspecific(pthread_key_t userkey, const void *value)
169bb535300SJeff Roberson {
170a091d823SDavid Xu struct pthread *pthread;
1719be6046aSKonstantin Belousov void *tmp;
1729be6046aSKonstantin Belousov pthread_key_t key;
173a091d823SDavid Xu
1749be6046aSKonstantin Belousov key = userkey - 1;
1759be6046aSKonstantin Belousov if ((unsigned int)key >= PTHREAD_KEYS_MAX ||
1769be6046aSKonstantin Belousov !_thread_keytable[key].allocated)
1779be6046aSKonstantin Belousov return (EINVAL);
1789be6046aSKonstantin Belousov
179a091d823SDavid Xu pthread = _get_curthread();
1809be6046aSKonstantin Belousov if (pthread->specific == NULL) {
181381c2d2eSKonstantin Belousov tmp = __thr_calloc(PTHREAD_KEYS_MAX,
182381c2d2eSKonstantin Belousov sizeof(struct pthread_specific_elem));
183381c2d2eSKonstantin Belousov if (tmp == NULL)
1849be6046aSKonstantin Belousov return (ENOMEM);
1859be6046aSKonstantin Belousov pthread->specific = tmp;
1869be6046aSKonstantin Belousov }
187bb535300SJeff Roberson if (pthread->specific[key].data == NULL) {
188bb535300SJeff Roberson if (value != NULL)
189bb535300SJeff Roberson pthread->specific_data_count++;
190a091d823SDavid Xu } else if (value == NULL)
191bb535300SJeff Roberson pthread->specific_data_count--;
192bb535300SJeff Roberson pthread->specific[key].data = value;
1939be6046aSKonstantin Belousov pthread->specific[key].seqno = _thread_keytable[key].seqno;
1949be6046aSKonstantin Belousov return (0);
195bb535300SJeff Roberson }
196bb535300SJeff Roberson
197bb535300SJeff Roberson void *
_thr_getspecific(pthread_key_t userkey)198*0ab1bfc7SKonstantin Belousov _thr_getspecific(pthread_key_t userkey)
199bb535300SJeff Roberson {
200a091d823SDavid Xu struct pthread *pthread;
20137a6356bSDavid Xu const void *data;
2029be6046aSKonstantin Belousov pthread_key_t key;
203bb535300SJeff Roberson
2049be6046aSKonstantin Belousov /* Check if there is specific data. */
2059be6046aSKonstantin Belousov key = userkey - 1;
2069be6046aSKonstantin Belousov if ((unsigned int)key >= PTHREAD_KEYS_MAX)
2079be6046aSKonstantin Belousov return (NULL);
2089be6046aSKonstantin Belousov
209a091d823SDavid Xu pthread = _get_curthread();
2109be6046aSKonstantin Belousov /* Check if this key has been used before. */
2119be6046aSKonstantin Belousov if (_thread_keytable[key].allocated && pthread->specific != NULL &&
2129be6046aSKonstantin Belousov pthread->specific[key].seqno == _thread_keytable[key].seqno) {
213bb535300SJeff Roberson /* Return the value: */
21437a6356bSDavid Xu data = pthread->specific[key].data;
215bb535300SJeff Roberson } else {
216bb535300SJeff Roberson /*
217bb535300SJeff Roberson * This key has not been used before, so return NULL
2189be6046aSKonstantin Belousov * instead.
219bb535300SJeff Roberson */
220bb535300SJeff Roberson data = NULL;
221bb535300SJeff Roberson }
22237a6356bSDavid Xu return (__DECONST(void *, data));
223bb535300SJeff Roberson }
224ed0ee6afSDavid Xu
225ed0ee6afSDavid Xu void
_thr_tsd_unload(struct dl_phdr_info * phdr_info)226ed0ee6afSDavid Xu _thr_tsd_unload(struct dl_phdr_info *phdr_info)
227ed0ee6afSDavid Xu {
2289be6046aSKonstantin Belousov struct pthread *curthread;
229ed0ee6afSDavid Xu void (*destructor)(void *);
230ed0ee6afSDavid Xu int key;
231ed0ee6afSDavid Xu
2329be6046aSKonstantin Belousov curthread = _get_curthread();
233ed0ee6afSDavid Xu THR_LOCK_ACQUIRE(curthread, &_keytable_lock);
234ed0ee6afSDavid Xu for (key = 0; key < PTHREAD_KEYS_MAX; key++) {
2359be6046aSKonstantin Belousov if (!_thread_keytable[key].allocated)
2369be6046aSKonstantin Belousov continue;
237ed0ee6afSDavid Xu destructor = _thread_keytable[key].destructor;
2389be6046aSKonstantin Belousov if (destructor == NULL)
2399be6046aSKonstantin Belousov continue;
240ed0ee6afSDavid Xu if (__elf_phdr_match_addr(phdr_info, destructor))
241ed0ee6afSDavid Xu _thread_keytable[key].destructor = NULL;
242ed0ee6afSDavid Xu }
243ed0ee6afSDavid Xu THR_LOCK_RELEASE(curthread, &_keytable_lock);
244ed0ee6afSDavid Xu }
245