Core-local allocator
[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     // 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 = detail::getCoreAllocatorStl<Holder, kNumSlots>(slot.index);
50       auto holder = std::allocate_shared<Holder>(alloc, p);
51       *slot = std::shared_ptr<T>(holder, p.get());
52     }
53   }
54
55   std::shared_ptr<T> get() const {
56     return slots_[detail::AccessSpreader<>::current(kNumSlots)];
57   }
58
59  private:
60   using Holder = std::shared_ptr<T>;
61
62   template <class, size_t>
63   friend class CoreCachedWeakPtr;
64
65   std::array<std::shared_ptr<T>, kNumSlots> slots_;
66 };
67
68 template <class T, size_t kNumSlots = 64>
69 class CoreCachedWeakPtr {
70  public:
71   explicit CoreCachedWeakPtr(const CoreCachedSharedPtr<T, kNumSlots>& p) {
72     for (auto slot : folly::enumerate(slots_)) {
73       *slot = p.slots_[slot.index];
74     }
75   }
76
77   std::weak_ptr<T> get() const {
78     return slots_[detail::AccessSpreader<>::current(kNumSlots)];
79   }
80
81  private:
82   std::array<std::weak_ptr<T>, kNumSlots> slots_;
83 };
84
85 } // namespace