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/config.hpp>
27 extern "builtin" void __lwsync(void);
28 extern "builtin" void __isync(void);
29 extern "builtin" int __fetch_and_add(volatile int* addr, int val);
30 extern "builtin" int __compare_and_swap(volatile int*, int*, int);
38 inline void atomic_increment( int *pw )
42 __fetch_and_add(pw, 1);
46 inline int atomic_decrement( int *pw )
50 int originalValue = __fetch_and_add(pw, -1);
53 return (originalValue - 1);
56 inline int atomic_conditional_increment( int *pw )
58 // if( *pw != 0 ) ++*pw;
62 int v = *const_cast<volatile int*>(pw);
64 // loop until state is known
67 if (__compare_and_swap(pw, &v, v + 1))
69 __isync(); return (v + 1);
74 class BOOST_SYMBOL_VISIBLE sp_counted_base
78 sp_counted_base( sp_counted_base const & );
79 sp_counted_base & operator= ( sp_counted_base const & );
81 int use_count_; // #shared
82 int weak_count_; // #weak + (#shared != 0)
83 char pad[64] __attribute__((__aligned__(64)));
84 // pad to prevent false sharing
87 sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
91 virtual ~sp_counted_base() // nothrow
95 // dispose() is called when use_count_ drops to zero, to release
96 // the resources managed by *this.
98 virtual void dispose() = 0; // nothrow
100 // destroy() is called when weak_count_ drops to zero.
102 virtual void destroy() // nothrow
107 virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
108 virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
109 virtual void * get_untyped_deleter() = 0;
113 atomic_increment( &use_count_ );
116 bool add_ref_lock() // true on success
118 return atomic_conditional_increment( &use_count_ ) != 0;
121 void release() // nothrow
123 if( atomic_decrement( &use_count_ ) == 0 )
130 void weak_add_ref() // nothrow
132 atomic_increment( &weak_count_ );
135 void weak_release() // nothrow
137 if( atomic_decrement( &weak_count_ ) == 0 )
143 long use_count() const // nothrow
145 return *const_cast<volatile int*>(&use_count_);
149 } // namespace detail
153 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_VACPP_PPC_HPP_INCLUDED