williamr@2: #ifndef BOOST_DETAIL_SP_COUNTED_BASE_GCC_IA64_HPP_INCLUDED williamr@2: #define BOOST_DETAIL_SP_COUNTED_BASE_GCC_IA64_HPP_INCLUDED williamr@2: williamr@2: // williamr@2: // detail/sp_counted_base_gcc_ia64.hpp - g++ on IA64 williamr@2: // williamr@2: // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd. williamr@2: // Copyright 2004-2006 Peter Dimov williamr@2: // Copyright 2005 Ben Hutchings williamr@2: // williamr@2: // Distributed under the Boost Software License, Version 1.0. (See williamr@2: // accompanying file LICENSE_1_0.txt or copy at williamr@2: // http://www.boost.org/LICENSE_1_0.txt) williamr@2: // williamr@2: // williamr@2: // Lock-free algorithm by Alexander Terekhov williamr@2: // williamr@2: williamr@2: #include williamr@2: williamr@2: namespace boost williamr@2: { williamr@2: williamr@2: namespace detail williamr@2: { williamr@2: williamr@2: inline void atomic_increment( int * pw ) williamr@2: { williamr@2: // ++*pw; williamr@2: williamr@2: int tmp; williamr@2: williamr@2: // No barrier is required here but fetchadd always has an acquire or williamr@2: // release barrier associated with it. We choose release as it should be williamr@2: // cheaper. williamr@2: __asm__ ("fetchadd4.rel %0=%1,1" : williamr@2: "=r"(tmp), "=m"(*pw) : williamr@2: "m"( *pw )); williamr@2: } williamr@2: williamr@2: inline int atomic_decrement( int * pw ) williamr@2: { williamr@2: // return --*pw; williamr@2: williamr@2: int rv; williamr@2: williamr@2: __asm__ (" fetchadd4.rel %0=%1,-1 ;; \n" williamr@2: " cmp.eq p7,p0=1,%0 ;; \n" williamr@2: "(p7) ld4.acq %0=%1 " : williamr@2: "=&r"(rv), "=m"(*pw) : williamr@2: "m"( *pw ) : williamr@2: "p7"); williamr@2: williamr@2: return rv; williamr@2: } williamr@2: williamr@2: inline int atomic_conditional_increment( int * pw ) williamr@2: { williamr@2: // if( *pw != 0 ) ++*pw; williamr@2: // return *pw; williamr@2: williamr@2: int rv, tmp, tmp2; williamr@2: williamr@2: __asm__ ("0: ld4 %0=%3 ;; \n" williamr@2: " cmp.eq p7,p0=0,%0 ;; \n" williamr@2: "(p7) br.cond.spnt 1f \n" williamr@2: " mov ar.ccv=%0 \n" williamr@2: " add %1=1,%0 ;; \n" williamr@2: " cmpxchg4.acq %2=%3,%1,ar.ccv ;; \n" williamr@2: " cmp.ne p7,p0=%0,%2 ;; \n" williamr@2: "(p7) br.cond.spnt 0b \n" williamr@2: " mov %0=%1 ;; \n" williamr@2: "1:" : williamr@2: "=&r"(rv), "=&r"(tmp), "=&r"(tmp2), "=m"(*pw) : williamr@2: "m"( *pw ) : williamr@2: "ar.ccv", "p7"); williamr@2: williamr@2: return rv; williamr@2: } williamr@2: williamr@2: class sp_counted_base williamr@2: { williamr@2: private: williamr@2: williamr@2: sp_counted_base( sp_counted_base const & ); williamr@2: sp_counted_base & operator= ( sp_counted_base const & ); williamr@2: williamr@2: int use_count_; // #shared williamr@2: int weak_count_; // #weak + (#shared != 0) williamr@2: williamr@2: public: williamr@2: williamr@2: sp_counted_base(): use_count_( 1 ), weak_count_( 1 ) williamr@2: { williamr@2: } williamr@2: williamr@2: virtual ~sp_counted_base() // nothrow williamr@2: { williamr@2: } williamr@2: williamr@2: // dispose() is called when use_count_ drops to zero, to release williamr@2: // the resources managed by *this. williamr@2: williamr@2: virtual void dispose() = 0; // nothrow williamr@2: williamr@2: // destroy() is called when weak_count_ drops to zero. williamr@2: williamr@2: virtual void destroy() // nothrow williamr@2: { williamr@2: delete this; williamr@2: } williamr@2: williamr@2: virtual void * get_deleter( std::type_info const & ti ) = 0; williamr@2: williamr@2: void add_ref_copy() williamr@2: { williamr@2: atomic_increment( &use_count_ ); williamr@2: } williamr@2: williamr@2: bool add_ref_lock() // true on success williamr@2: { williamr@2: return atomic_conditional_increment( &use_count_ ) != 0; williamr@2: } williamr@2: williamr@2: void release() // nothrow williamr@2: { williamr@2: if( atomic_decrement( &use_count_ ) == 0 ) williamr@2: { williamr@2: dispose(); williamr@2: weak_release(); williamr@2: } williamr@2: } williamr@2: williamr@2: void weak_add_ref() // nothrow williamr@2: { williamr@2: atomic_increment( &weak_count_ ); williamr@2: } williamr@2: williamr@2: void weak_release() // nothrow williamr@2: { williamr@2: if( atomic_decrement( &weak_count_ ) == 0 ) williamr@2: { williamr@2: destroy(); williamr@2: } williamr@2: } williamr@2: williamr@2: long use_count() const // nothrow williamr@2: { williamr@2: return static_cast( use_count_ ); // TODO use ld.acq here williamr@2: } williamr@2: }; williamr@2: williamr@2: } // namespace detail williamr@2: williamr@2: } // namespace boost williamr@2: williamr@2: #endif // #ifndef BOOST_DETAIL_SP_COUNTED_BASE_GCC_IA64_HPP_INCLUDED