Commit bd068648 authored by Dave Watson's avatar Dave Watson Committed by Facebook Github Bot

atomic_shared_ptr

Summary:
A (almost) lock-free atomic_shared_ptr, matching the proposed concurrency TS interface.

See notes at top of file

Reviewed By: davidtgoldblatt

Differential Revision: D4716098

fbshipit-source-id: b9ca2443ba9e227ebb6f40807128073c6e14222a
parent 2f0cabfb
......@@ -16,11 +16,12 @@
#pragma once
#include <atomic>
#include <type_traits>
#include <folly/Traits.h>
#include <string.h>
#include <folly/detail/AtomicUtils.h>
#include <stdint.h>
#include <string.h>
#include <atomic>
#include <type_traits>
namespace folly {
......@@ -75,10 +76,19 @@ class AtomicStruct {
}
bool compare_exchange_strong(
T& v0, T v1,
std::memory_order mo = std::memory_order_seq_cst) noexcept {
T& v0,
T v1,
std::memory_order mo = std::memory_order_seq_cst) noexcept {
return compare_exchange_strong(
v0, v1, mo, detail::default_failure_memory_order(mo));
}
bool compare_exchange_strong(
T& v0,
T v1,
std::memory_order success,
std::memory_order failure) noexcept {
Raw d0 = encode(v0);
bool rv = data.compare_exchange_strong(d0, encode(v1), mo);
bool rv = data.compare_exchange_strong(d0, encode(v1), success, failure);
if (!rv) {
v0 = decode(d0);
}
......@@ -86,10 +96,19 @@ class AtomicStruct {
}
bool compare_exchange_weak(
T& v0, T v1,
std::memory_order mo = std::memory_order_seq_cst) noexcept {
T& v0,
T v1,
std::memory_order mo = std::memory_order_seq_cst) noexcept {
return compare_exchange_weak(
v0, v1, mo, detail::default_failure_memory_order(mo));
}
bool compare_exchange_weak(
T& v0,
T v1,
std::memory_order success,
std::memory_order failure) noexcept {
Raw d0 = encode(v0);
bool rv = data.compare_exchange_weak(d0, encode(v1), mo);
bool rv = data.compare_exchange_weak(d0, encode(v1), success, failure);
if (!rv) {
v0 = decode(d0);
}
......
......@@ -55,6 +55,7 @@ nobase_follyinclude_HEADERS = \
CPortability.h \
detail/AtomicHashUtils.h \
detail/AtomicUnorderedMapUtils.h \
detail/AtomicUtils.h \
detail/BitIteratorDetail.h \
detail/BitsDetail.h \
detail/CacheLocality.h \
......@@ -95,6 +96,8 @@ nobase_follyinclude_HEADERS = \
Executor.h \
Expected.h \
experimental/AsymmetricMemoryBarrier.h \
experimental/AtomicSharedPtr.h \
experimental/detail/AtomicSharedPtr-detail.h \
experimental/AutoTimer.h \
experimental/Bits.h \
experimental/BitVectorCoding.h \
......
......@@ -144,4 +144,9 @@ static_assert(sizeof(PackedSyncPtr<void>) == 8,
"PackedSyncPtr should be only 8 bytes---something is "
"messed up");
template <typename T>
std::ostream& operator<<(std::ostream& os, const PackedSyncPtr<T>& ptr) {
os << "PackedSyncPtr(" << ptr.get() << ", " << ptr.extra() << ")";
return os;
}
}
/*
* Copyright 2017 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <atomic>
namespace folly {
namespace detail {
inline std::memory_order default_failure_memory_order(
std::memory_order successMode) {
switch (successMode) {
case std::memory_order_acq_rel:
return std::memory_order_acquire;
case std::memory_order_release:
return std::memory_order_relaxed;
default:
return successMode;
}
}
}
} // namespace
This diff is collapsed.
/*
* Copyright 2017-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <atomic>
#include <memory>
namespace folly {
namespace detail {
class shared_ptr_internals {
public:
template <typename T, typename... Args>
static std::shared_ptr<T> make_ptr(Args&&... args) {
return std::make_shared<T>(std::forward<Args...>(args...));
}
typedef std::__shared_count<std::_S_atomic> shared_count;
typedef std::_Sp_counted_base<std::_S_atomic> counted_base;
template <typename T>
using CountedPtr = std::shared_ptr<T>;
template <typename T>
static counted_base* get_counted_base(const std::shared_ptr<T>& bar) {
// reinterpret_pointer_cast<const void>
// Not quite C++ legal, but explicit template instantiation access to
// private members requires full type name (i.e. shared_ptr<const void>, not
// shared_ptr<T>)
const std::shared_ptr<const void>& ptr(
reinterpret_cast<const std::shared_ptr<const void>&>(bar));
return (ptr.*fieldPtr(access_shared_ptr{})).*fieldPtr(access_base{});
}
static void inc_shared_count(counted_base* base, long count) {
__gnu_cxx::__atomic_add_dispatch(
&(base->*fieldPtr(access_use_count{})), count);
}
template <typename T>
static void release_shared(counted_base* base, long count) {
// If count == 1, this is equivalent to base->_M_release()
if (__gnu_cxx::__exchange_and_add_dispatch(
&(base->*fieldPtr(access_use_count{})), -count) == count) {
base->_M_dispose();
if (__gnu_cxx::__exchange_and_add_dispatch(
&(base->*fieldPtr(access_weak_count{})), -1) == 1) {
base->_M_destroy();
}
}
}
template <typename T>
static T* get_shared_ptr(counted_base* base) {
// See if this was a make_shared allocation
auto inplace = base->_M_get_deleter(typeid(std::_Sp_make_shared_tag));
if (inplace) {
return (T*)inplace;
}
// Could also be a _Sp_counted_deleter, but the layout is the same
auto ptr =
static_cast<std::_Sp_counted_ptr<const void*, std::_S_atomic>*>(base);
return (T*)(ptr->*fieldPtr(access_counted_ptr_ptr{}));
}
template <typename T>
static T* release_ptr(std::shared_ptr<T>& p) {
auto res = p.get();
std::shared_ptr<const void>& ptr(
reinterpret_cast<std::shared_ptr<const void>&>(p));
ptr.*fieldPtr(access_shared_ptr_ptr{}) = nullptr;
(ptr.*fieldPtr(access_refcount{})).*fieldPtr(access_base{}) = nullptr;
return res;
}
template <typename T>
static std::shared_ptr<T> get_shared_ptr_from_counted_base(
counted_base* base,
bool inc = true) {
if (!base) {
return nullptr;
}
std::shared_ptr<const void> newp;
if (inc) {
inc_shared_count(base, 1);
}
newp.*fieldPtr(access_shared_ptr_ptr{}) =
get_shared_ptr<const void>(base); // _M_ptr
(newp.*fieldPtr(access_refcount{})).*fieldPtr(access_base{}) = base;
// reinterpret_pointer_cast<T>
auto res = reinterpret_cast<std::shared_ptr<T>*>(&newp);
return std::move(*res);
}
private:
/* Accessors for private members using explicit template instantiation */
struct access_shared_ptr {
typedef shared_count std::__shared_ptr<const void, std::_S_atomic>::*type;
friend type fieldPtr(access_shared_ptr);
};
struct access_base {
typedef counted_base* shared_count::*type;
friend type fieldPtr(access_base);
};
struct access_use_count {
typedef _Atomic_word counted_base::*type;
friend type fieldPtr(access_use_count);
};
struct access_weak_count {
typedef _Atomic_word counted_base::*type;
friend type fieldPtr(access_weak_count);
};
struct access_counted_ptr_ptr {
typedef const void* std::_Sp_counted_ptr<const void*, std::_S_atomic>::*
type;
friend type fieldPtr(access_counted_ptr_ptr);
};
struct access_shared_ptr_ptr {
typedef const void* std::__shared_ptr<const void, std::_S_atomic>::*type;
friend type fieldPtr(access_shared_ptr_ptr);
};
struct access_refcount {
typedef shared_count std::__shared_ptr<const void, std::_S_atomic>::*type;
friend type fieldPtr(access_refcount);
};
template <typename Tag, typename Tag::type M>
struct Rob {
friend typename Tag::type fieldPtr(Tag) {
return M;
}
};
};
template struct shared_ptr_internals::Rob<
shared_ptr_internals::access_shared_ptr,
&std::__shared_ptr<const void, std::_S_atomic>::_M_refcount>;
template struct shared_ptr_internals::Rob<
shared_ptr_internals::access_base,
&shared_ptr_internals::shared_count::_M_pi>;
template struct shared_ptr_internals::Rob<
shared_ptr_internals::access_use_count,
&shared_ptr_internals::counted_base::_M_use_count>;
template struct shared_ptr_internals::Rob<
shared_ptr_internals::access_weak_count,
&shared_ptr_internals::counted_base::_M_weak_count>;
template struct shared_ptr_internals::Rob<
shared_ptr_internals::access_counted_ptr_ptr,
&std::_Sp_counted_ptr<const void*, std::_S_atomic>::_M_ptr>;
template struct shared_ptr_internals::Rob<
shared_ptr_internals::access_shared_ptr_ptr,
&std::__shared_ptr<const void, std::_S_atomic>::_M_ptr>;
template struct shared_ptr_internals::Rob<
shared_ptr_internals::access_refcount,
&std::__shared_ptr<const void, std::_S_atomic>::_M_refcount>;
} // namespace detail
} // namespace folly
/*
* Copyright 2017-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
struct counted_shared_tag {};
template <template <typename> class Atom = std::atomic>
struct intrusive_shared_count {
intrusive_shared_count() {
counts.store(0);
}
void add_ref(uint64_t count = 1) {
counts.fetch_add(count);
}
uint64_t release_ref(uint64_t count = 1) {
return counts.fetch_sub(count);
}
Atom<uint64_t> counts;
};
template <template <typename> class Atom = std::atomic>
struct counted_ptr_base {
protected:
static intrusive_shared_count<Atom>* getRef(void* pt) {
char* p = (char*)pt;
p -= sizeof(intrusive_shared_count<Atom>);
return (intrusive_shared_count<Atom>*)p;
}
};
// basically shared_ptr, but only supports make_counted, and provides
// access to add_ref / release_ref with a count. Alias not supported.
template <typename T, template <typename> class Atom = std::atomic>
class counted_ptr : public counted_ptr_base<Atom> {
public:
T* p_;
counted_ptr() : p_(nullptr) {}
counted_ptr(counted_shared_tag, T* p) : p_(p) {
if (p_)
counted_ptr_base<Atom>::getRef(p_)->add_ref();
}
counted_ptr(const counted_ptr& o) : p_(o.p_) {
if (p_)
counted_ptr_base<Atom>::getRef(p_)->add_ref();
}
counted_ptr& operator=(const counted_ptr& o) {
if (p_ && counted_ptr_base<Atom>::getRef(p_)->release_ref() == 1) {
p_->~T();
free(counted_ptr_base<Atom>::getRef(p_));
}
p_ = o.p_;
if (p_)
counted_ptr_base<Atom>::getRef(p_)->add_ref();
return *this;
}
explicit counted_ptr(T* p) : p_(p) {
CHECK(!p);
}
~counted_ptr() {
if (p_ && counted_ptr_base<Atom>::getRef(p_)->release_ref() == 1) {
p_->~T();
free(counted_ptr_base<Atom>::getRef(p_));
}
}
typename std::add_lvalue_reference<T>::type operator*() const {
return *p_;
}
T* get() const {
return p_;
}
T* operator->() const {
return p_;
}
explicit operator bool() const {
return p_ == nullptr ? false : true;
}
bool operator==(const counted_ptr<T, Atom>& p) const {
return get() == p.get();
}
};
template <
template <typename> class Atom = std::atomic,
typename T,
typename... Args>
counted_ptr<T, Atom> make_counted(Args&&... args) {
char* mem = (char*)malloc(sizeof(T) + sizeof(intrusive_shared_count<Atom>));
if (!mem) {
throw std::bad_alloc();
}
new (mem) intrusive_shared_count<Atom>();
T* ptr = (T*)(mem + sizeof(intrusive_shared_count<Atom>));
new (ptr) T(std::forward<Args>(args)...);
return counted_ptr<T, Atom>(counted_shared_tag(), ptr);
}
template <template <typename> class Atom = std::atomic>
class counted_ptr_internals : public counted_ptr_base<Atom> {
public:
template <typename T, typename... Args>
static counted_ptr<T, Atom> make_ptr(Args&&... args) {
return make_counted<Atom, T>(std::forward<Args...>(args...));
}
template <typename T>
using CountedPtr = counted_ptr<T, Atom>;
typedef void counted_base;
template <typename T>
static counted_base* get_counted_base(const counted_ptr<T, Atom>& bar) {
return bar.p_;
}
template <typename T>
static T* get_shared_ptr(counted_base* base) {
return (T*)base;
}
template <typename T>
static T* release_ptr(counted_ptr<T, Atom>& p) {
auto res = p.p_;
p.p_ = nullptr;
return res;
}
template <typename T>
static counted_ptr<T, Atom> get_shared_ptr_from_counted_base(
counted_base* base,
bool inc = true) {
auto res = counted_ptr<T, Atom>(counted_shared_tag(), (T*)(base));
if (!inc) {
release_shared<T>(base, 1);
}
return res;
}
static void inc_shared_count(counted_base* base, int64_t count) {
counted_ptr_base<Atom>::getRef(base)->add_ref(count);
}
template <typename T>
static void release_shared(counted_base* base, uint64_t count) {
if (count == counted_ptr_base<Atom>::getRef(base)->release_ref(count)) {
((T*)base)->~T();
free(counted_ptr_base<Atom>::getRef(base));
}
}
};
/*
* Copyright 2017-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <atomic>
#include <memory>
#include <thread>
#include <folly/experimental/AtomicSharedPtr.h>
#include <folly/experimental/test/AtomicSharedPtrCounted.h>
#include <folly/portability/GTest.h>
#include <folly/test/DeterministicSchedule.h>
using namespace folly;
using namespace folly::test;
using namespace std;
static int c_count{0};
static int d_count{0};
using DSched = DeterministicSchedule;
DEFINE_int64(seed, 0, "Seed for random number generators");
DEFINE_int32(num_threads, 32, "Number of threads");
struct foo {
foo() {
c_count++;
}
~foo() {
d_count++;
}
};
TEST(AtomicSharedPtr, operators) {
atomic_shared_ptr<int> fooptr;
EXPECT_TRUE(fooptr.is_lock_free());
auto i = new int(5);
std::shared_ptr<int> s(i);
fooptr.store(s);
shared_ptr<int> bar(fooptr);
EXPECT_TRUE(fooptr.compare_exchange_strong(s, nullptr));
s.reset();
bar.reset();
}
TEST(AtomicSharedPtr, exchange) {
atomic_shared_ptr<int> fooptr;
auto a = make_shared<int>(1);
fooptr.store(std::move(a));
auto b = fooptr.exchange(make_shared<int>());
EXPECT_EQ(*b, 1);
}
TEST(AtomicSharedPtr, foo) {
c_count = 0;
d_count = 0;
{
atomic_shared_ptr<foo> fooptr;
fooptr.store(make_shared<foo>());
EXPECT_EQ(1, c_count);
EXPECT_EQ(0, d_count);
{
auto res = fooptr.load();
EXPECT_EQ(1, c_count);
EXPECT_EQ(0, d_count);
}
EXPECT_EQ(1, c_count);
EXPECT_EQ(0, d_count);
}
EXPECT_EQ(1, c_count);
EXPECT_EQ(1, d_count);
}
TEST(AtomicSharedPtr, counted) {
c_count = 0;
d_count = 0;
{
atomic_shared_ptr<foo, std::atomic, counted_ptr_internals<std::atomic>>
fooptr;
fooptr.store(make_counted<std::atomic, foo>());
EXPECT_EQ(1, c_count);
EXPECT_EQ(0, d_count);
{
auto res = fooptr.load();
EXPECT_EQ(1, c_count);
EXPECT_EQ(0, d_count);
}
EXPECT_EQ(1, c_count);
EXPECT_EQ(0, d_count);
}
EXPECT_EQ(1, c_count);
EXPECT_EQ(1, d_count);
}
TEST(AtomicSharedPtr, counted2) {
auto foo = make_counted<std::atomic, bool>();
atomic_shared_ptr<bool, std::atomic, counted_ptr_internals<std::atomic>>
fooptr(foo);
fooptr.store(foo);
fooptr.load();
}
TEST(AtomicSharedPtr, ConstTest) {
const auto a(std::make_shared<foo>());
atomic_shared_ptr<foo> atom;
atom.store(a);
atomic_shared_ptr<const foo> catom;
}
TEST(AtomicSharedPtr, AliasingConstructorTest) {
c_count = 0;
d_count = 0;
auto a = std::make_shared<foo>();
auto b = new foo;
auto alias = std::shared_ptr<foo>(a, b);
atomic_shared_ptr<foo> asp;
asp.store(alias);
a.reset();
alias.reset();
auto res1 = asp.load();
auto res2 = asp.exchange(nullptr);
EXPECT_EQ(b, res1.get());
EXPECT_EQ(b, res2.get());
EXPECT_EQ(2, c_count);
EXPECT_EQ(0, d_count);
res1.reset();
res2.reset();
EXPECT_EQ(2, c_count);
EXPECT_EQ(1, d_count);
delete b;
EXPECT_EQ(2, c_count);
EXPECT_EQ(2, d_count);
}
TEST(AtomicSharedPtr, DeterministicTest) {
DSched sched(DSched::uniform(FLAGS_seed));
auto foo = make_counted<DeterministicAtomic, bool>();
atomic_shared_ptr<
bool,
DeterministicAtomic,
counted_ptr_internals<DeterministicAtomic>>
fooptr(foo);
std::vector<std::thread> threads(FLAGS_num_threads);
for (int tid = 0; tid < FLAGS_num_threads; ++tid) {
threads[tid] = DSched::thread([&, tid]() {
for (int i = 0; i < 1000; i++) {
auto l = fooptr.load();
EXPECT_TRUE(l.get() != nullptr);
fooptr.compare_exchange_strong(l, l);
fooptr.store(make_counted<DeterministicAtomic, bool>());
EXPECT_FALSE(fooptr.compare_exchange_strong(
l, make_counted<DeterministicAtomic, bool>()));
}
});
}
for (auto& t : threads) {
DSched::join(t);
}
}
......@@ -35,6 +35,7 @@
#include <vector>
#include <folly/ScopeGuard.h>
#include <folly/detail/AtomicUtils.h>
#include <folly/detail/CacheLocality.h>
#include <folly/detail/Futex.h>
......@@ -231,10 +232,20 @@ struct DeterministicAtomic {
bool is_lock_free() const noexcept { return data.is_lock_free(); }
bool compare_exchange_strong(
T& v0, T v1, std::memory_order mo = std::memory_order_seq_cst) noexcept {
T& v0,
T v1,
std::memory_order mo = std::memory_order_seq_cst) noexcept {
return compare_exchange_strong(
v0, v1, mo, detail::default_failure_memory_order(mo));
}
bool compare_exchange_strong(
T& v0,
T v1,
std::memory_order success,
std::memory_order failure) noexcept {
DeterministicSchedule::beforeSharedAccess();
auto orig = v0;
bool rv = data.compare_exchange_strong(v0, v1, mo);
bool rv = data.compare_exchange_strong(v0, v1, success, failure);
FOLLY_TEST_DSCHED_VLOG(this << ".compare_exchange_strong(" << std::hex
<< orig << ", " << std::hex << v1 << ") -> "
<< rv << "," << std::hex << v0);
......@@ -243,10 +254,20 @@ struct DeterministicAtomic {
}
bool compare_exchange_weak(
T& v0, T v1, std::memory_order mo = std::memory_order_seq_cst) noexcept {
T& v0,
T v1,
std::memory_order mo = std::memory_order_seq_cst) noexcept {
return compare_exchange_weak(
v0, v1, mo, detail::default_failure_memory_order(mo));
}
bool compare_exchange_weak(
T& v0,
T v1,
std::memory_order success,
std::memory_order failure) noexcept {
DeterministicSchedule::beforeSharedAccess();
auto orig = v0;
bool rv = data.compare_exchange_weak(v0, v1, mo);
bool rv = data.compare_exchange_weak(v0, v1, success, failure);
FOLLY_TEST_DSCHED_VLOG(this << ".compare_exchange_weak(" << std::hex << orig
<< ", " << std::hex << v1 << ") -> " << rv
<< "," << std::hex << v0);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment