2 * Copyright 2017-present Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <folly/AtomicStruct.h>
19 #include <folly/PackedSyncPtr.h>
20 #include <folly/detail/AtomicUtils.h>
21 #include <folly/experimental/detail/AtomicSharedPtr-detail.h>
26 * This is an implementation of the std::atomic_shared_ptr TS
27 * http://en.cppreference.com/w/cpp/experimental/atomic_shared_ptr
28 * https://isocpp.org/files/papers/N4162.pdf
30 * AFAIK, the only other implementation is Anthony Williams from
31 * Just::thread library:
33 * https://bitbucket.org/anthonyw/atomic_shared_ptr
35 * implementation details:
37 * Basically, three things need to be atomically exchanged to make this work:
39 * * the pointer to the control block
40 * * the aliased pointer, if any.
42 * The Williams version does it with DWcas: 32 bits for local count, 64
43 * bits for control block ptr, and he changes the shared_ptr
44 * implementation to also store the aliased pointers using a linked list
45 * like structure, and provides 32-bit index accessors to them (like
46 * IndexedMemPool trick).
48 * This version instead stores the 48 bits of address, plus 16 bits of
49 * local count in a single 8byte pointer. This avoids 'lock cmpxchg16b',
50 * which is much slower than 'lock xchg' in the normal 'store' case. In
51 * the less-common aliased pointer scenaro, we just allocate it in a new
52 * block, and store a pointer to that instead.
54 * Note that even if we only want to use the 3-bits of pointer alignment,
55 * this trick should still work - Any more than 4 concurrent accesses
56 * will have to go to an external map count instead (slower, but lots of
57 * concurrent access will be slow anyway due to bouncing cachelines).
59 * As a perf optimization, we currently batch up local count and only
60 * move it global every once in a while. This means load() is usually
61 * only a single atomic operation, instead of 3. For this trick to work,
62 * we probably need at least 8 bits to make batching worth it.
65 // A note on noexcept: If the pointer is an aliased pointer,
66 // store() will allocate. Otherwise is noexcept.
71 template <typename> class Atom = std::atomic,
72 typename CountedDetail = detail::shared_ptr_internals>
73 class atomic_shared_ptr {
74 using SharedPtr = typename CountedDetail::template CountedPtr<T>;
75 using BasePtr = typename CountedDetail::counted_base;
76 using PackedPtr = folly::PackedSyncPtr<BasePtr>;
79 atomic_shared_ptr() noexcept {
82 explicit atomic_shared_ptr(SharedPtr foo) /* noexcept */
83 : atomic_shared_ptr() {
86 atomic_shared_ptr(const atomic_shared_ptr<T>&) = delete;
88 ~atomic_shared_ptr() {
89 store(SharedPtr(nullptr));
91 void operator=(SharedPtr desired) /* noexcept */ {
94 void operator=(const atomic_shared_ptr<T>&) = delete;
96 bool is_lock_free() const noexcept {
97 // lock free unless more than EXTERNAL_OFFSET threads are
98 // contending and they all get unlucky and scheduled out during
101 // TODO: Could use a lock-free external map to fix this
106 SharedPtr load(std::memory_order order = std::memory_order_seq_cst) const
108 auto local = takeOwnedBase(order);
110 auto res = get_shared_ptr(local, false);
112 return std::move(res);
114 /* implicit */ operator SharedPtr() const {
120 std::memory_order order = std::memory_order_seq_cst) /* noexcept */ {
121 auto newptr = get_newptr(std::move(n));
122 auto old = ptr_.exchange(newptr, order);
123 release_external(old);
128 std::memory_order order = std::memory_order_seq_cst) /* noexcept */ {
129 auto newptr = get_newptr(std::move(n));
130 auto old = ptr_.exchange(newptr, order);
135 old_ptr = get_shared_ptr(old);
136 release_external(old);
142 bool compare_exchange_weak(
145 std::memory_order mo = std::memory_order_seq_cst) noexcept {
146 return compare_exchange_weak(
147 expected, n, mo, detail::default_failure_memory_order(mo));
149 bool compare_exchange_weak(
152 std::memory_order success,
153 std::memory_order failure) /* noexcept */ {
154 auto newptr = get_newptr(n);
155 PackedPtr oldptr, expectedptr;
157 oldptr = takeOwnedBase(success);
158 if (!owners_eq(oldptr, CountedDetail::get_counted_base(expected))) {
159 expected = get_shared_ptr(oldptr, false);
160 release_external(newptr);
163 expectedptr = oldptr; // Need oldptr to release if failed
164 if (ptr_.compare_exchange_weak(expectedptr, newptr, success, failure)) {
166 release_external(oldptr, -1);
171 expected = get_shared_ptr(oldptr, false);
173 expected = SharedPtr(nullptr);
175 release_external(newptr);
179 bool compare_exchange_weak(
182 std::memory_order mo = std::memory_order_seq_cst) noexcept {
183 return compare_exchange_weak(
184 expected, desired, mo, detail::default_failure_memory_order(mo));
186 bool compare_exchange_weak(
189 std::memory_order success,
190 std::memory_order failure) /* noexcept */ {
191 return compare_exchange_weak(expected, desired, success, failure);
193 bool compare_exchange_strong(
196 std::memory_order mo = std::memory_order_seq_cst) noexcept {
197 return compare_exchange_strong(
198 expected, n, mo, detail::default_failure_memory_order(mo));
200 bool compare_exchange_strong(
203 std::memory_order success,
204 std::memory_order failure) /* noexcept */ {
205 auto local_expected = expected;
207 if (compare_exchange_weak(expected, n, success, failure)) {
210 } while (local_expected == expected);
214 bool compare_exchange_strong(
217 std::memory_order mo = std::memory_order_seq_cst) noexcept {
218 return compare_exchange_strong(
219 expected, desired, mo, detail::default_failure_memory_order(mo));
221 bool compare_exchange_strong(
224 std::memory_order success,
225 std::memory_order failure) /* noexcept */ {
226 return compare_exchange_strong(expected, desired, success, failure);
230 // Matches packed_sync_pointer. Must be > max number of local
231 // counts. This is the max number of threads that can access this
232 // atomic_shared_ptr at once before we start blocking.
233 static constexpr unsigned EXTERNAL_OFFSET{0x2000};
234 // Bit signifying aliased constructor
235 static constexpr unsigned ALIASED_PTR{0x4000};
237 mutable AtomicStruct<PackedPtr, Atom> ptr_;
239 void add_external(BasePtr* res, int64_t c = 0) const {
241 CountedDetail::inc_shared_count(res, EXTERNAL_OFFSET + c);
243 void release_external(PackedPtr& res, int64_t c = 0) const {
247 int64_t count = get_local_count(res) + c;
248 int64_t diff = EXTERNAL_OFFSET - count;
250 CountedDetail::template release_shared<T>(res.get(), diff);
252 PackedPtr get_newptr(const SharedPtr& n) const {
258 newval = CountedDetail::get_counted_base(n);
259 if (n.get() != CountedDetail::template get_shared_ptr<T>(newval)) {
260 // This is an aliased sharedptr. Make an un-aliased one
261 // by wrapping in *another* shared_ptr.
262 auto data = CountedDetail::template make_ptr<SharedPtr>(n);
263 newval = CountedDetail::get_counted_base(data);
265 // (add external must happen before data goes out of scope)
266 add_external(newval);
268 add_external(newval);
273 newptr.init(newval, count);
277 PackedPtr get_newptr(SharedPtr&& n) const {
283 newval = CountedDetail::get_counted_base(n);
284 if (n.get() != CountedDetail::template get_shared_ptr<T>(newval)) {
285 // This is an aliased sharedptr. Make an un-aliased one
286 // by wrapping in *another* shared_ptr.
287 auto data = CountedDetail::template make_ptr<SharedPtr>(std::move(n));
288 newval = CountedDetail::get_counted_base(data);
290 CountedDetail::release_ptr(data);
291 add_external(newval, -1);
293 CountedDetail::release_ptr(n);
294 add_external(newval, -1);
299 newptr.init(newval, count);
309 unsigned int get_local_count(const PackedPtr& p) const {
310 return p.extra() & ~ALIASED_PTR;
313 // Check pointer equality considering wrapped aliased pointers.
314 bool owners_eq(PackedPtr& p1, BasePtr* p2) {
315 bool aliased1 = p1.extra() & ALIASED_PTR;
317 auto p1a = CountedDetail::template get_shared_ptr_from_counted_base<T>(
319 return CountedDetail::get_counted_base(p1a) == p2;
321 return p1.get() == p2;
324 SharedPtr get_shared_ptr(const PackedPtr& p, bool inc = true) const {
325 bool aliased = p.extra() & ALIASED_PTR;
327 auto res = CountedDetail::template get_shared_ptr_from_counted_base<T>(
331 CountedDetail::template get_shared_ptr_from_counted_base<SharedPtr>(
335 return std::move(res);
338 /* Get a reference to the pointer, either from the local batch or
339 * from the global count.
341 * return is the base ptr, and the previous local count, if it is
342 * needed for compare_and_swap later.
344 PackedPtr takeOwnedBase(std::memory_order order) const noexcept {
345 PackedPtr local, newlocal;
346 local = ptr_.load(std::memory_order_acquire);
352 if (get_local_count(newlocal) + 1 > EXTERNAL_OFFSET) {
353 // spinlock in the rare case we have more than
354 // EXTERNAL_OFFSET threads trying to access at once.
355 std::this_thread::yield();
356 // Force DeterministicSchedule to choose a different thread
357 local = ptr_.load(std::memory_order_acquire);
359 newlocal.setExtra(newlocal.extra() + 1);
360 assert(get_local_count(newlocal) > 0);
361 if (ptr_.compare_exchange_weak(local, newlocal, order)) {
367 // Check if we need to push a batch from local -> global
368 auto batchcount = EXTERNAL_OFFSET / 2;
369 if (get_local_count(newlocal) > batchcount) {
370 CountedDetail::inc_shared_count(newlocal.get(), batchcount);
371 putOwnedBase(newlocal.get(), batchcount, order);
377 void putOwnedBase(BasePtr* p, unsigned int count, std::memory_order mo) const
379 PackedPtr local = ptr_.load(std::memory_order_acquire);
381 if (local.get() != p) {
384 auto newlocal = local;
385 if (get_local_count(local) > count) {
386 newlocal.setExtra(local.extra() - count);
388 // Otherwise it may be the same pointer, but someone else won
389 // the compare_exchange below, local count was already made
390 // global. We decrement the global count directly instead of
394 if (ptr_.compare_exchange_weak(local, newlocal, mo)) {
399 CountedDetail::template release_shared<T>(p, count);