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 (THREAD_GETMEM_NC(self
, p_specific
[idx1st
]) == NULL
) {
103 void *newp
= calloc(PTHREAD_KEY_2NDLEVEL_SIZE
, sizeof (void *));
106 THREAD_SETMEM_NC(self
, p_specific
[idx1st
], newp
);
108 THREAD_GETMEM_NC(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 (THREAD_GETMEM_NC(self
, p_specific
[idx1st
]) == NULL
125 || !pthread_keys
[key
].in_use
)
127 return THREAD_GETMEM_NC(self
, p_specific
[idx1st
])[idx2nd
];
129 weak_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
;
140 for (round
= 0, found_nonzero
= 1;
141 found_nonzero
&& round
< PTHREAD_DESTRUCTOR_ITERATIONS
;
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
;
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 int __libc_internal_tsd_set(enum __libc_tsd_key_t key
, const void * pointer
)
166 pthread_descr self
= thread_self();
168 THREAD_SETMEM_NC(self
, p_libc_specific
[key
], (void *) pointer
);
172 void * __libc_internal_tsd_get(enum __libc_tsd_key_t key
)
174 pthread_descr self
= thread_self();
176 return THREAD_GETMEM_NC(self
, p_libc_specific
[key
]);