This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: atomic operations for shared_ptr ?


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]