sp_counted_base_vacpp_ppc.hpp 3.6 KB

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