(__pthread_lock): Force lock->__status to be read from memory on every spin.
[glibc.git] / linuxthreads / specific.c
bloba7fa8ff5b7d5f926ad3828dbd06ecda5fb835e84
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"
22 #include "spinlock.h"
23 #include <bits/libc-lock.h>
26 /* Table of keys. */
28 static struct pthread_key_struct pthread_keys[PTHREAD_KEYS_MAX] =
29 { { 0, NULL } };
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;
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
77 that 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 the thread already is terminated don't modify the memory. */
84 if (!th->p_terminated) {
85 /* pthread_exit() may try to free th->p_specific[idx1st] concurrently. */
86 __pthread_lock(THREAD_GETMEM(th, p_lock), self);
87 if (th->p_specific[idx1st] != NULL)
88 th->p_specific[idx1st][idx2nd] = NULL;
89 __pthread_unlock(THREAD_GETMEM(th, p_lock));
91 th = th->p_nextlive;
92 } while (th != self);
93 pthread_mutex_unlock(&pthread_keys_mutex);
94 return 0;
97 /* Set the value of a key */
99 int __pthread_setspecific(pthread_key_t key, const void * pointer)
101 pthread_descr self = thread_self();
102 unsigned int idx1st, idx2nd;
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);
114 THREAD_GETMEM_NC(self, p_specific[idx1st])[idx2nd] = (void *) pointer;
115 return 0;
117 strong_alias (__pthread_setspecific, pthread_setspecific)
119 /* Get the value of a key */
121 void * __pthread_getspecific(pthread_key_t key)
123 pthread_descr self = thread_self();
124 unsigned int idx1st, idx2nd;
126 if (key >= PTHREAD_KEYS_MAX)
127 return NULL;
128 idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
129 idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
130 if (THREAD_GETMEM_NC(self, p_specific[idx1st]) == NULL
131 || !pthread_keys[key].in_use)
132 return NULL;
133 return THREAD_GETMEM_NC(self, p_specific[idx1st])[idx2nd];
135 strong_alias (__pthread_getspecific, pthread_getspecific)
137 /* Call the destruction routines on all keys */
139 void __pthread_destroy_specifics()
141 pthread_descr self = thread_self();
142 int i, j, round, found_nonzero;
143 destr_function destr;
144 void * data;
146 for (round = 0, found_nonzero = 1;
147 found_nonzero && round < PTHREAD_DESTRUCTOR_ITERATIONS;
148 round++) {
149 found_nonzero = 0;
150 for (i = 0; i < PTHREAD_KEY_1STLEVEL_SIZE; i++)
151 if (THREAD_GETMEM_NC(self, p_specific[i]) != NULL)
152 for (j = 0; j < PTHREAD_KEY_2NDLEVEL_SIZE; j++) {
153 destr = pthread_keys[i * PTHREAD_KEY_2NDLEVEL_SIZE + j].destr;
154 data = THREAD_GETMEM_NC(self, p_specific[i])[j];
155 if (destr != NULL && data != NULL) {
156 THREAD_GETMEM_NC(self, p_specific[i])[j] = NULL;
157 destr(data);
158 found_nonzero = 1;
162 __pthread_lock(THREAD_GETMEM(self, p_lock), self);
163 for (i = 0; i < PTHREAD_KEY_1STLEVEL_SIZE; i++) {
164 if (THREAD_GETMEM_NC(self, p_specific[i]) != NULL) {
165 free(THREAD_GETMEM_NC(self, p_specific[i]));
166 THREAD_SETMEM_NC(self, p_specific[i], NULL);
169 __pthread_unlock(THREAD_GETMEM(self, p_lock));
172 /* Thread-specific data for libc. */
174 static int
175 libc_internal_tsd_set(enum __libc_tsd_key_t key, const void * pointer)
177 pthread_descr self = thread_self();
179 THREAD_SETMEM_NC(self, p_libc_specific[key], (void *) pointer);
180 return 0;
182 int (*__libc_internal_tsd_set)(enum __libc_tsd_key_t key, const void * pointer)
183 = libc_internal_tsd_set;
185 static void *
186 libc_internal_tsd_get(enum __libc_tsd_key_t key)
188 pthread_descr self = thread_self();
190 return THREAD_GETMEM_NC(self, p_libc_specific[key]);
192 void * (*__libc_internal_tsd_get)(enum __libc_tsd_key_t key)
193 = libc_internal_tsd_get;