Update.
[glibc.git] / linuxthreads / specific.c
blob0c302d4b4196f00f60935bfb8fb63e1594855e07
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. */
15 /* Thread-specific data */
17 #include <errno.h>
18 #include <stddef.h>
19 #include <stdlib.h>
20 #include "pthread.h"
21 #include "internals.h"
23 /* Table of keys. */
25 static struct pthread_key_struct pthread_keys[PTHREAD_KEYS_MAX] =
26 { { 0, NULL } };
28 /* For debugging purposes put the maximum number of keys in a variable. */
29 const int __linuxthreads_pthread_keys_max = PTHREAD_KEYS_MAX;
30 const int __linuxthreads_pthread_key_2ndlevel_size = PTHREAD_KEY_2NDLEVEL_SIZE;
32 /* Mutex to protect access to pthread_keys */
34 static pthread_mutex_t pthread_keys_mutex = PTHREAD_MUTEX_INITIALIZER;
36 /* Create a new key */
38 int __pthread_key_create(pthread_key_t * key, destr_function destr)
40 int i;
42 pthread_mutex_lock(&pthread_keys_mutex);
43 for (i = 0; i < PTHREAD_KEYS_MAX; i++) {
44 if (! pthread_keys[i].in_use) {
45 /* Mark key in use */
46 pthread_keys[i].in_use = 1;
47 pthread_keys[i].destr = destr;
48 pthread_mutex_unlock(&pthread_keys_mutex);
49 *key = i;
50 return 0;
53 pthread_mutex_unlock(&pthread_keys_mutex);
54 return EAGAIN;
56 strong_alias (__pthread_key_create, pthread_key_create)
58 /* Delete a key */
60 int pthread_key_delete(pthread_key_t key)
62 pthread_descr self = thread_self();
63 pthread_descr th;
64 unsigned int idx1st, idx2nd;
66 pthread_mutex_lock(&pthread_keys_mutex);
67 if (key >= PTHREAD_KEYS_MAX || !pthread_keys[key].in_use) {
68 pthread_mutex_unlock(&pthread_keys_mutex);
69 return EINVAL;
71 pthread_keys[key].in_use = 0;
72 pthread_keys[key].destr = NULL;
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 idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
77 idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
78 th = self;
79 do {
80 /* If the thread already is terminated don't modify the memory. */
81 if (!th->p_terminated && th->p_specific[idx1st] != NULL)
82 th->p_specific[idx1st][idx2nd] = NULL;
83 th = th->p_nextlive;
84 } while (th != self);
85 pthread_mutex_unlock(&pthread_keys_mutex);
86 return 0;
89 /* Set the value of a key */
91 int __pthread_setspecific(pthread_key_t key, const void * pointer)
93 pthread_descr self = thread_self();
94 unsigned int idx1st, idx2nd;
96 if (key >= PTHREAD_KEYS_MAX || !pthread_keys[key].in_use)
97 return EINVAL;
98 idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
99 idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
100 if (THREAD_GETMEM_NC(self, p_specific[idx1st]) == NULL) {
101 void *newp = calloc(PTHREAD_KEY_2NDLEVEL_SIZE, sizeof (void *));
102 if (newp == NULL)
103 return ENOMEM;
104 THREAD_SETMEM_NC(self, p_specific[idx1st], newp);
106 THREAD_GETMEM_NC(self, p_specific[idx1st])[idx2nd] = (void *) pointer;
107 return 0;
109 strong_alias (__pthread_setspecific, pthread_setspecific)
111 /* Get the value of a key */
113 void * __pthread_getspecific(pthread_key_t key)
115 pthread_descr self = thread_self();
116 unsigned int idx1st, idx2nd;
118 if (key >= PTHREAD_KEYS_MAX)
119 return NULL;
120 idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
121 idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
122 if (THREAD_GETMEM_NC(self, p_specific[idx1st]) == NULL
123 || !pthread_keys[key].in_use)
124 return NULL;
125 return THREAD_GETMEM_NC(self, p_specific[idx1st])[idx2nd];
127 strong_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;
136 void * data;
138 for (round = 0, found_nonzero = 1;
139 found_nonzero && round < PTHREAD_DESTRUCTOR_ITERATIONS;
140 round++) {
141 found_nonzero = 0;
142 for (i = 0; i < PTHREAD_KEY_1STLEVEL_SIZE; i++)
143 if (THREAD_GETMEM_NC(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 = THREAD_GETMEM_NC(self, p_specific[i])[j];
147 if (destr != NULL && data != NULL) {
148 THREAD_GETMEM_NC(self, p_specific[i])[j] = NULL;
149 destr(data);
150 found_nonzero = 1;
154 for (i = 0; i < PTHREAD_KEY_1STLEVEL_SIZE; i++) {
155 if (THREAD_GETMEM_NC(self, p_specific[i]) != NULL)
156 free(THREAD_GETMEM_NC(self, p_specific[i]));
160 /* Thread-specific data for libc. */
162 static int
163 libc_internal_tsd_set(enum __libc_tsd_key_t key, const void * pointer)
165 pthread_descr self = thread_self();
167 THREAD_SETMEM_NC(self, p_libc_specific[key], (void *) pointer);
168 return 0;
170 int (*__libc_internal_tsd_set)(enum __libc_tsd_key_t key, const void * pointer)
171 = libc_internal_tsd_set;
173 static void *
174 libc_internal_tsd_get(enum __libc_tsd_key_t key)
176 pthread_descr self = thread_self();
178 return THREAD_GETMEM_NC(self, p_libc_specific[key]);
180 void * (*__libc_internal_tsd_get)(enum __libc_tsd_key_t key)
181 = libc_internal_tsd_get;