sp_counted_base_cw_ppc.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_CW_PPC_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_CW_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_cw_ppc.hpp - CodeWarrior 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 CodeWarrior/PowerPC sp_counted_base")
  29. #endif
  30. namespace boost
  31. {
  32. namespace detail
  33. {
  34. inline void atomic_increment( register long * pw )
  35. {
  36. register int a;
  37. asm
  38. {
  39. loop:
  40. lwarx a, 0, pw
  41. addi a, a, 1
  42. stwcx. a, 0, pw
  43. bne- loop
  44. }
  45. }
  46. inline long atomic_decrement( register long * pw )
  47. {
  48. register int a;
  49. asm
  50. {
  51. #if defined(__PPCZen__) || defined(__PPCe500__) || defined(__PPCe500v2__)
  52. msync
  53. #else
  54. sync
  55. #endif
  56. loop:
  57. lwarx a, 0, pw
  58. addi a, a, -1
  59. stwcx. a, 0, pw
  60. bne- loop
  61. isync
  62. }
  63. return a;
  64. }
  65. inline long atomic_conditional_increment( register long * pw )
  66. {
  67. register int a;
  68. asm
  69. {
  70. loop:
  71. lwarx a, 0, pw
  72. cmpwi a, 0
  73. beq store
  74. addi a, a, 1
  75. store:
  76. stwcx. a, 0, pw
  77. bne- loop
  78. }
  79. return a;
  80. }
  81. class BOOST_SYMBOL_VISIBLE sp_counted_base
  82. {
  83. private:
  84. sp_counted_base( sp_counted_base const & );
  85. sp_counted_base & operator= ( sp_counted_base const & );
  86. long use_count_; // #shared
  87. long weak_count_; // #weak + (#shared != 0)
  88. public:
  89. sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
  90. {
  91. }
  92. virtual ~sp_counted_base() // nothrow
  93. {
  94. }
  95. // dispose() is called when use_count_ drops to zero, to release
  96. // the resources managed by *this.
  97. virtual void dispose() = 0; // nothrow
  98. // destroy() is called when weak_count_ drops to zero.
  99. virtual void destroy() // nothrow
  100. {
  101. delete this;
  102. }
  103. virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
  104. virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
  105. virtual void * get_untyped_deleter() = 0;
  106. void add_ref_copy()
  107. {
  108. atomic_increment( &use_count_ );
  109. }
  110. bool add_ref_lock() // true on success
  111. {
  112. return atomic_conditional_increment( &use_count_ ) != 0;
  113. }
  114. void release() // nothrow
  115. {
  116. if( atomic_decrement( &use_count_ ) == 0 )
  117. {
  118. dispose();
  119. weak_release();
  120. }
  121. }
  122. void weak_add_ref() // nothrow
  123. {
  124. atomic_increment( &weak_count_ );
  125. }
  126. void weak_release() // nothrow
  127. {
  128. if( atomic_decrement( &weak_count_ ) == 0 )
  129. {
  130. destroy();
  131. }
  132. }
  133. long use_count() const // nothrow
  134. {
  135. return static_cast<long const volatile &>( use_count_ );
  136. }
  137. };
  138. } // namespace detail
  139. } // namespace boost
  140. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_CW_PPC_HPP_INCLUDED