1 /* Linuxthreads - a simple clone()-based implementation of Posix        */
2 /* threads for Linux.                                                   */
3 /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr)              */
4 /*                                                                      */
5 /* This program is free software; you can redistribute it and/or        */
6 /* modify it under the terms of the GNU Library General Public License  */
7 /* as published by the Free Software Foundation; either version 2       */
8 /* of the License, or (at your option) any later version.               */
9 /*                                                                      */
10 /* This program is distributed in the hope that it will be useful,      */
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of       */
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        */
13 /* GNU Library General Public License for more details.                 */
14 
15 /* Thread-specific data */
16 
17 #include <features.h>
18 #include <errno.h>
19 #include <stddef.h>
20 #include <stdlib.h>
21 #include "pthread.h"
22 #include "internals.h"
23 #include "spinlock.h"
24 #include "restart.h"
25 
26 /* Table of keys. */
27 
28 static struct pthread_key_struct pthread_keys[PTHREAD_KEYS_MAX] =
29   { { 0, NULL } };
30 
31 /* For debugging purposes put the maximum number of keys in a variable.  */
32 const int __linuxthreads_pthread_keys_max = PTHREAD_KEYS_MAX;
33 const int __linuxthreads_pthread_key_2ndlevel_size = PTHREAD_KEY_2NDLEVEL_SIZE;
34 
35 /* Mutex to protect access to pthread_keys */
36 
37 static pthread_mutex_t pthread_keys_mutex = PTHREAD_MUTEX_INITIALIZER;
38 
39 /* Create a new key */
40 
pthread_key_create(pthread_key_t * key,destr_function destr)41 int pthread_key_create(pthread_key_t * key, destr_function destr)
42 {
43   int i;
44 
45   __pthread_mutex_lock(&pthread_keys_mutex);
46   for (i = 0; i < PTHREAD_KEYS_MAX; i++) {
47     if (! pthread_keys[i].in_use) {
48       /* Mark key in use */
49       pthread_keys[i].in_use = 1;
50       pthread_keys[i].destr = destr;
51       __pthread_mutex_unlock(&pthread_keys_mutex);
52       *key = i;
53       return 0;
54     }
55   }
56   __pthread_mutex_unlock(&pthread_keys_mutex);
57   return EAGAIN;
58 }
59 
60 /* Delete a key */
pthread_key_delete(pthread_key_t key)61 int pthread_key_delete(pthread_key_t key)
62 {
63     pthread_descr self = thread_self();
64 
65     __pthread_mutex_lock(&pthread_keys_mutex);
66     if (key >= PTHREAD_KEYS_MAX || !pthread_keys[key].in_use) {
67 	__pthread_mutex_unlock(&pthread_keys_mutex);
68 	return EINVAL;
69     }
70     pthread_keys[key].in_use = 0;
71     pthread_keys[key].destr = NULL;
72 
73     /* Set the value of the key to NULL in all running threads, so
74        that if the key is reallocated later by pthread_key_create, its
75        associated values will be NULL in all threads.
76        Do nothing if no threads have been created yet.  */
77     if (__pthread_manager_request != -1)
78     {
79 	pthread_descr th;
80 	unsigned int idx1st, idx2nd;
81 
82 	idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
83 	idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
84 	th = self;
85 	do {
86 	    /* If the thread already is terminated don't modify the memory.  */
87 	    if (!th->p_terminated && th->p_specific[idx1st] != NULL)
88 		th->p_specific[idx1st][idx2nd] = NULL;
89 	    th = th->p_nextlive;
90 	} while (th != self);
91     }
92 
93     __pthread_mutex_unlock(&pthread_keys_mutex);
94     return 0;
95 }
96 
97 /* Set the value of a key */
98 
pthread_setspecific(pthread_key_t key,const void * pointer)99 int pthread_setspecific(pthread_key_t key, const void * pointer)
100 {
101     pthread_descr self = thread_self();
102     unsigned int idx1st, idx2nd;
103 
104     if (key >= PTHREAD_KEYS_MAX || !pthread_keys[key].in_use)
105 	return EINVAL;
106     idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
107     idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
108     if (THREAD_GETMEM_NC(self, p_specific[idx1st]) == NULL) {
109 	void *newp = calloc(PTHREAD_KEY_2NDLEVEL_SIZE, sizeof (void *));
110 	if (newp == NULL)
111 	    return ENOMEM;
112 	THREAD_SETMEM_NC(self, p_specific[idx1st], newp);
113     }
114     THREAD_GETMEM_NC(self, p_specific[idx1st])[idx2nd] = (void *) pointer;
115     return 0;
116 }
117 
118 /* Get the value of a key */
119 
pthread_getspecific(pthread_key_t key)120 void * pthread_getspecific(pthread_key_t key)
121 {
122     pthread_descr self = thread_self();
123     unsigned int idx1st, idx2nd;
124 
125     if (key >= PTHREAD_KEYS_MAX)
126 	return NULL;
127     idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
128     idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
129     if (THREAD_GETMEM_NC(self, p_specific[idx1st]) == NULL
130 	    || !pthread_keys[key].in_use)
131 	return NULL;
132     return THREAD_GETMEM_NC(self, p_specific[idx1st])[idx2nd];
133 }
134 
135 /* Call the destruction routines on all keys */
136 
__pthread_destroy_specifics(void)137 void __pthread_destroy_specifics(void)
138 {
139     pthread_descr self = thread_self();
140     int i, j, round, found_nonzero;
141     destr_function destr;
142     void * data;
143 
144     for (round = 0, found_nonzero = 1;
145 	    found_nonzero && round < PTHREAD_DESTRUCTOR_ITERATIONS;
146 	    round++) {
147 	found_nonzero = 0;
148 	for (i = 0; i < PTHREAD_KEY_1STLEVEL_SIZE; i++)
149 	    if (THREAD_GETMEM_NC(self, p_specific[i]) != NULL)
150 		for (j = 0; j < PTHREAD_KEY_2NDLEVEL_SIZE; j++) {
151 		    destr = pthread_keys[i * PTHREAD_KEY_2NDLEVEL_SIZE + j].destr;
152 		    data = THREAD_GETMEM_NC(self, p_specific[i])[j];
153 		    if (destr != NULL && data != NULL) {
154 			THREAD_GETMEM_NC(self, p_specific[i])[j] = NULL;
155 			destr(data);
156 			found_nonzero = 1;
157 		    }
158 		}
159     }
160     __pthread_lock(THREAD_GETMEM(self, p_lock), self);
161     for (i = 0; i < PTHREAD_KEY_1STLEVEL_SIZE; i++) {
162 	if (THREAD_GETMEM_NC(self, p_specific[i]) != NULL) {
163 	    free(THREAD_GETMEM_NC(self, p_specific[i]));
164 	    THREAD_SETMEM_NC(self, p_specific[i], NULL);
165 	}
166     }
167     __pthread_unlock(THREAD_GETMEM(self, p_lock));
168 }
169 
170 #if !defined __UCLIBC_HAS_TLS__ && defined __UCLIBC_HAS_RPC__
171 
172 /* Thread-specific data for libc. */
173 
174 int
__pthread_internal_tsd_set(int key,const void * pointer)175 __pthread_internal_tsd_set (int key, const void * pointer)
176 {
177   pthread_descr self = thread_self();
178 
179   THREAD_SETMEM_NC(self, p_libc_specific[key], (void *) pointer);
180   return 0;
181 }
182 
183 void *
__pthread_internal_tsd_get(int key)184 __pthread_internal_tsd_get (int key)
185 {
186   pthread_descr self = thread_self();
187 
188   return THREAD_GETMEM_NC(self, p_libc_specific[key]);
189 }
190 
191 void ** __attribute__ ((__const__))
__pthread_internal_tsd_address(int key)192 __pthread_internal_tsd_address (int key)
193 {
194   pthread_descr self = thread_self();
195   return &self->p_libc_specific[key];
196 }
197 
198 #endif
199