Skip to content
This repository has been archived by the owner on Apr 23, 2020. It is now read-only.

Commit

Permalink
Implement [util.smartptr.shared.atomic]. This is the last unimplemented
Browse files Browse the repository at this point in the history
section in libc++.  This requires a recompiled dylib.  Failure to rebuild
the dylib will result in a link-time error if and only if the functions from
[util.smartptr.shared.atomic] are used.

The implementation is not lock free.  After considerable thought, I know of no
way to make the implementation lock free.  Ideas welcome along that front.  But
changing the ABI of shared_ptr is not on the table at this point.

The mutex used to lock these function is encapsulated by std::__sp_mut.  The
only thing the client knows about std::__sp_mut is that it has a void* data
member, can't be constructed, and has lock and unlock members.  Within the
binary __sp_mut is currently implemented as a pointer to a std::mutex.  That can
change in the future without disturbing the ABI (as long as sizeof(__sp_mut)
remains constant.

I specifically did not make __sp_mut a spin lock as I have a pathological
distrust of spin locks.  Testing on OS X reveals that the use of std::mutex in
this role is not a large performance penalty as long as the contention for the
mutex is low (more likely to get the lock than to have to wait).  In the future
we can still make __sp_mut a spin lock if that is what is desired (without ABI
damage).

The dylib contains 16 __sp_mut's to be chosen based on the hash of the address
of the shared_ptr.  The constant 16 is a ball-park reasonable space/time
tradeoff.

std::hash<T*> was changed to call __murmur2_or_cityhash, instead of the identity
function.  I had thought we had already done this, but I was mistaken.

All of this is under #if __has_feature(cxx_atomic) even though the
implementation is not lock free, because the signatures require access to
std::memory_order, which is currently available only under
__has_feature(cxx_atomic).

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@160940 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
Howard Hinnant committed Jul 30, 2012
1 parent 116ce6a commit 5fec82d
Show file tree
Hide file tree
Showing 13 changed files with 592 additions and 1 deletion.
149 changes: 148 additions & 1 deletion include/memory
Expand Up @@ -602,6 +602,10 @@ void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
#include <cassert>
#endif

#if __has_feature(cxx_atomic)
# include <atomic>
#endif

#include <__undef_min_max>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Expand Down Expand Up @@ -3387,8 +3391,19 @@ struct __scalar_hash<_Tp, 4>

template<class _Tp>
struct _LIBCPP_VISIBLE hash<_Tp*>
: public __scalar_hash<_Tp*>
: public unary_function<_Tp*, size_t>
{
_LIBCPP_INLINE_VISIBILITY
size_t operator()(_Tp* __v) const _NOEXCEPT
{
union
{
_Tp* __t;
size_t __a;
} __u;
__u.__t = __v;
return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
}
};

template <class _Tp, class _Dp>
Expand Down Expand Up @@ -3934,6 +3949,10 @@ public:
_LIBCPP_INLINE_VISIBILITY
bool owner_before(weak_ptr<_Up> const& __p) const
{return __cntrl_ < __p.__cntrl_;}
_LIBCPP_INLINE_VISIBILITY
bool
__owner_equivalent(const shared_ptr& __p) const
{return __cntrl_ == __p.__cntrl_;}

#ifndef _LIBCPP_NO_RTTI
template <class _Dp>
Expand Down Expand Up @@ -5245,6 +5264,134 @@ inline _LIBCPP_INLINE_VISIBILITY
basic_ostream<_CharT, _Traits>&
operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);

#if __has_feature(cxx_atomic)

class __sp_mut
{
void* _;
public:
void lock() _NOEXCEPT;
void unlock() _NOEXCEPT;

private:
_LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
__sp_mut(const __sp_mut&);
__sp_mut& operator=(const __sp_mut&);

friend __sp_mut& __get_sp_mut(const void*);
};

__sp_mut& __get_sp_mut(const void*);

template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
bool
atomic_is_lock_free(const shared_ptr<_Tp>*)
{
return false;
}

template <class _Tp>
shared_ptr<_Tp>
atomic_load(const shared_ptr<_Tp>* __p)
{
__sp_mut& __m = __get_sp_mut(__p);
__m.lock();
shared_ptr<_Tp> __q = *__p;
__m.unlock();
return __q;
}

template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
shared_ptr<_Tp>
atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
{
return atomic_load(__p);
}

template <class _Tp>
void
atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
{
__sp_mut& __m = __get_sp_mut(__p);
__m.lock();
__p->swap(__r);
__m.unlock();
}

template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
void
atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
{
atomic_store(__p, __r);
}

template <class _Tp>
shared_ptr<_Tp>
atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
{
__sp_mut& __m = __get_sp_mut(__p);
__m.lock();
__p->swap(__r);
__m.unlock();
return __r;
}

template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
shared_ptr<_Tp>
atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
{
return atomic_exchange(__p, __r);
}

template <class _Tp>
bool
atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
{
__sp_mut& __m = __get_sp_mut(__p);
__m.lock();
if (__p->__owner_equivalent(*__v))
{
*__p = __w;
__m.unlock();
return true;
}
*__v = *__p;
__m.unlock();
return false;
}

