2 * Copyright 2017 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.
22 #include <folly/Enumerate.h>
23 #include <folly/concurrency/CacheLocality.h>
28 * This class creates core-local caches for a given shared_ptr, to
29 * mitigate contention when acquiring/releasing it.
31 * It has the same thread-safety guarantees as shared_ptr: it is safe
32 * to concurrently call get(), but reset()s must be synchronized with
33 * reads and other resets().
35 * @author Giuseppe Ottaviano <ott@fb.com>
37 template <class T, size_t kNumSlots = 64>
38 class CoreCachedSharedPtr {
40 explicit CoreCachedSharedPtr(const std::shared_ptr<T>& p = nullptr) {
44 void reset(const std::shared_ptr<T>& p = nullptr) {
45 // Allocate each Holder in a different CoreAllocator stripe to
46 // prevent false sharing. Their control blocks will be adjacent
47 // thanks to allocate_shared().
48 for (auto slot : folly::enumerate(slots_)) {
49 auto alloc = getCoreAllocatorStl<Holder, kNumSlots>(slot.index);
50 auto holder = std::allocate_shared<Holder>(alloc, p);
51 *slot = std::shared_ptr<T>(holder, p.get());
55 std::shared_ptr<T> get() const {
56 return slots_[AccessSpreader<>::current(kNumSlots)];
60 using Holder = std::shared_ptr<T>;
62 template <class, size_t>
63 friend class CoreCachedWeakPtr;
65 std::array<std::shared_ptr<T>, kNumSlots> slots_;
68 template <class T, size_t kNumSlots = 64>
69 class CoreCachedWeakPtr {
71 explicit CoreCachedWeakPtr(const CoreCachedSharedPtr<T, kNumSlots>& p) {
72 for (auto slot : folly::enumerate(slots_)) {
73 *slot = p.slots_[slot.index];
77 std::weak_ptr<T> get() const {
78 return slots_[AccessSpreader<>::current(kNumSlots)];
82 std::array<std::weak_ptr<T>, kNumSlots> slots_;