1 /* Linuxthreads - a simple clone()-based implementation of Posix */
2 /* threads for Linux. */
3 /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
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. */
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. */
15 /* Thread-specific data */
20 #include "internals.h"
22 typedef void (*destr_function)(void *);
26 struct pthread_key_struct {
27 int in_use; /* already allocated? */
28 destr_function destr; /* destruction routine */
31 static struct pthread_key_struct pthread_keys[PTHREAD_KEYS_MAX] =
34 /* Mutex to protect access to pthread_keys */
36 static pthread_mutex_t pthread_keys_mutex = PTHREAD_MUTEX_INITIALIZER;
38 /* Create a new key */
40 int __pthread_key_create(pthread_key_t * key, destr_function destr)
44 pthread_mutex_lock(&pthread_keys_mutex);
45 for (i = 0; i < PTHREAD_KEYS_MAX; i++) {
46 if (! pthread_keys[i].in_use) {
48 pthread_keys[i].in_use = 1;
49 pthread_keys[i].destr = destr;
50 pthread_mutex_unlock(&pthread_keys_mutex);
55 pthread_mutex_unlock(&pthread_keys_mutex);
58 weak_alias (__pthread_key_create, pthread_key_create)
62 int pthread_key_delete(pthread_key_t key)
64 pthread_descr self = thread_self();
66 unsigned int idx1st, idx2nd;
68 pthread_mutex_lock(&pthread_keys_mutex);
69 if (key >= PTHREAD_KEYS_MAX || !pthread_keys[key].in_use) {
70 pthread_mutex_unlock(&pthread_keys_mutex);
73 pthread_keys[key].in_use = 0;
74 pthread_keys[key].destr = NULL;
75 /* Set the value of the key to NULL in all running threads, so that
76 if the key is reallocated later by pthread_key_create, its
77 associated values will be NULL in all threads. */
78 idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
79 idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
82 if (th->p_specific[idx1st] != NULL)
83 th->p_specific[idx1st][idx2nd] = NULL;
86 pthread_mutex_unlock(&pthread_keys_mutex);
90 /* Set the value of a key */
92 int __pthread_setspecific(pthread_key_t key, const void * pointer)
94 pthread_descr self = thread_self();
95 unsigned int idx1st, idx2nd;
97 if (key >= PTHREAD_KEYS_MAX || !pthread_keys[key].in_use)
99 idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
100 idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
101 if (self->p_specific[idx1st] == NULL) {
102 self->p_specific[idx1st] =
103 calloc(PTHREAD_KEY_2NDLEVEL_SIZE, sizeof (void *));
104 if (self->p_specific[idx1st] == NULL)
107 self->p_specific[idx1st][idx2nd] = (void *) pointer;
110 weak_alias (__pthread_setspecific, pthread_setspecific)
112 /* Get the value of a key */
114 void * __pthread_getspecific(pthread_key_t key)
116 pthread_descr self = thread_self();
117 unsigned int idx1st, idx2nd;
119 if (key >= PTHREAD_KEYS_MAX)
121 idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
122 idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
123 if (self->p_specific[idx1st] == NULL || !pthread_keys[key].in_use)
125 return self->p_specific[idx1st][idx2nd];
127 weak_alias (__pthread_getspecific, pthread_getspecific)
129 /* Call the destruction routines on all keys */
131 void __pthread_destroy_specifics()
133 pthread_descr self = thread_self();
134 int i, j, round, found_nonzero;
135 destr_function destr;
138 for (round = 0, found_nonzero = 1;
139 found_nonzero && round < PTHREAD_DESTRUCTOR_ITERATIONS;
142 for (i = 0; i < PTHREAD_KEY_1STLEVEL_SIZE; i++)
143 if (self->p_specific[i] != NULL)
144 for (j = 0; j < PTHREAD_KEY_2NDLEVEL_SIZE; j++) {
145 destr = pthread_keys[i * PTHREAD_KEY_2NDLEVEL_SIZE + j].destr;
146 data = self->p_specific[i][j];
147 if (destr != NULL && data != NULL) {
148 self->p_specific[i][j] = NULL;
154 for (i = 0; i < PTHREAD_KEY_1STLEVEL_SIZE; i++) {
155 if (self->p_specific[i] != NULL) free(self->p_specific[i]);
159 /* Thread-specific data for libc. */
161 int __libc_internal_tsd_set(enum __libc_tsd_key_t key, const void * pointer)
163 pthread_descr self = thread_self();
165 self->p_libc_specific[key] = (void *) pointer;
169 void * __libc_internal_tsd_get(enum __libc_tsd_key_t key)
171 pthread_descr self = thread_self();
173 return self->p_libc_specific[key];