1 /* pthread_key internal declatations for the Hurd version.
2 Copyright (C) 2002-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 #include <pthread.h>
20 #include <libc-lockP.h>
21 #include <pthreadP.h>
22
23 #define PTHREAD_KEY_MEMBERS \
24 void **thread_specifics; /* This is only resized by the thread, and always growing */ \
25 unsigned thread_specifics_size; /* Number of entries in thread_specifics */
26
27 #define PTHREAD_KEY_INVALID (void *) (-1)
28
29
30 /* __PTHREAD_KEY_DESTRUCTORS is an array of destructors with
31 __PTHREAD_KEY_SIZE elements. If an element with index less than
32 __PTHREAD_KEY_COUNT is invalid, it shall contain the value
33 PTHREAD_KEY_INVALID which shall be distinct from NULL.
34
35 Normally, we just add new keys to the end of the array and realloc
36 it as necessary. The pthread_key_create routine may decide to
37 rescan the array if __PTHREAD_KEY_FREE is large. */
38 extern void (**__pthread_key_destructors) (void *arg);
39 extern int __pthread_key_size;
40 extern int __pthread_key_count;
41 /* Number of invalid elements in the array. Does not include elements
42 for which memory has been allocated but which have not yet been
43 used (i.e. those elements with indexes greater than
44 __PTHREAD_KEY_COUNT). */
45 extern int __pthread_key_invalid_count;
46
47 /* Protects the above variables. This must be a recursive lock: the
48 destructors may call pthread_key_delete. */
49 extern pthread_mutex_t __pthread_key_lock;
50
51 /* Protects the initialization of the mutex above. */
52 extern pthread_once_t __pthread_key_once;
53
54 #include <assert.h>
55
56 static inline void
__pthread_key_lock_ready(void)57 __pthread_key_lock_ready (void)
58 {
59 void do_init (void)
60 {
61 int err;
62 pthread_mutexattr_t attr;
63
64 err = __pthread_mutexattr_init (&attr);
65 assert_perror (err);
66
67 err = __pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
68 assert_perror (err);
69
70 err = __pthread_mutex_init (&__pthread_key_lock, &attr);
71 assert_perror (err);
72
73 err = __pthread_mutexattr_destroy (&attr);
74 assert_perror (err);
75 }
76
77 __pthread_once (&__pthread_key_once, do_init);
78 }
79