A core-cached shared_ptr
[folly.git] / folly / concurrency / CoreCachedSharedPtr.h
1 /*
2  * Copyright 2017 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #pragma once
18
19 #include <array>
20 #include <memory>
21
22 #include <folly/Enumerate.h>
23 #include <folly/detail/CacheLocality.h>
24
25 namespace folly {
26
27 /**
28  * This class creates core-local caches for a given shared_ptr, to
29  * mitigate contention when acquiring/releasing it.
30  *
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().
34  *
35  * @author Giuseppe Ottaviano <ott@fb.com>
36  */
37 template <class T, size_t kNumSlots = 64>
38 class CoreCachedSharedPtr {
39  public:
40   explicit CoreCachedSharedPtr(const std::shared_ptr<T>& p = nullptr) {
41     reset(p);
42   }
43
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());
48     }
49   }
50
51   std::shared_ptr<T> get() const {
52     return slots_[detail::AccessSpreader<>::current(kNumSlots)];
53   }
54
55  private:
56   template <class, size_t>
57   friend class CoreCachedWeakPtr;
58
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
61   // spaced.
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;
65   };
66
67   std::array<std::shared_ptr<T>, kNumSlots> slots_;
68 };
69
70 template <class T, size_t kNumSlots = 64>
71 class CoreCachedWeakPtr {
72  public:
73   explicit CoreCachedWeakPtr(const CoreCachedSharedPtr<T, kNumSlots>& p) {
74     for (auto slot : folly::enumerate(slots_)) {
75       *slot = p.slots_[slot.index];
76     }
77   }
78
79   std::weak_ptr<T> get() const {
80     return slots_[detail::AccessSpreader<>::current(kNumSlots)];
81   }
82
83  private:
84   std::array<std::weak_ptr<T>, kNumSlots> slots_;
85 };
86
87 } // namespace