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/detail/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 for (auto& slot : slots_) {
46 auto holder = std::make_shared<Holder>(p);
47 slot = std::shared_ptr<T>(holder, p.get());
51 std::shared_ptr<T> get() const {
52 return slots_[detail::AccessSpreader<>::current(kNumSlots)];
56 template <class, size_t>
57 friend class CoreCachedWeakPtr;
59 // Space the Holders by a cache line, so their control blocks (which
60 // are adjacent to the slots thanks to make_shared()) will also be
62 struct FOLLY_ALIGN_TO_AVOID_FALSE_SHARING Holder {
63 explicit Holder(std::shared_ptr<T> p) : ptr(std::move(p)) {}
64 std::shared_ptr<T> ptr;
67 std::array<std::shared_ptr<T>, kNumSlots> slots_;
70 template <class T, size_t kNumSlots = 64>
71 class CoreCachedWeakPtr {
73 explicit CoreCachedWeakPtr(const CoreCachedSharedPtr<T, kNumSlots>& p) {
74 for (auto slot : folly::enumerate(slots_)) {
75 *slot = p.slots_[slot.index];
79 std::weak_ptr<T> get() const {
80 return slots_[detail::AccessSpreader<>::current(kNumSlots)];
84 std::array<std::weak_ptr<T>, kNumSlots> slots_;