Update.
[glibc.git] / linuxthreads / specific.c
blob674353dd3968e8bdddae0fcd0cb96429612c6575
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 typedef void (*destr_function)(void *);
25 /* Table of keys. */
27 struct pthread_key_struct {
28 int in_use; /* already allocated? */
29 destr_function destr; /* destruction routine */
32 static struct pthread_key_struct pthread_keys[PTHREAD_KEYS_MAX] =
33 { { 0, NULL } };
35 /* Mutex to protect access to pthread_keys */
37 static pthread_mutex_t pthread_keys_mutex = PTHREAD_MUTEX_INITIALIZER;
39 /* Create a new key */
41 int __pthread_key_create(pthread_key_t * key, destr_function destr)
43 int i;
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;
56 pthread_mutex_unlock(&pthread_keys_mutex);
57 return EAGAIN;
59 strong_alias (__pthread_key_create, pthread_key_create)
61 /* Delete a key */
63 int pthread_key_delete(pthread_key_t key)
65 pthread_descr self = thread_self();
66 pthread_descr th;
67 unsigned int idx1st, idx2nd;
69 pthread_mutex_lock(&pthread_keys_mutex);
70 if (key >= PTHREAD_KEYS_MAX || !pthread_keys[key].in_use) {
71 pthread_mutex_unlock(&pthread_keys_mutex);
72 return EINVAL;
74 pthread_keys[key].in_use = 0;
75 pthread_keys[key].destr = NULL;
76 /* Set the value of the key to NULL in all running threads, so that
77 if the key is reallocated later by pthread_key_create, its
78 associated values will be NULL in all threads. */
79 idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
80 idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
81 th = self;
82 do {
83 if (th->p_specific[idx1st] != NULL)
84 th->p_specific[idx1st][idx2nd] = NULL;
85 th = th->p_nextlive;
86 } while (th != self);
87 pthread_mutex_unlock(&pthread_keys_mutex);
88 return 0;
91 /* Set the value of a key */
93 int __pthread_setspecific(pthread_key_t key, const void * pointer)
95 pthread_descr self = thread_self();
96 unsigned int idx1st, idx2nd;
98 if (key >= PTHREAD_KEYS_MAX || !pthread_keys[key].in_use)
99 return EINVAL;
100 idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
101 idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
102 if (THREAD_GETMEM_NC(self, p_specific[idx1st]) == NULL) {
103 void *newp = calloc(PTHREAD_KEY_2NDLEVEL_SIZE, sizeof (void *));
104 if (newp == NULL)
105 return ENOMEM;
106 THREAD_SETMEM_NC(self, p_specific[idx1st], newp);
108 THREAD_GETMEM_NC(self, p_specific[idx1st])[idx2nd] = (void *) pointer;
109 return 0;
111 strong_alias (__pthread_setspecific, pthread_setspecific)
113 /* Get the value of a key */
115 void * __pthread_getspecific(pthread_key_t key)
117 pthread_descr self = thread_self();
118 unsigned int idx1st, idx2nd;
120 if (key >= PTHREAD_KEYS_MAX)
121 return NULL;
122 idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
123 idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
124 if (THREAD_GETMEM_NC(self, p_specific[idx1st]) == NULL
125 || !pthread_keys[key].in_use)
126 return NULL;
127 return THREAD_GETMEM_NC(self, p_specific[idx1st])[idx2nd];
129 strong_alias (__pthread_getspecific, pthread_getspecific)
131 /* Call the destruction routines on all keys */
133 void __pthread_destroy_specifics()
135 pthread_descr self = thread_self();
136 int i, j, round, found_nonzero;
137 destr_function destr;
138 void * data;
140 for (round = 0, found_nonzero = 1;
141 found_nonzero && round < PTHREAD_DESTRUCTOR_ITERATIONS;
142 round++) {
143 found_nonzero = 0;
144 for (i = 0; i < PTHREAD_KEY_1STLEVEL_SIZE; i++)
145 if (THREAD_GETMEM_NC(self, p_specific[i]) != NULL)
146 for (j = 0; j < PTHREAD_KEY_2NDLEVEL_SIZE; j++) {
147 destr = pthread_keys[i * PTHREAD_KEY_2NDLEVEL_SIZE + j].destr;
148 data = THREAD_GETMEM_NC(self, p_specific[i])[j];
149 if (destr != NULL && data != NULL) {
150 THREAD_GETMEM_NC(self, p_specific[i])[j] = NULL;
151 destr(data);
152 found_nonzero = 1;
156 for (i = 0; i < PTHREAD_KEY_1STLEVEL_SIZE; i++) {
157 if (THREAD_GETMEM_NC(self, p_specific[i]) != NULL)
158 free(THREAD_GETMEM_NC(self, p_specific[i]));
162 /* Thread-specific data for libc. */
164 static int
165 libc_internal_tsd_set(enum __libc_tsd_key_t key, const void * pointer)
167 pthread_descr self = thread_self();
169 THREAD_SETMEM_NC(self, p_libc_specific[key], (void *) pointer);
170 return 0;
172 int (*__libc_internal_tsd_set)(enum __libc_tsd_key_t key, const void * pointer)
173 = libc_internal_tsd_set;
175 static void *
176 libc_internal_tsd_get(enum __libc_tsd_key_t key)
178 pthread_descr self = thread_self();
180 return THREAD_GETMEM_NC(self, p_libc_specific[key]);
182 void * (*__libc_internal_tsd_get)(enum __libc_tsd_key_t key)
183 = libc_internal_tsd_get;