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/detail/sp_typeinfo.hpp>
26 extern "builtin" void __lwsync(void);
27 extern "builtin" void __isync(void);
28 extern "builtin" int __fetch_and_add(volatile int* addr, int val);
29 extern "builtin" int __compare_and_swap(volatile int*, int*, int);
37 inline void atomic_increment( int *pw )
41 __fetch_and_add(pw, 1);
45 inline int atomic_decrement( int *pw )
49 int originalValue = __fetch_and_add(pw, -1);
52 return (originalValue - 1);
55 inline int atomic_conditional_increment( int *pw )
57 // if( *pw != 0 ) ++*pw;
61 int v = *const_cast<volatile int*>(pw);
63 // loop until state is known
66 if (__compare_and_swap(pw, &v, v + 1))
68 __isync(); return (v + 1);
77 sp_counted_base( sp_counted_base const & );
78 sp_counted_base & operator= ( sp_counted_base const & );
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
86 sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
90 virtual ~sp_counted_base() // nothrow
94 // dispose() is called when use_count_ drops to zero, to release
95 // the resources managed by *this.
97 virtual void dispose() = 0; // nothrow
99 // destroy() is called when weak_count_ drops to zero.
101 virtual void destroy() // nothrow
106 virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
107 virtual void * get_untyped_deleter() = 0;
111 atomic_increment( &use_count_ );
114 bool add_ref_lock() // true on success
116 return atomic_conditional_increment( &use_count_ ) != 0;
119 void release() // nothrow
121 if( atomic_decrement( &use_count_ ) == 0 )
128 void weak_add_ref() // nothrow
130 atomic_increment( &weak_count_ );
133 void weak_release() // nothrow
135 if( atomic_decrement( &weak_count_ ) == 0 )
141 long use_count() const // nothrow
143 return *const_cast<volatile int*>(&use_count_);
147 } // namespace detail
151 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_VACPP_PPC_HPP_INCLUDED