sp_counted_base_gcc_sparc.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_SPARC_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_SPARC_HPP_INCLUDED
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. // detail/sp_counted_base_gcc_sparc.hpp - g++ on Sparc V8+
  8. //
  9. // Copyright (c) 2006 Piotr Wyderski
  10. // Copyright (c) 2006 Tomas Puverle
  11. // Copyright (c) 2006 Peter Dimov
  12. //
  13. // Distributed under the Boost Software License, Version 1.0.
  14. // See accompanying file LICENSE_1_0.txt or copy at
  15. // http://www.boost.org/LICENSE_1_0.txt
  16. //
  17. // Thanks to Michael van der Westhuizen
  18. #include <boost/smart_ptr/detail/sp_typeinfo_.hpp>
  19. #include <boost/config.hpp>
  20. #include <inttypes.h> // int32_t
  21. namespace boost
  22. {
  23. namespace detail
  24. {
  25. inline int32_t compare_and_swap( int32_t * dest_, int32_t compare_, int32_t swap_ )
  26. {
  27. __asm__ __volatile__( "cas [%1], %2, %0"
  28. : "+r" (swap_)
  29. : "r" (dest_), "r" (compare_)
  30. : "memory" );
  31. return swap_;
  32. }
  33. inline int32_t atomic_fetch_and_add( int32_t * pw, int32_t dv )
  34. {
  35. // long r = *pw;
  36. // *pw += dv;
  37. // return r;
  38. for( ;; )
  39. {
  40. int32_t r = *pw;
  41. if( __builtin_expect((compare_and_swap(pw, r, r + dv) == r), 1) )
  42. {
  43. return r;
  44. }
  45. }
  46. }
  47. inline void atomic_increment( int32_t * pw )
  48. {
  49. atomic_fetch_and_add( pw, 1 );
  50. }
  51. inline int32_t atomic_decrement( int32_t * pw )
  52. {
  53. return atomic_fetch_and_add( pw, -1 );
  54. }
  55. inline int32_t atomic_conditional_increment( int32_t * pw )
  56. {
  57. // long r = *pw;
  58. // if( r != 0 ) ++*pw;
  59. // return r;
  60. for( ;; )
  61. {
  62. int32_t r = *pw;
  63. if( r == 0 )
  64. {
  65. return r;
  66. }
  67. if( __builtin_expect( ( compare_and_swap( pw, r, r + 1 ) == r ), 1 ) )
  68. {
  69. return r;
  70. }
  71. }
  72. }
  73. class BOOST_SYMBOL_VISIBLE sp_counted_base
  74. {
  75. private:
  76. sp_counted_base( sp_counted_base const & );
  77. sp_counted_base & operator= ( sp_counted_base const & );
  78. int32_t use_count_; // #shared
  79. int32_t weak_count_; // #weak + (#shared != 0)
  80. public:
  81. sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
  82. {
  83. }
  84. virtual ~sp_counted_base() // nothrow
  85. {
  86. }
  87. // dispose() is called when use_count_ drops to zero, to release
  88. // the resources managed by *this.
  89. virtual void dispose() = 0; // nothrow
  90. // destroy() is called when weak_count_ drops to zero.
  91. virtual void destroy() // nothrow
  92. {
  93. delete this;
  94. }
  95. virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
  96. virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
  97. virtual void * get_untyped_deleter() = 0;
  98. void add_ref_copy()
  99. {
  100. atomic_increment( &use_count_ );
  101. }
  102. bool add_ref_lock() // true on success
  103. {
  104. return atomic_conditional_increment( &use_count_ ) != 0;
  105. }
  106. void release() // nothrow
  107. {
  108. if( atomic_decrement( &use_count_ ) == 1 )
  109. {
  110. dispose();
  111. weak_release();
  112. }
  113. }
  114. void weak_add_ref() // nothrow
  115. {
  116. atomic_increment( &weak_count_ );
  117. }
  118. void weak_release() // nothrow
  119. {
  120. if( atomic_decrement( &weak_count_ ) == 1 )
  121. {
  122. destroy();
  123. }
  124. }
  125. long use_count() const // nothrow
  126. {
  127. return const_cast< int32_t const volatile & >( use_count_ );
  128. }
  129. };
  130. } // namespace detail
  131. } // namespace boost
  132. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_SPARC_HPP_INCLUDED