1 | /* __pthread_destory_specific. Hurd version. |
2 | Copyright (C) 2002-2024 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 <stdlib.h> |
21 | |
22 | #include <pt-internal.h> |
23 | |
24 | void |
25 | __pthread_destroy_specific (struct __pthread *thread) |
26 | { |
27 | int i; |
28 | int seen_one; |
29 | |
30 | /* Check if there is any thread specific data. */ |
31 | if (thread->thread_specifics == NULL) |
32 | { |
33 | for (i = 0; i < PTHREAD_STATIC_KEYS; i++) |
34 | { |
35 | if (thread->static_thread_specifics[i] != NULL) |
36 | break; |
37 | } |
38 | |
39 | if (i == PTHREAD_STATIC_KEYS) |
40 | return; |
41 | } |
42 | |
43 | __pthread_key_lock_ready (); |
44 | |
45 | /* Iterate and call the destructors on any thread specific data. */ |
46 | for (;;) |
47 | { |
48 | seen_one = 0; |
49 | |
50 | __pthread_mutex_lock (&__pthread_key_lock); |
51 | |
52 | for (i = 0; i < __pthread_key_count; i++) |
53 | { |
54 | void *value; |
55 | |
56 | if (__pthread_key_destructors[i] == PTHREAD_KEY_INVALID) |
57 | continue; |
58 | |
59 | if (thread->thread_specifics == NULL) |
60 | { |
61 | if (i >= PTHREAD_STATIC_KEYS) |
62 | break; |
63 | value = thread->static_thread_specifics[i]; |
64 | } |
65 | else |
66 | { |
67 | if (i >= thread->thread_specifics_size) |
68 | break; |
69 | value = thread->thread_specifics[i]; |
70 | } |
71 | |
72 | if (value != NULL) |
73 | { |
74 | if (thread->thread_specifics == NULL) |
75 | thread->static_thread_specifics[i] = 0; |
76 | else |
77 | thread->thread_specifics[i] = 0; |
78 | |
79 | if (__pthread_key_destructors[i]) |
80 | { |
81 | seen_one = 1; |
82 | __pthread_key_destructors[i] (value); |
83 | } |
84 | } |
85 | } |
86 | |
87 | __pthread_mutex_unlock (&__pthread_key_lock); |
88 | |
89 | if (!seen_one) |
90 | break; |
91 | |
92 | /* This may take a very long time. Let those blocking on |
93 | pthread_key_create or pthread_key_delete make progress. */ |
94 | __sched_yield (); |
95 | } |
96 | |
97 | free (ptr: thread->thread_specifics); |
98 | thread->thread_specifics = 0; |
99 | thread->thread_specifics_size = 0; |
100 | memset (&thread->static_thread_specifics, 0, |
101 | sizeof (thread->static_thread_specifics)); |
102 | } |
103 | |