sp_counted_base_cw_ppc.hpp 3.1 KB

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