sp_counted_base_gcc_ppc.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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/config.hpp>
  25. namespace boost
  26. {
  27. namespace detail
  28. {
  29. inline void atomic_increment( int * pw )
  30. {
  31. // ++*pw;
  32. int tmp;
  33. __asm__
  34. (
  35. "0:\n\t"
  36. "lwarx %1, 0, %2\n\t"
  37. "addi %1, %1, 1\n\t"
  38. "stwcx. %1, 0, %2\n\t"
  39. "bne- 0b":
  40. "=m"( *pw ), "=&b"( tmp ):
  41. "r"( pw ), "m"( *pw ):
  42. "cc"
  43. );
  44. }
  45. inline int atomic_decrement( int * pw )
  46. {
  47. // return --*pw;
  48. int rv;
  49. __asm__ __volatile__
  50. (
  51. "sync\n\t"
  52. "0:\n\t"
  53. "lwarx %1, 0, %2\n\t"
  54. "addi %1, %1, -1\n\t"
  55. "stwcx. %1, 0, %2\n\t"
  56. "bne- 0b\n\t"
  57. "isync":
  58. "=m"( *pw ), "=&b"( rv ):
  59. "r"( pw ), "m"( *pw ):
  60. "memory", "cc"
  61. );
  62. return rv;
  63. }
  64. inline int atomic_conditional_increment( int * pw )
  65. {
  66. // if( *pw != 0 ) ++*pw;
  67. // return *pw;
  68. int rv;
  69. __asm__
  70. (
  71. "0:\n\t"
  72. "lwarx %1, 0, %2\n\t"
  73. "cmpwi %1, 0\n\t"
  74. "beq 1f\n\t"
  75. "addi %1, %1, 1\n\t"
  76. "1:\n\t"
  77. "stwcx. %1, 0, %2\n\t"
  78. "bne- 0b":
  79. "=m"( *pw ), "=&b"( rv ):
  80. "r"( pw ), "m"( *pw ):
  81. "cc"
  82. );
  83. return rv;
  84. }
  85. class BOOST_SYMBOL_VISIBLE sp_counted_base
  86. {
  87. private:
  88. sp_counted_base( sp_counted_base const & );
  89. sp_counted_base & operator= ( sp_counted_base const & );
  90. int use_count_; // #shared
  91. int weak_count_; // #weak + (#shared != 0)
  92. public:
  93. sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
  94. {
  95. }
  96. virtual ~sp_counted_base() // nothrow
  97. {
  98. }
  99. // dispose() is called when use_count_ drops to zero, to release
  100. // the resources managed by *this.
  101. virtual void dispose() = 0; // nothrow
  102. // destroy() is called when weak_count_ drops to zero.
  103. virtual void destroy() // nothrow
  104. {
  105. delete this;
  106. }
  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;
  110. void add_ref_copy()
  111. {
  112. atomic_increment( &use_count_ );
  113. }
  114. bool add_ref_lock() // true on success
  115. {
  116. return atomic_conditional_increment( &use_count_ ) != 0;
  117. }
  118. void release() // nothrow
  119. {
  120. if( atomic_decrement( &use_count_ ) == 0 )
  121. {
  122. dispose();
  123. weak_release();
  124. }
  125. }
  126. void weak_add_ref() // nothrow
  127. {
  128. atomic_increment( &weak_count_ );
  129. }
  130. void weak_release() // nothrow
  131. {
  132. if( atomic_decrement( &weak_count_ ) == 0 )
  133. {
  134. destroy();
  135. }
  136. }
  137. long use_count() const // nothrow
  138. {
  139. return static_cast<int const volatile &>( use_count_ );
  140. }
  141. };
  142. } // namespace detail
  143. } // namespace boost
  144. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_PPC_HPP_INCLUDED