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
5 // detail/sp_counted_base_vacpp_ppc.hpp - xlC(vacpp) on POWER
6 // based on: detail/sp_counted_base_w32.hpp
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.
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)
18 // Lock-free algorithm by Alexander Terekhov
20 // Thanks to Ben Hitchings for the #weak + (#shared != 0)
24 #include <boost/smart_ptr/detail/sp_typeinfo_.hpp>
25 #include <boost/smart_ptr/detail/sp_obsolete.hpp>
26 #include <boost/config.hpp>
28 #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
30 #include <boost/config/pragma_message.hpp>
31 BOOST_PRAGMA_MESSAGE("Using xlC/PowerPC sp_counted_base")
37 extern "builtin" void __lwsync(void);
38 extern "builtin" void __isync(void);
39 extern "builtin" int __fetch_and_add(volatile int* addr, int val);
40 extern "builtin" int __compare_and_swap(volatile int*, int*, int);
48 inline void atomic_increment( int *pw )
52 __fetch_and_add(pw, 1);
56 inline int atomic_decrement( int *pw )
60 int originalValue = __fetch_and_add(pw, -1);
63 return (originalValue - 1);
66 inline int atomic_conditional_increment( int *pw )
68 // if( *pw != 0 ) ++*pw;
72 int v = *const_cast<volatile int*>(pw);
74 // loop until state is known
77 if (__compare_and_swap(pw, &v, v + 1))
79 __isync(); return (v + 1);
84 class BOOST_SYMBOL_VISIBLE sp_counted_base
88 sp_counted_base( sp_counted_base const & );
89 sp_counted_base & operator= ( sp_counted_base const & );
91 int use_count_; // #shared
92 int weak_count_; // #weak + (#shared != 0)
93 char pad[64] __attribute__((__aligned__(64)));
94 // pad to prevent false sharing
97 sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
101 virtual ~sp_counted_base() // nothrow
105 // dispose() is called when use_count_ drops to zero, to release
106 // the resources managed by *this.
108 virtual void dispose() = 0; // nothrow
110 // destroy() is called when weak_count_ drops to zero.
112 virtual void destroy() // nothrow
117 virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
118 virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
119 virtual void * get_untyped_deleter() = 0;
123 atomic_increment( &use_count_ );
126 bool add_ref_lock() // true on success
128 return atomic_conditional_increment( &use_count_ ) != 0;
131 void release() // nothrow
133 if( atomic_decrement( &use_count_ ) == 0 )
140 void weak_add_ref() // nothrow
142 atomic_increment( &weak_count_ );
145 void weak_release() // nothrow
147 if( atomic_decrement( &weak_count_ ) == 0 )
153 long use_count() const // nothrow
155 return *const_cast<volatile int*>(&use_count_);
159 } // namespace detail
163 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_VACPP_PPC_HPP_INCLUDED