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.
21 #include <folly/concurrency/CacheLocality.h>
26 * Holds a type T, in addition to enough padding to ensure that it isn't subject
27 * to false sharing within the range used by folly.
29 * If `sizeof(T) <= alignof(T)` then the inner `T` will be entirely within one
30 * false sharing range (AKA cache line).
33 class CachelinePadded {
35 alignof(T) < CacheLocality::kFalseSharingRange,
36 "CachelinePadded does not support types aligned >= a cache-line.");
39 template <typename... Args>
40 explicit CachelinePadded(Args&&... args)
41 : inner_(std::forward<Args>(args)...) {}
47 const T* get() const {
55 const T* operator->() const {
63 const T& operator*() const {
68 static constexpr size_t paddingSize() noexcept {
69 return CacheLocality::kFalseSharingRange -
70 (alignof(T) % CacheLocality::kFalseSharingRange);
72 char paddingPre_[paddingSize()];
74 char paddingPost_[paddingSize()];