| Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2008 The Android Open Source Project | 
|  | 3 | * All rights reserved. | 
|  | 4 | * | 
|  | 5 | * Redistribution and use in source and binary forms, with or without | 
|  | 6 | * modification, are permitted provided that the following conditions | 
|  | 7 | * are met: | 
|  | 8 | *  * Redistributions of source code must retain the above copyright | 
|  | 9 | *    notice, this list of conditions and the following disclaimer. | 
|  | 10 | *  * Redistributions in binary form must reproduce the above copyright | 
|  | 11 | *    notice, this list of conditions and the following disclaimer in | 
|  | 12 | *    the documentation and/or other materials provided with the | 
|  | 13 | *    distribution. | 
|  | 14 | * | 
|  | 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
|  | 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
|  | 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | 
|  | 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | 
|  | 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | 
|  | 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | 
|  | 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | 
|  | 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | 
|  | 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
|  | 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | 
|  | 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 
|  | 26 | * SUCH DAMAGE. | 
|  | 27 | */ | 
|  | 28 |  | 
| Dan Albert | 75ef63d | 2014-11-21 00:18:07 -0800 | [diff] [blame] | 29 | #include <errno.h> | 
| Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 30 | #include <pthread.h> | 
| Yabin Cui | 5e2bd71 | 2015-02-20 16:15:33 -0800 | [diff] [blame] | 31 | #include <stdatomic.h> | 
| Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 32 |  | 
| dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 33 | #include "private/bionic_defs.h" | 
| Elliott Hughes | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame] | 34 | #include "private/bionic_tls.h" | 
| Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 35 | #include "pthread_internal.h" | 
| Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 36 |  | 
| Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 37 | typedef void (*key_destructor_t)(void*); | 
|  | 38 |  | 
| Yabin Cui | 5e2bd71 | 2015-02-20 16:15:33 -0800 | [diff] [blame] | 39 | #define SEQ_KEY_IN_USE_BIT     0 | 
| Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 40 |  | 
| Yabin Cui | 5e2bd71 | 2015-02-20 16:15:33 -0800 | [diff] [blame] | 41 | #define SEQ_INCREMENT_STEP  (1 << SEQ_KEY_IN_USE_BIT) | 
| Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 42 |  | 
| Yabin Cui | 5e2bd71 | 2015-02-20 16:15:33 -0800 | [diff] [blame] | 43 | // pthread_key_internal_t records the use of each pthread key slot: | 
|  | 44 | //   seq records the state of the slot. | 
|  | 45 | //      bit 0 is 1 when the key is in use, 0 when it is unused. Each time we create or delete the | 
|  | 46 | //      pthread key in the slot, we increse the seq by 1 (which inverts bit 0). The reason to use | 
|  | 47 | //      a sequence number instead of a boolean value here is that when the key slot is deleted and | 
|  | 48 | //      reused for a new key, pthread_getspecific will not return stale data. | 
|  | 49 | //   key_destructor records the destructor called at thread exit. | 
|  | 50 | struct pthread_key_internal_t { | 
|  | 51 | atomic_uintptr_t seq; | 
|  | 52 | atomic_uintptr_t key_destructor; | 
| Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 53 | }; | 
|  | 54 |  | 
| Yabin Cui | 5e2bd71 | 2015-02-20 16:15:33 -0800 | [diff] [blame] | 55 | static pthread_key_internal_t key_map[BIONIC_PTHREAD_KEY_COUNT]; | 
| Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 56 |  | 
| Yabin Cui | 5e2bd71 | 2015-02-20 16:15:33 -0800 | [diff] [blame] | 57 | static inline bool SeqOfKeyInUse(uintptr_t seq) { | 
|  | 58 | return seq & (1 << SEQ_KEY_IN_USE_BIT); | 
|  | 59 | } | 
| Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 60 |  | 
| Yabin Cui | 5ddbb3f | 2015-03-05 20:35:32 -0800 | [diff] [blame] | 61 | #define KEY_VALID_FLAG (1 << 31) | 
|  | 62 |  | 
|  | 63 | static_assert(sizeof(pthread_key_t) == sizeof(int) && static_cast<pthread_key_t>(-1) < 0, | 
|  | 64 | "pthread_key_t should be typedef to int"); | 
|  | 65 |  | 
| Yabin Cui | 5e2bd71 | 2015-02-20 16:15:33 -0800 | [diff] [blame] | 66 | static inline bool KeyInValidRange(pthread_key_t key) { | 
| Yabin Cui | 5ddbb3f | 2015-03-05 20:35:32 -0800 | [diff] [blame] | 67 | // key < 0 means bit 31 is set. | 
|  | 68 | // Then key < (2^31 | BIONIC_PTHREAD_KEY_COUNT) means the index part of key < BIONIC_PTHREAD_KEY_COUNT. | 
|  | 69 | return (key < (KEY_VALID_FLAG | BIONIC_PTHREAD_KEY_COUNT)); | 
| Yabin Cui | 5e2bd71 | 2015-02-20 16:15:33 -0800 | [diff] [blame] | 70 | } | 
| Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 71 |  | 
| Ryan Prichard | ecad24f | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 72 | static inline pthread_key_data_t* get_thread_key_data() { | 
| Ryan Prichard | 1e660b7 | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 73 | return __get_bionic_tls().key_data; | 
| Ryan Prichard | ecad24f | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 74 | } | 
|  | 75 |  | 
| Yabin Cui | 5e2bd71 | 2015-02-20 16:15:33 -0800 | [diff] [blame] | 76 | // Called from pthread_exit() to remove all pthread keys. This must call the destructor of | 
|  | 77 | // all keys that have a non-NULL data value and a non-NULL destructor. | 
| Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 78 | __LIBC_HIDDEN__ void pthread_key_clean_all() { | 
| Yabin Cui | 5e2bd71 | 2015-02-20 16:15:33 -0800 | [diff] [blame] | 79 | // Because destructors can do funky things like deleting/creating other keys, | 
|  | 80 | // we need to implement this in a loop. | 
| Ryan Prichard | ecad24f | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 81 | pthread_key_data_t* key_data = get_thread_key_data(); | 
| Yabin Cui | 5e2bd71 | 2015-02-20 16:15:33 -0800 | [diff] [blame] | 82 | for (size_t rounds = PTHREAD_DESTRUCTOR_ITERATIONS; rounds > 0; --rounds) { | 
|  | 83 | size_t called_destructor_count = 0; | 
|  | 84 | for (size_t i = 0; i < BIONIC_PTHREAD_KEY_COUNT; ++i) { | 
|  | 85 | uintptr_t seq = atomic_load_explicit(&key_map[i].seq, memory_order_relaxed); | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 86 | if (SeqOfKeyInUse(seq) && seq == key_data[i].seq && key_data[i].data != nullptr) { | 
| Yabin Cui | 5e2bd71 | 2015-02-20 16:15:33 -0800 | [diff] [blame] | 87 | // Other threads may be calling pthread_key_delete/pthread_key_create while current thread | 
|  | 88 | // is exiting. So we need to ensure we read the right key_destructor. | 
|  | 89 | // We can rely on a user-established happens-before relationship between the creation and | 
|  | 90 | // use of pthread key to ensure that we're not getting an earlier key_destructor. | 
|  | 91 | // To avoid using the key_destructor of the newly created key in the same slot, we need to | 
|  | 92 | // recheck the sequence number after reading key_destructor. As a result, we either see the | 
|  | 93 | // right key_destructor, or the sequence number must have changed when we reread it below. | 
|  | 94 | key_destructor_t key_destructor = reinterpret_cast<key_destructor_t>( | 
|  | 95 | atomic_load_explicit(&key_map[i].key_destructor, memory_order_relaxed)); | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 96 | if (key_destructor == nullptr) { | 
| Yabin Cui | 5e2bd71 | 2015-02-20 16:15:33 -0800 | [diff] [blame] | 97 | continue; | 
|  | 98 | } | 
|  | 99 | atomic_thread_fence(memory_order_acquire); | 
|  | 100 | if (atomic_load_explicit(&key_map[i].seq, memory_order_relaxed) != seq) { | 
|  | 101 | continue; | 
|  | 102 | } | 
|  | 103 |  | 
|  | 104 | // We need to clear the key data now, this will prevent the destructor (or a later one) | 
|  | 105 | // from seeing the old value if it calls pthread_getspecific(). | 
|  | 106 | // We don't do this if 'key_destructor == NULL' just in case another destructor | 
|  | 107 | // function is responsible for manually releasing the corresponding data. | 
|  | 108 | void* data = key_data[i].data; | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 109 | key_data[i].data = nullptr; | 
| Yabin Cui | 5e2bd71 | 2015-02-20 16:15:33 -0800 | [diff] [blame] | 110 |  | 
|  | 111 | (*key_destructor)(data); | 
|  | 112 | ++called_destructor_count; | 
|  | 113 | } | 
|  | 114 | } | 
|  | 115 |  | 
|  | 116 | // If we didn't call any destructors, there is no need to check the pthread keys again. | 
|  | 117 | if (called_destructor_count == 0) { | 
|  | 118 | break; | 
|  | 119 | } | 
|  | 120 | } | 
| Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 121 | } | 
|  | 122 |  | 
| dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 123 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE | 
| Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 124 | int pthread_key_create(pthread_key_t* key, void (*key_destructor)(void*)) { | 
| Yabin Cui | 5e2bd71 | 2015-02-20 16:15:33 -0800 | [diff] [blame] | 125 | for (size_t i = 0; i < BIONIC_PTHREAD_KEY_COUNT; ++i) { | 
|  | 126 | uintptr_t seq = atomic_load_explicit(&key_map[i].seq, memory_order_relaxed); | 
|  | 127 | while (!SeqOfKeyInUse(seq)) { | 
|  | 128 | if (atomic_compare_exchange_weak(&key_map[i].seq, &seq, seq + SEQ_INCREMENT_STEP)) { | 
|  | 129 | atomic_store(&key_map[i].key_destructor, reinterpret_cast<uintptr_t>(key_destructor)); | 
| Yabin Cui | 5ddbb3f | 2015-03-05 20:35:32 -0800 | [diff] [blame] | 130 | *key = i | KEY_VALID_FLAG; | 
| Yabin Cui | 5e2bd71 | 2015-02-20 16:15:33 -0800 | [diff] [blame] | 131 | return 0; | 
|  | 132 | } | 
|  | 133 | } | 
|  | 134 | } | 
|  | 135 | return EAGAIN; | 
| Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 136 | } | 
|  | 137 |  | 
|  | 138 | // Deletes a pthread_key_t. note that the standard mandates that this does | 
|  | 139 | // not call the destructors for non-NULL key values. Instead, it is the | 
|  | 140 | // responsibility of the caller to properly dispose of the corresponding data | 
|  | 141 | // and resources, using any means it finds suitable. | 
| dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 142 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE | 
| Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 143 | int pthread_key_delete(pthread_key_t key) { | 
| Yabin Cui | 5ddbb3f | 2015-03-05 20:35:32 -0800 | [diff] [blame] | 144 | if (__predict_false(!KeyInValidRange(key))) { | 
| Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 145 | return EINVAL; | 
|  | 146 | } | 
| Yabin Cui | 5ddbb3f | 2015-03-05 20:35:32 -0800 | [diff] [blame] | 147 | key &= ~KEY_VALID_FLAG; | 
| Yabin Cui | 5e2bd71 | 2015-02-20 16:15:33 -0800 | [diff] [blame] | 148 | // Increase seq to invalidate values in all threads. | 
|  | 149 | uintptr_t seq = atomic_load_explicit(&key_map[key].seq, memory_order_relaxed); | 
|  | 150 | if (SeqOfKeyInUse(seq)) { | 
|  | 151 | if (atomic_compare_exchange_strong(&key_map[key].seq, &seq, seq + SEQ_INCREMENT_STEP)) { | 
|  | 152 | return 0; | 
|  | 153 | } | 
| Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 154 | } | 
| Yabin Cui | 5e2bd71 | 2015-02-20 16:15:33 -0800 | [diff] [blame] | 155 | return EINVAL; | 
| Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 156 | } | 
|  | 157 |  | 
| dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 158 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE | 
| Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 159 | void* pthread_getspecific(pthread_key_t key) { | 
| Yabin Cui | 5ddbb3f | 2015-03-05 20:35:32 -0800 | [diff] [blame] | 160 | if (__predict_false(!KeyInValidRange(key))) { | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 161 | return nullptr; | 
| Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 162 | } | 
| Yabin Cui | 5ddbb3f | 2015-03-05 20:35:32 -0800 | [diff] [blame] | 163 | key &= ~KEY_VALID_FLAG; | 
| Yabin Cui | 5e2bd71 | 2015-02-20 16:15:33 -0800 | [diff] [blame] | 164 | uintptr_t seq = atomic_load_explicit(&key_map[key].seq, memory_order_relaxed); | 
| Ryan Prichard | ecad24f | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 165 | pthread_key_data_t* data = &get_thread_key_data()[key]; | 
| Yabin Cui | 5e2bd71 | 2015-02-20 16:15:33 -0800 | [diff] [blame] | 166 | // It is user's responsibility to synchornize between the creation and use of pthread keys, | 
|  | 167 | // so we use memory_order_relaxed when checking the sequence number. | 
|  | 168 | if (__predict_true(SeqOfKeyInUse(seq) && data->seq == seq)) { | 
|  | 169 | return data->data; | 
|  | 170 | } | 
| Yabin Cui | 5ddbb3f | 2015-03-05 20:35:32 -0800 | [diff] [blame] | 171 | // We arrive here when current thread holds the seq of an deleted pthread key. So the | 
|  | 172 | // data is for the deleted pthread key, and should be cleared. | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 173 | data->data = nullptr; | 
|  | 174 | return nullptr; | 
| Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 175 | } | 
|  | 176 |  | 
| dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 177 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE | 
| Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 178 | int pthread_setspecific(pthread_key_t key, const void* ptr) { | 
| Yabin Cui | 5ddbb3f | 2015-03-05 20:35:32 -0800 | [diff] [blame] | 179 | if (__predict_false(!KeyInValidRange(key))) { | 
| Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 180 | return EINVAL; | 
|  | 181 | } | 
| Yabin Cui | 5ddbb3f | 2015-03-05 20:35:32 -0800 | [diff] [blame] | 182 | key &= ~KEY_VALID_FLAG; | 
| Yabin Cui | 5e2bd71 | 2015-02-20 16:15:33 -0800 | [diff] [blame] | 183 | uintptr_t seq = atomic_load_explicit(&key_map[key].seq, memory_order_relaxed); | 
| Yabin Cui | 5ddbb3f | 2015-03-05 20:35:32 -0800 | [diff] [blame] | 184 | if (__predict_true(SeqOfKeyInUse(seq))) { | 
| Ryan Prichard | ecad24f | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 185 | pthread_key_data_t* data = &get_thread_key_data()[key]; | 
| Yabin Cui | 5e2bd71 | 2015-02-20 16:15:33 -0800 | [diff] [blame] | 186 | data->seq = seq; | 
|  | 187 | data->data = const_cast<void*>(ptr); | 
|  | 188 | return 0; | 
|  | 189 | } | 
|  | 190 | return EINVAL; | 
| Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 191 | } |