This repository has been archived by the owner on Apr 23, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement [util.smartptr.shared.atomic]. This is the last unimplemented
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
Showing
13 changed files
with
592 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
.../memory/util.smartptr/util.smartptr.shared.atomic/atomic_compare_exchange_strong.pass.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
51 changes: 51 additions & 0 deletions
51
...til.smartptr/util.smartptr.shared.atomic/atomic_compare_exchange_strong_explicit.pass.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
46 changes: 46 additions & 0 deletions
46
...es/memory/util.smartptr/util.smartptr.shared.atomic/atomic_compare_exchange_weak.pass.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
Oops, something went wrong.