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 typedef void (*destr_function
)(void *);
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
] =
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 weak_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 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
;
83 if (th
->p_specific
[idx1st
] != NULL
)
84 th
->p_specific
[idx1st
][idx2nd
] = NULL
;
87 pthread_mutex_unlock(&pthread_keys_mutex
);
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
)
100 idx1st
= key
/ PTHREAD_KEY_2NDLEVEL_SIZE
;
101 idx2nd
= key
% PTHREAD_KEY_2NDLEVEL_SIZE
;
102 if (self
->p_specific
[idx1st
] == NULL
) {
103 self
->p_specific
[idx1st
] =
104 calloc(PTHREAD_KEY_2NDLEVEL_SIZE
, sizeof (void *));
105 if (self
->p_specific
[idx1st
] == NULL
)
108 self
->p_specific
[idx1st
][idx2nd
] = (void *) pointer
;
111 weak_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
)
122 idx1st
= key
/ PTHREAD_KEY_2NDLEVEL_SIZE
;
123 idx2nd
= key
% PTHREAD_KEY_2NDLEVEL_SIZE
;
124 if (self
->p_specific
[idx1st
] == NULL
|| !pthread_keys
[key
].in_use
)
126 return self
->p_specific
[idx1st
][idx2nd
];
128 weak_alias (__pthread_getspecific
, pthread_getspecific
)
130 /* Call the destruction routines on all keys */
132 void __pthread_destroy_specifics()
134 pthread_descr self
= thread_self();
135 int i
, j
, round
, found_nonzero
;
136 destr_function destr
;
139 for (round
= 0, found_nonzero
= 1;
140 found_nonzero
&& round
< PTHREAD_DESTRUCTOR_ITERATIONS
;
143 for (i
= 0; i
< PTHREAD_KEY_1STLEVEL_SIZE
; i
++)
144 if (self
->p_specific
[i
] != NULL
)
145 for (j
= 0; j
< PTHREAD_KEY_2NDLEVEL_SIZE
; j
++) {
146 destr
= pthread_keys
[i
* PTHREAD_KEY_2NDLEVEL_SIZE
+ j
].destr
;
147 data
= self
->p_specific
[i
][j
];
148 if (destr
!= NULL
&& data
!= NULL
) {
149 self
->p_specific
[i
][j
] = NULL
;
155 for (i
= 0; i
< PTHREAD_KEY_1STLEVEL_SIZE
; i
++) {
156 if (self
->p_specific
[i
] != NULL
) free(self
->p_specific
[i
]);
160 /* Thread-specific data for libc. */
162 int __libc_internal_tsd_set(enum __libc_tsd_key_t key
, const void * pointer
)
164 pthread_descr self
= thread_self();
166 self
->p_libc_specific
[key
] = (void *) pointer
;
170 void * __libc_internal_tsd_get(enum __libc_tsd_key_t key
)
172 pthread_descr self
= thread_self();
174 return self
->p_libc_specific
[key
];