sp_counted_base_gcc_ppc.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_PPC_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_PPC_HPP_INCLUDED
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. //
  8. // detail/sp_counted_base_gcc_ppc.hpp - g++ on PowerPC
  9. //
  10. // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
  11. // Copyright 2004-2005 Peter Dimov
  12. //
  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)
  16. //
  17. //
  18. // Lock-free algorithm by Alexander Terekhov
  19. //
  20. // Thanks to Ben Hitchings for the #weak + (#shared != 0)
  21. // formulation
  22. //
  23. #include <boost/smart_ptr/detail/sp_typeinfo_.hpp>
  24. #include <boost/smart_ptr/detail/sp_obsolete.hpp>
  25. #include <boost/config.hpp>
  26. #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
  27. #include <boost/config/pragma_message.hpp>
  28. BOOST_PRAGMA_MESSAGE("Using g++/PowerPC sp_counted_base")
  29. #endif
  30. BOOST_SP_OBSOLETE()
  31. namespace boost
  32. {
  33. namespace detail
  34. {
  35. inline void atomic_increment( int * pw )
  36. {
  37. // ++*pw;
  38. int tmp;
  39. __asm__
  40. (
  41. "0:\n\t"
  42. "lwarx %1, 0, %2\n\t"
  43. "addi %1, %1, 1\n\t"
  44. "stwcx. %1, 0, %2\n\t"
  45. "bne- 0b":
  46. "=m"( *pw ), "=&b"( tmp ):
  47. "r"( pw ), "m"( *pw ):
  48. "cc"
  49. );
  50. }
  51. inline int atomic_decrement( int * pw )
  52. {
  53. // return --*pw;
  54. int rv;
  55. __asm__ __volatile__
  56. (
  57. "sync\n\t"
  58. "0:\n\t"
  59. "lwarx %1, 0, %2\n\t"
  60. "addi %1, %1, -1\n\t"
  61. "stwcx. %1, 0, %2\n\t"
  62. "bne- 0b\n\t"
  63. "isync":
  64. "=m"( *pw ), "=&b"( rv ):
  65. "r"( pw ), "m"( *pw ):
  66. "memory", "cc"
  67. );
  68. return rv;
  69. }
  70. inline int atomic_conditional_increment( int * pw )
  71. {
  72. // if( *pw != 0 ) ++*pw;
  73. // return *pw;
  74. int rv;
  75. __asm__
  76. (
  77. "0:\n\t"
  78. "lwarx %1, 0, %2\n\t"
  79. "cmpwi %1, 0\n\t"
  80. "beq 1f\n\t"
  81. "addi %1, %1, 1\n\t"
  82. "1:\n\t"
  83. "stwcx. %1, 0, %2\n\t"
  84. "bne- 0b":
  85. "=m"( *pw ), "=&b"( rv ):
  86. "r"( pw ), "m"( *pw ):
  87. "cc"
  88. );
  89. return rv;
  90. }
  91. class BOOST_SYMBOL_VISIBLE sp_counted_base
  92. {
  93. private:
  94. sp_counted_base( sp_counted_base const & );
  95. sp_counted_base & operator= ( sp_counted_base const & );
  96. int use_count_; // #shared
  97. int weak_count_; // #weak + (#shared != 0)
  98. public:
  99. sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
  100. {
  101. }
  102. virtual ~sp_counted_base() // nothrow
  103. {
  104. }
  105. // dispose() is called when use_count_ drops to zero, to release
  106. // the resources managed by *this.
  107. virtual void dispose() = 0; // nothrow
  108. // destroy() is called when weak_count_ drops to zero.
  109. virtual void destroy() // nothrow
  110. {
  111. delete this;
  112. }
  113. virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
  114. virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
  115. virtual void * get_untyped_deleter() = 0;
  116. void add_ref_copy()
  117. {
  118. atomic_increment( &use_count_ );
  119. }
  120. bool add_ref_lock() // true on success
  121. {
  122. return atomic_conditional_increment( &use_count_ ) != 0;
  123. }
  124. void release() // nothrow
  125. {
  126. if( atomic_decrement( &use_count_ ) == 0 )
  127. {
  128. dispose();
  129. weak_release();
  130. }
  131. }
  132. void weak_add_ref() // nothrow
  133. {
  134. atomic_increment( &weak_count_ );
  135. }
  136. void weak_release() // nothrow
  137. {
  138. if( atomic_decrement( &weak_count_ ) == 0 )
  139. {
  140. destroy();
  141. }
  142. }
  143. long use_count() const // nothrow
  144. {
  145. return static_cast<int const volatile &>( use_count_ );
  146. }
  147. };
  148. } // namespace detail
  149. } // namespace boost
  150. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_PPC_HPP_INCLUDED