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 */
21 #include "internals.h"
23 #include <bits/libc-lock.h>
28 static struct pthread_key_struct pthread_keys
[PTHREAD_KEYS_MAX
] =
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
)
45 pthread_mutex_lock(&pthread_keys_mutex
);
46 for (i
= 0; i
< PTHREAD_KEYS_MAX
; i
++) {
47 if (! pthread_keys
[i
].in_use
) {
49 pthread_keys
[i
].in_use
= 1;
50 pthread_keys
[i
].destr
= destr
;
51 pthread_mutex_unlock(&pthread_keys_mutex
);
56 pthread_mutex_unlock(&pthread_keys_mutex
);
59 strong_alias (__pthread_key_create
, pthread_key_create
)
63 int pthread_key_delete(pthread_key_t key
)
65 pthread_descr self
= thread_self();
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
);
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
;
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
));
93 pthread_mutex_unlock(&pthread_keys_mutex
);
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
)
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 *));
112 THREAD_SETMEM_NC(self
, p_specific
[idx1st
], newp
);
114 THREAD_GETMEM_NC(self
, p_specific
[idx1st
])[idx2nd
] = (void *) pointer
;
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
)
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
)
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
;
146 for (round
= 0, found_nonzero
= 1;
147 found_nonzero
&& round
< PTHREAD_DESTRUCTOR_ITERATIONS
;
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
;
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. */
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
);
182 int (*__libc_internal_tsd_set
)(enum __libc_tsd_key_t key
, const void * pointer
)
183 = libc_internal_tsd_set
;
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
;