template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
bool
atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
{
return atomic_compare_exchange_strong(__p, __v, __w);
}

template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
bool
atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
shared_ptr<_Tp> __w, memory_order, memory_order)
{
return atomic_compare_exchange_strong(__p, __v, __w);
}

template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
bool
atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
shared_ptr<_Tp> __w, memory_order, memory_order)
{
return atomic_compare_exchange_weak(__p, __v, __w);
}

#endif // __has_feature(cxx_atomic)

//enum class
struct _LIBCPP_VISIBLE pointer_safety
{
Expand Down
35 changes: 35 additions & 0 deletions src/memory.cpp
Expand Up @@ -9,6 +9,7 @@

#define _LIBCPP_BUILDING_MEMORY
#include "memory"
#include "mutex"

_LIBCPP_BEGIN_NAMESPACE_STD

Expand Down Expand Up @@ -117,6 +118,40 @@ __shared_weak_count::__get_deleter(const type_info&) const _NOEXCEPT

#endif // _LIBCPP_NO_RTTI

static const std::size_t __sp_mut_count = 16;
static mutex mut_back[__sp_mut_count];

_LIBCPP_CONSTEXPR __sp_mut::__sp_mut(void* p) _NOEXCEPT
: _(p)
{
}

void
__sp_mut::lock() _NOEXCEPT
{
reinterpret_cast<mutex*>(_)->lock();
}

void
__sp_mut::unlock() _NOEXCEPT
{
reinterpret_cast<mutex*>(_)->unlock();
}

__sp_mut&
__get_sp_mut(const void* p)
{
static __sp_mut muts[__sp_mut_count]
{
&mut_back[ 0], &mut_back[ 1], &mut_back[ 2], &mut_back[ 3],
&mut_back[ 4], &mut_back[ 5], &mut_back[ 6], &mut_back[ 7],
&mut_back[ 8], &mut_back[ 9], &mut_back[10], &mut_back[11],
&mut_back[12], &mut_back[13], &mut_back[14], &mut_back[15]
};
return muts[hash<const void*>()(p) & (__sp_mut_count-1)];
}


void
declare_reachable(void*)
{
Expand Down
@@ -0,0 +1,46 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

// <memory>

// shared_ptr

// template <class T>
// bool
// atomic_compare_exchange_strong(shared_ptr<T>* p, shared_ptr<T>* v,
// shared_ptr<T> w);

#include <memory>
#include <cassert>

int main()
{
#if __has_feature(cxx_atomic)
{
std::shared_ptr<int> p(new int(4));
std::shared_ptr<int> v(new int(3));
std::shared_ptr<int> w(new int(2));
bool b = std::atomic_compare_exchange_strong(&p, &v, w);
assert(b == false);
assert(*p == 4);
assert(*v == 4);
assert(*w == 2);
}
{
std::shared_ptr<int> p(new int(4));
std::shared_ptr<int> v = p;
std::shared_ptr<int> w(new int(2));
bool b = std::atomic_compare_exchange_strong(&p, &v, w);
assert(b == true);
assert(*p == 2);
assert(*v == 4);
assert(*w == 2);
}
#endif
}
@@ -0,0 +1,51 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

// <memory>

// shared_ptr

// template <class T>
// bool
// atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
// shared_ptr<T> w, memory_order success,
// memory_order failure);

#include <memory>
#include <cassert>

int main()
{
#if __has_feature(cxx_atomic)
{
std::shared_ptr<int> p(new int(4));
std::shared_ptr<int> v(new int(3));
std::shared_ptr<int> w(new int(2));
bool b = std::atomic_compare_exchange_strong_explicit(&p, &v, w,
std::memory_order_seq_cst,
std::memory_order_seq_cst);
assert(b == false);
assert(*p == 4);
assert(*v == 4);
assert(*w == 2);
}
{
std::shared_ptr<int> p(new int(4));
std::shared_ptr<int> v = p;
std::shared_ptr<int> w(new int(2));
bool b = std::atomic_compare_exchange_strong_explicit(&p, &v, w,
std::memory_order_seq_cst,
std::memory_order_seq_cst);
assert(b == true);
assert(*p == 2);
assert(*v == 4);
assert(*w == 2);
}
#endif
}
@@ -0,0 +1,46 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

// <memory>

// shared_ptr

// template <class T>
// bool
// atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v,
// shared_ptr<T> w);

#include <memory>
#include <cassert>

int main()
{
#if __has_feature(cxx_atomic)
{
std::shared_ptr<int> p(new int(4));
std::shared_ptr<int> v(new int(3));
std::shared_ptr<int> w(new int(2));
bool b = std::atomic_compare_exchange_weak(&p, &v, w);
assert(b == false);
assert(*p == 4);
assert(*v == 4);
assert(*w == 2);
}
{
std::shared_ptr<int> p(new int(4));
std::shared_ptr<int> v = p;
std::shared_ptr<int> w(new int(2));
bool b = std::atomic_compare_exchange_weak(&p, &v, w);
assert(b == true);
assert(*p == 2);
assert(*v == 4);
assert(*w == 2);
}
#endif
}

0 comments on commit 5fec82d

Please sign in to comment.