Ticket #6667: sp_counted_base_vacpp_ppc.hpp.patch

File sp_counted_base_vacpp_ppc.hpp.patch, 3.6 KB (added by hstong@…, 11 years ago)

Implementation file for VACPP on ppc.

  • boost/smart_ptr/detail/sp_counted_base_vacpp_ppc.hpp

    old new  
     1#ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_VACPP_PPC_HPP_INCLUDED
     2#define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_VACPP_PPC_HPP_INCLUDED
     3
     4//
     5//  detail/sp_counted_base_vacpp_ppc.hpp - xlC(vacpp) on POWER
     6//   based on: detail/sp_counted_base_w32.hpp
     7//
     8//  Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
     9//  Copyright 2004-2005 Peter Dimov
     10//  Copyright 2006 Michael van der Westhuizen
     11//  Copyright 2012 IBM Corp.
     12//
     13//  Distributed under the Boost Software License, Version 1.0. (See
     14//  accompanying file LICENSE_1_0.txt or copy at
     15//  http://www.boost.org/LICENSE_1_0.txt)
     16//
     17//
     18//  Lock-free algorithm by Alexander Terekhov
     19//
     20//  Thanks to Ben Hitchings for the #weak + (#shared != 0)
     21//  formulation
     22//
     23
     24#include <boost/detail/sp_typeinfo.hpp>
     25
     26extern "builtin" void __lwsync(void);
     27extern "builtin" void __isync(void);
     28extern "builtin" int __fetch_and_add(volatile int* addr, int val);
     29extern "builtin" int __compare_and_swap(volatile int*, int*, int);
     30
     31namespace boost
     32{
     33
     34namespace detail
     35{
     36
     37inline void atomic_increment( int *pw )
     38{
     39   // ++*pw;
     40   __lwsync();
     41   __fetch_and_add(pw, 1);
     42   __isync();
     43}
     44
     45inline int atomic_decrement( int *pw )
     46{
     47   // return --*pw;
     48   __lwsync();
     49   int originalValue = __fetch_and_add(pw, -1);
     50   __isync();
     51
     52   return (originalValue - 1);
     53}
     54
     55inline int atomic_conditional_increment( int *pw )
     56{
     57   // if( *pw != 0 ) ++*pw;
     58   // return *pw;
     59
     60   __lwsync();
     61   int v = *const_cast<volatile int*>(pw);
     62   for (;;)
     63   // loop until state is known
     64   {
     65      if (v == 0) return 0;
     66      if (__compare_and_swap(pw, &v, v + 1))
     67      {
     68         __isync(); return (v + 1);
     69      }
     70   }
     71}
     72
     73class sp_counted_base
     74{
     75private:
     76
     77    sp_counted_base( sp_counted_base const & );
     78    sp_counted_base & operator= ( sp_counted_base const & );
     79
     80    int use_count_;        // #shared
     81    int weak_count_;       // #weak + (#shared != 0)
     82    char pad[64] __attribute__((__aligned__(64)));
     83            // pad to prevent false sharing
     84public:
     85
     86    sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
     87    {
     88    }
     89
     90    virtual ~sp_counted_base() // nothrow
     91    {
     92    }
     93
     94    // dispose() is called when use_count_ drops to zero, to release
     95    // the resources managed by *this.
     96
     97    virtual void dispose() = 0; // nothrow
     98
     99    // destroy() is called when weak_count_ drops to zero.
     100
     101    virtual void destroy() // nothrow
     102    {
     103        delete this;
     104    }
     105
     106    virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
     107
     108    void add_ref_copy()
     109    {
     110        atomic_increment( &use_count_ );
     111    }
     112
     113    bool add_ref_lock() // true on success
     114    {
     115        return atomic_conditional_increment( &use_count_ ) != 0;
     116    }
     117
     118    void release() // nothrow
     119    {
     120        if( atomic_decrement( &use_count_ ) == 0 )
     121        {
     122            dispose();
     123            weak_release();
     124        }
     125    }
     126
     127    void weak_add_ref() // nothrow
     128    {
     129        atomic_increment( &weak_count_ );
     130    }
     131
     132    void weak_release() // nothrow
     133    {
     134        if( atomic_decrement( &weak_count_ ) == 0 )
     135        {
     136            destroy();
     137        }
     138    }
     139
     140    long use_count() const // nothrow
     141    {
     142        return *const_cast<volatile int*>(&use_count_);
     143    }
     144};
     145
     146} // namespace detail
     147
     148} // namespace boost
     149
     150#endif  // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_VACPP_PPC_HPP_INCLUDED