| 1 | /* Deallocation thread-specific data structures related to pthread_key_create. |
| 2 | This file is part of the GNU C Library. |
| 3 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2002. |
| 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 <pthreadP.h> |
| 20 | |
| 21 | /* Deallocate POSIX thread-local-storage. */ |
| 22 | void |
| 23 | __nptl_deallocate_tsd (void) |
| 24 | { |
| 25 | struct pthread *self = THREAD_SELF; |
| 26 | |
| 27 | /* Maybe no data was ever allocated. This happens often so we have |
| 28 | a flag for this. */ |
| 29 | if (THREAD_GETMEM (self, specific_used)) |
| 30 | { |
| 31 | size_t round; |
| 32 | size_t cnt; |
| 33 | |
| 34 | round = 0; |
| 35 | do |
| 36 | { |
| 37 | size_t idx; |
| 38 | |
| 39 | /* So far no new nonzero data entry. */ |
| 40 | THREAD_SETMEM (self, specific_used, false); |
| 41 | |
| 42 | for (cnt = idx = 0; cnt < PTHREAD_KEY_1STLEVEL_SIZE; ++cnt) |
| 43 | { |
| 44 | struct pthread_key_data *level2; |
| 45 | |
| 46 | level2 = THREAD_GETMEM_NC (self, specific, cnt); |
| 47 | |
| 48 | if (level2 != NULL) |
| 49 | { |
| 50 | size_t inner; |
| 51 | |
| 52 | for (inner = 0; inner < PTHREAD_KEY_2NDLEVEL_SIZE; |
| 53 | ++inner, ++idx) |
| 54 | { |
| 55 | void *data = level2[inner].data; |
| 56 | |
| 57 | if (data != NULL) |
| 58 | { |
| 59 | /* Always clear the data. */ |
| 60 | level2[inner].data = NULL; |
| 61 | |
| 62 | /* Make sure the data corresponds to a valid |
| 63 | key. This test fails if the key was |
| 64 | deallocated and also if it was |
| 65 | re-allocated. It is the user's |
| 66 | responsibility to free the memory in this |
| 67 | case. */ |
| 68 | if (level2[inner].seq |
| 69 | == __pthread_keys[idx].seq |
| 70 | /* It is not necessary to register a destructor |
| 71 | function. */ |
| 72 | && __pthread_keys[idx].destr != NULL) |
| 73 | /* Call the user-provided destructor. */ |
| 74 | __pthread_keys[idx].destr (data); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | else |
| 79 | idx += PTHREAD_KEY_1STLEVEL_SIZE; |
| 80 | } |
| 81 | |
| 82 | if (THREAD_GETMEM (self, specific_used) == 0) |
| 83 | /* No data has been modified. */ |
| 84 | goto just_free; |
| 85 | } |
| 86 | /* We only repeat the process a fixed number of times. */ |
| 87 | while (__builtin_expect (++round < PTHREAD_DESTRUCTOR_ITERATIONS, 0)); |
| 88 | |
| 89 | /* Just clear the memory of the first block for reuse. */ |
| 90 | memset (&THREAD_SELF->specific_1stblock, '\0', |
| 91 | sizeof (self->specific_1stblock)); |
| 92 | |
| 93 | just_free: |
| 94 | /* Free the memory for the other blocks. */ |
| 95 | for (cnt = 1; cnt < PTHREAD_KEY_1STLEVEL_SIZE; ++cnt) |
| 96 | { |
| 97 | struct pthread_key_data *level2; |
| 98 | |
| 99 | level2 = THREAD_GETMEM_NC (self, specific, cnt); |
| 100 | if (level2 != NULL) |
| 101 | { |
| 102 | /* The first block is allocated as part of the thread |
| 103 | descriptor. */ |
| 104 | free (level2); |
| 105 | THREAD_SETMEM_NC (self, specific, cnt, NULL); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | THREAD_SETMEM (self, specific_used, false); |
| 110 | } |
| 111 | } |
| 112 | libc_hidden_def (__nptl_deallocate_tsd) |
| 113 | |