• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
29 #include <errno.h>
30 #include <pthread.h>
31 #include <stdatomic.h>
32 
33 #include "private/bionic_defs.h"
34 #include "private/bionic_tls.h"
35 #include "pthread_internal.h"
36 
37 typedef void (*key_destructor_t)(void*);
38 
39 #define SEQ_KEY_IN_USE_BIT     0
40 
41 #define SEQ_INCREMENT_STEP  (1 << SEQ_KEY_IN_USE_BIT)
42 
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;
53 };
54 
55 static pthread_key_internal_t key_map[BIONIC_PTHREAD_KEY_COUNT];
56 
SeqOfKeyInUse(uintptr_t seq)57 static inline bool SeqOfKeyInUse(uintptr_t seq) {
58   return seq & (1 << SEQ_KEY_IN_USE_BIT);
59 }
60 
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 
KeyInValidRange(pthread_key_t key)66 static inline bool KeyInValidRange(pthread_key_t key) {
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));
70 }
71 
get_thread_key_data()72 static inline pthread_key_data_t* get_thread_key_data() {
73   return __get_bionic_tls().key_data;
74 }
75 
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.
pthread_key_clean_all()78 __LIBC_HIDDEN__ void pthread_key_clean_all() {
79   // Because destructors can do funky things like deleting/creating other keys,
80   // we need to implement this in a loop.
81   pthread_key_data_t* key_data = get_thread_key_data();
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);
86       if (SeqOfKeyInUse(seq) && seq == key_data[i].seq) {
87         // POSIX explicitly says that the destructor is only called if the
88         // thread has a non-null value for the key.
89         if (key_data[i].data == nullptr) {
90           continue;
91         }
92 
93         // Other threads can call pthread_key_delete()/pthread_key_create()
94         // while this thread is exiting, so we need to ensure we read the right
95         // key_destructor.
96         // We can rely on a user-established happens-before relationship between the creation and
97         // use of a pthread key to ensure that we're not getting an earlier key_destructor.
98         // To avoid using the key_destructor of the newly created key in the same slot, we need to
99         // recheck the sequence number after reading key_destructor. As a result, we either see the
100         // right key_destructor, or the sequence number must have changed when we reread it below.
101         key_destructor_t key_destructor = reinterpret_cast<key_destructor_t>(
102           atomic_load_explicit(&key_map[i].key_destructor, memory_order_relaxed));
103         if (key_destructor == nullptr) {
104           continue;
105         }
106         atomic_thread_fence(memory_order_acquire);
107         if (atomic_load_explicit(&key_map[i].seq, memory_order_relaxed) != seq) {
108            continue;
109         }
110 
111         // We need to clear the key data now, this will prevent the destructor (or a later one)
112         // from seeing the old value if it calls pthread_getspecific().
113         // We don't do this if 'key_destructor == NULL' just in case another destructor
114         // function is responsible for manually releasing the corresponding data.
115         void* data = key_data[i].data;
116         key_data[i].data = nullptr;
117         (*key_destructor)(data);
118         ++called_destructor_count;
119       }
120     }
121 
122     // If we didn't call any destructors, there is no need to check the pthread keys again.
123     if (called_destructor_count == 0) {
124       break;
125     }
126   }
127 }
128 
129 __BIONIC_WEAK_FOR_NATIVE_BRIDGE
pthread_key_create(pthread_key_t * key,void (* key_destructor)(void *))130 int pthread_key_create(pthread_key_t* key, void (*key_destructor)(void*)) {
131   for (size_t i = 0; i < BIONIC_PTHREAD_KEY_COUNT; ++i) {
132     uintptr_t seq = atomic_load_explicit(&key_map[i].seq, memory_order_relaxed);
133     while (!SeqOfKeyInUse(seq)) {
134       if (atomic_compare_exchange_weak(&key_map[i].seq, &seq, seq + SEQ_INCREMENT_STEP)) {
135         atomic_store(&key_map[i].key_destructor, reinterpret_cast<uintptr_t>(key_destructor));
136         *key = i | KEY_VALID_FLAG;
137         return 0;
138       }
139     }
140   }
141   return EAGAIN;
142 }
143 
144 // Deletes a pthread_key_t. note that the standard mandates that this does
145 // not call the destructors for non-NULL key values. Instead, it is the
146 // responsibility of the caller to properly dispose of the corresponding data
147 // and resources, using any means it finds suitable.
148 __BIONIC_WEAK_FOR_NATIVE_BRIDGE
pthread_key_delete(pthread_key_t key)149 int pthread_key_delete(pthread_key_t key) {
150   if (__predict_false(!KeyInValidRange(key))) {
151     return EINVAL;
152   }
153   key &= ~KEY_VALID_FLAG;
154   // Increase seq to invalidate values in all threads.
155   uintptr_t seq = atomic_load_explicit(&key_map[key].seq, memory_order_relaxed);
156   if (SeqOfKeyInUse(seq)) {
157     if (atomic_compare_exchange_strong(&key_map[key].seq, &seq, seq + SEQ_INCREMENT_STEP)) {
158       return 0;
159     }
160   }
161   return EINVAL;
162 }
163 
164 __BIONIC_WEAK_FOR_NATIVE_BRIDGE
pthread_getspecific(pthread_key_t key)165 void* pthread_getspecific(pthread_key_t key) {
166   if (__predict_false(!KeyInValidRange(key))) {
167     return nullptr;
168   }
169   key &= ~KEY_VALID_FLAG;
170   uintptr_t seq = atomic_load_explicit(&key_map[key].seq, memory_order_relaxed);
171   pthread_key_data_t* data = &get_thread_key_data()[key];
172   // It is the user's responsibility to synchronize between the creation and use of pthread keys,
173   // so we use memory_order_relaxed when checking the sequence number.
174   if (__predict_true(SeqOfKeyInUse(seq) && data->seq == seq)) {
175     return data->data;
176   }
177   // We arrive here when the current thread holds the seq of a deleted pthread key.
178   // The data is for the deleted pthread key, and should be cleared.
179   data->data = nullptr;
180   return nullptr;
181 }
182 
183 __BIONIC_WEAK_FOR_NATIVE_BRIDGE
pthread_setspecific(pthread_key_t key,const void * ptr)184 int pthread_setspecific(pthread_key_t key, const void* ptr) {
185   if (__predict_false(!KeyInValidRange(key))) {
186     return EINVAL;
187   }
188   key &= ~KEY_VALID_FLAG;
189   uintptr_t seq = atomic_load_explicit(&key_map[key].seq, memory_order_relaxed);
190   if (__predict_true(SeqOfKeyInUse(seq))) {
191     pthread_key_data_t* data = &get_thread_key_data()[key];
192     data->seq = seq;
193     data->data = const_cast<void*>(ptr);
194     return 0;
195   }
196   return EINVAL;
197 }
198