blob: 65e087902ab6889bd0cea79e46e55f497c7f6955 [file] [log] [blame]
Elliott Hughes44b53ad2013-02-11 20:18:47 +00001/*
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 Albert75ef63d2014-11-21 00:18:07 -080029#include <errno.h>
Elliott Hughes44b53ad2013-02-11 20:18:47 +000030#include <pthread.h>
Yabin Cui5e2bd712015-02-20 16:15:33 -080031#include <stdatomic.h>
Elliott Hughes44b53ad2013-02-11 20:18:47 +000032
Elliott Hugheseb847bc2013-10-09 15:50:50 -070033#include "private/bionic_tls.h"
Elliott Hughes44b53ad2013-02-11 20:18:47 +000034#include "pthread_internal.h"
Elliott Hughes44b53ad2013-02-11 20:18:47 +000035
Elliott Hughes44b53ad2013-02-11 20:18:47 +000036typedef void (*key_destructor_t)(void*);
37
Yabin Cui5e2bd712015-02-20 16:15:33 -080038#define SEQ_KEY_IN_USE_BIT 0
Elliott Hughes44b53ad2013-02-11 20:18:47 +000039
Yabin Cui5e2bd712015-02-20 16:15:33 -080040#define SEQ_INCREMENT_STEP (1 << SEQ_KEY_IN_USE_BIT)
Elliott Hughes44b53ad2013-02-11 20:18:47 +000041
Yabin Cui5e2bd712015-02-20 16:15:33 -080042// pthread_key_internal_t records the use of each pthread key slot:
43// seq records the state of the slot.
44// bit 0 is 1 when the key is in use, 0 when it is unused. Each time we create or delete the
45// pthread key in the slot, we increse the seq by 1 (which inverts bit 0). The reason to use
46// a sequence number instead of a boolean value here is that when the key slot is deleted and
47// reused for a new key, pthread_getspecific will not return stale data.
48// key_destructor records the destructor called at thread exit.
49struct pthread_key_internal_t {
50 atomic_uintptr_t seq;
51 atomic_uintptr_t key_destructor;
Elliott Hughes44b53ad2013-02-11 20:18:47 +000052};
53
Yabin Cui5e2bd712015-02-20 16:15:33 -080054static pthread_key_internal_t key_map[BIONIC_PTHREAD_KEY_COUNT];
Elliott Hughes44b53ad2013-02-11 20:18:47 +000055
Yabin Cui5e2bd712015-02-20 16:15:33 -080056static inline bool SeqOfKeyInUse(uintptr_t seq) {
57 return seq & (1 << SEQ_KEY_IN_USE_BIT);
58}
Elliott Hughes44b53ad2013-02-11 20:18:47 +000059
Yabin Cui5e2bd712015-02-20 16:15:33 -080060static inline bool KeyInValidRange(pthread_key_t key) {
61 return key >= 0 && key < BIONIC_PTHREAD_KEY_COUNT;
62}
Elliott Hughes44b53ad2013-02-11 20:18:47 +000063
Yabin Cui5e2bd712015-02-20 16:15:33 -080064// Called from pthread_exit() to remove all pthread keys. This must call the destructor of
65// all keys that have a non-NULL data value and a non-NULL destructor.
Elliott Hughes44b53ad2013-02-11 20:18:47 +000066__LIBC_HIDDEN__ void pthread_key_clean_all() {
Yabin Cui5e2bd712015-02-20 16:15:33 -080067 // Because destructors can do funky things like deleting/creating other keys,
68 // we need to implement this in a loop.
69 pthread_key_data_t* key_data = __get_thread()->key_data;
70 for (size_t rounds = PTHREAD_DESTRUCTOR_ITERATIONS; rounds > 0; --rounds) {
71 size_t called_destructor_count = 0;
72 for (size_t i = 0; i < BIONIC_PTHREAD_KEY_COUNT; ++i) {
73 uintptr_t seq = atomic_load_explicit(&key_map[i].seq, memory_order_relaxed);
74 if (SeqOfKeyInUse(seq) && seq == key_data[i].seq && key_data[i].data != NULL) {
75 // Other threads may be calling pthread_key_delete/pthread_key_create while current thread
76 // is exiting. So we need to ensure we read the right key_destructor.
77 // We can rely on a user-established happens-before relationship between the creation and
78 // use of pthread key to ensure that we're not getting an earlier key_destructor.
79 // To avoid using the key_destructor of the newly created key in the same slot, we need to
80 // recheck the sequence number after reading key_destructor. As a result, we either see the
81 // right key_destructor, or the sequence number must have changed when we reread it below.
82 key_destructor_t key_destructor = reinterpret_cast<key_destructor_t>(
83 atomic_load_explicit(&key_map[i].key_destructor, memory_order_relaxed));
84 if (key_destructor == NULL) {
85 continue;
86 }
87 atomic_thread_fence(memory_order_acquire);
88 if (atomic_load_explicit(&key_map[i].seq, memory_order_relaxed) != seq) {
89 continue;
90 }
91
92 // We need to clear the key data now, this will prevent the destructor (or a later one)
93 // from seeing the old value if it calls pthread_getspecific().
94 // We don't do this if 'key_destructor == NULL' just in case another destructor
95 // function is responsible for manually releasing the corresponding data.
96 void* data = key_data[i].data;
97 key_data[i].data = NULL;
98
99 (*key_destructor)(data);
100 ++called_destructor_count;
101 }
102 }
103
104 // If we didn't call any destructors, there is no need to check the pthread keys again.
105 if (called_destructor_count == 0) {
106 break;
107 }
108 }
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000109}
110
111int pthread_key_create(pthread_key_t* key, void (*key_destructor)(void*)) {
Yabin Cui5e2bd712015-02-20 16:15:33 -0800112 for (size_t i = 0; i < BIONIC_PTHREAD_KEY_COUNT; ++i) {
113 uintptr_t seq = atomic_load_explicit(&key_map[i].seq, memory_order_relaxed);
114 while (!SeqOfKeyInUse(seq)) {
115 if (atomic_compare_exchange_weak(&key_map[i].seq, &seq, seq + SEQ_INCREMENT_STEP)) {
116 atomic_store(&key_map[i].key_destructor, reinterpret_cast<uintptr_t>(key_destructor));
117 *key = i;
118 return 0;
119 }
120 }
121 }
122 return EAGAIN;
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000123}
124
125// Deletes a pthread_key_t. note that the standard mandates that this does
126// not call the destructors for non-NULL key values. Instead, it is the
127// responsibility of the caller to properly dispose of the corresponding data
128// and resources, using any means it finds suitable.
129int pthread_key_delete(pthread_key_t key) {
Yabin Cui5e2bd712015-02-20 16:15:33 -0800130 if (!KeyInValidRange(key)) {
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000131 return EINVAL;
132 }
Yabin Cui5e2bd712015-02-20 16:15:33 -0800133 // Increase seq to invalidate values in all threads.
134 uintptr_t seq = atomic_load_explicit(&key_map[key].seq, memory_order_relaxed);
135 if (SeqOfKeyInUse(seq)) {
136 if (atomic_compare_exchange_strong(&key_map[key].seq, &seq, seq + SEQ_INCREMENT_STEP)) {
137 return 0;
138 }
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000139 }
Yabin Cui5e2bd712015-02-20 16:15:33 -0800140 return EINVAL;
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000141}
142
143void* pthread_getspecific(pthread_key_t key) {
Yabin Cui5e2bd712015-02-20 16:15:33 -0800144 if (!KeyInValidRange(key)) {
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000145 return NULL;
146 }
Yabin Cui5e2bd712015-02-20 16:15:33 -0800147 uintptr_t seq = atomic_load_explicit(&key_map[key].seq, memory_order_relaxed);
148 pthread_key_data_t* data = &(__get_thread()->key_data[key]);
149 // It is user's responsibility to synchornize between the creation and use of pthread keys,
150 // so we use memory_order_relaxed when checking the sequence number.
151 if (__predict_true(SeqOfKeyInUse(seq) && data->seq == seq)) {
152 return data->data;
153 }
154 data->data = NULL;
155 return NULL;
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000156}
157
158int pthread_setspecific(pthread_key_t key, const void* ptr) {
Yabin Cui5e2bd712015-02-20 16:15:33 -0800159 if (!KeyInValidRange(key)) {
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000160 return EINVAL;
161 }
Yabin Cui5e2bd712015-02-20 16:15:33 -0800162 uintptr_t seq = atomic_load_explicit(&key_map[key].seq, memory_order_relaxed);
163 if (SeqOfKeyInUse(seq)) {
164 pthread_key_data_t* data = &(__get_thread()->key_data[key]);
165 data->seq = seq;
166 data->data = const_cast<void*>(ptr);
167 return 0;
168 }
169 return EINVAL;
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000170}