This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: atomic operations for shared_ptr ?
- From: Oleg Endo <oleg dot endo at t-online dot de>
- To: Bronek Kozicki <brok at spamcop dot net>
- Cc: Jonathan Wakely <jwakely dot gcc at gmail dot com>, libstdc++ <libstdc++ at gcc dot gnu dot org>
- Date: Sun, 25 Aug 2013 20:47:23 +0200
- Subject: Re: atomic operations for shared_ptr ?
- References: <5218F14F dot 9080804 at spamcop dot net> <CAH6eHdTX+Un55-xK6w4Qx-T8s2TT=KjPLYzW23EJFwswAqRuKg at mail dot gmail dot com> <52190267 dot 5010905 at spamcop dot net> <CAH6eHdQOQuWv0tsZPc=6nnLs0duuObtXKNQar_mZjzLR1GoXDQ at mail dot gmail dot com> <52190AD2 dot 9010604 at spamcop dot net> <52190D3B dot 9000201 at spamcop dot net> <5219172C dot 4020409 at spamcop dot net>
On Sat, 2013-08-24 at 21:27 +0100, Bronek Kozicki wrote:
> Here is example naiive implementation with a spinlock pool. Even though
> far from perfect, I feel it would be improvement over existing
> siutuation, provided large enough pool.
>
>
> B.
>
>
> extern const size_t __shared_ptr_spinlocks_size = 256;
> extern std::atomic_flag* __shared_ptr_spinlocks; // definition omitted
>
> // platform dependent, drop least significant bits from a pointer
> #define PTR_ALIGNMENT_BITS 3
>
> namespace std
> {
> template <typename T>
> shared_ptr<T> atomic_load(const shared_ptr<T>* p)
> {
> const size_t i = ((reinterpret_cast<size_t>(p) >>
> PTR_ALIGNMENT_BITS) & (__shared_ptr_spinlocks_size - 1));
> while (__shared_ptr_spinlocks[i].test_and_set()) ; // spin
> shared_ptr<T> r = *p;
> __shared_ptr_spinlocks[i].clear();
> }
>
> template <typename T>
> shared_ptr<T> atomic_store(shared_ptr<T>* p, shared_ptr<T> r)
> {
> const size_t i = ((reinterpret_cast<size_t>(p) >>
> PTR_ALIGNMENT_BITS) & (__shared_ptr_spinlocks_size - 1));
> while (__shared_ptr_spinlocks[i].test_and_set()) ; // spin
> swap(*p, r);
> __shared_ptr_spinlocks[i].clear();
> }
>
> // etc.
> }
Probably you know it already, but just in case ...
There's a piece of compatibility code in libstdc++ that does already
something like that:
libstdc++-v3/src/c++11/compatibility-atomic-c++0x.cc
(__atomic_flag_for_address)
Cheers,
Oleg