sp_counted_base_vacpp_ppc.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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/config.hpp>
  24. extern "builtin" void __lwsync(void);
  25. extern "builtin" void __isync(void);
  26. extern "builtin" int __fetch_and_add(volatile int* addr, int val);
  27. extern "builtin" int __compare_and_swap(volatile int*, int*, int);
  28. namespace boost
  29. {
  30. namespace detail
  31. {
  32. inline void atomic_increment( int *pw )
  33. {
  34. // ++*pw;
  35. __lwsync();
  36. __fetch_and_add(pw, 1);
  37. __isync();
  38. }
  39. inline int atomic_decrement( int *pw )
  40. {
  41. // return --*pw;
  42. __lwsync();
  43. int originalValue = __fetch_and_add(pw, -1);
  44. __isync();
  45. return (originalValue - 1);
  46. }
  47. inline int atomic_conditional_increment( int *pw )
  48. {
  49. // if( *pw != 0 ) ++*pw;
  50. // return *pw;
  51. __lwsync();
  52. int v = *const_cast<volatile int*>(pw);
  53. for (;;)
  54. // loop until state is known
  55. {
  56. if (v == 0) return 0;
  57. if (__compare_and_swap(pw, &v, v + 1))
  58. {
  59. __isync(); return (v + 1);
  60. }
  61. }
  62. }
  63. class BOOST_SYMBOL_VISIBLE sp_counted_base
  64. {
  65. private:
  66. sp_counted_base( sp_counted_base const & );
  67. sp_counted_base & operator= ( sp_counted_base const & );
  68. int use_count_; // #shared
  69. int weak_count_; // #weak + (#shared != 0)
  70. char pad[64] __attribute__((__aligned__(64)));
  71. // pad to prevent false sharing
  72. public:
  73. sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
  74. {
  75. }
  76. virtual ~sp_counted_base() // nothrow
  77. {
  78. }
  79. // dispose() is called when use_count_ drops to zero, to release
  80. // the resources managed by *this.
  81. virtual void dispose() = 0; // nothrow
  82. // destroy() is called when weak_count_ drops to zero.
  83. virtual void destroy() // nothrow
  84. {
  85. delete this;
  86. }
  87. virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
  88. virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
  89. virtual void * get_untyped_deleter() = 0;
  90. void add_ref_copy()
  91. {
  92. atomic_increment( &use_count_ );
  93. }
  94. bool add_ref_lock() // true on success
  95. {
  96. return atomic_conditional_increment( &use_count_ ) != 0;
  97. }
  98. void release() // nothrow
  99. {
  100. if( atomic_decrement( &use_count_ ) == 0 )
  101. {
  102. dispose();
  103. weak_release();
  104. }
  105. }
  106. void weak_add_ref() // nothrow
  107. {
  108. atomic_increment( &weak_count_ );
  109. }
  110. void weak_release() // nothrow
  111. {
  112. if( atomic_decrement( &weak_count_ ) == 0 )
  113. {
  114. destroy();
  115. }
  116. }
  117. long use_count() const // nothrow
  118. {
  119. return *const_cast<volatile int*>(&use_count_);
  120. }
  121. };
  122. } // namespace detail
  123. } // namespace boost
  124. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_VACPP_PPC_HPP_INCLUDED