nptl: remove asm from sysdep-cancel.h
[uclibc-ng.git] / libpthread / linuxthreads / specific.c
blobd0be8a9f0fcf523c0864358440f2375226644f82
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 <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"
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;
60 /* Delete a key */
61 int pthread_key_delete(pthread_key_t key)
63 pthread_descr self = thread_self();
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;
70 pthread_keys[key].in_use = 0;
71 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 Do nothing if no threads have been created yet. */
77 if (__pthread_manager_request != -1)
79 pthread_descr th;
80 unsigned int idx1st, idx2nd;
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);
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;
118 /* Get the value of a key */
120 void * pthread_getspecific(pthread_key_t key)
122 pthread_descr self = thread_self();
123 unsigned int idx1st, idx2nd;
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];
135 /* Call the destruction routines on all keys */
137 void __pthread_destroy_specifics(void)
139 pthread_descr self = thread_self();
140 int i, j, round, found_nonzero;
141 destr_function destr;
142 void * data;
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;
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);
167 __pthread_unlock(THREAD_GETMEM(self, p_lock));