sp_counted_base_gcc_sparc.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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/smart_ptr/detail/sp_obsolete.hpp>
  20. #include <boost/config.hpp>
  21. #include <inttypes.h> // int32_t
  22. #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
  23. #include <boost/config/pragma_message.hpp>
  24. BOOST_PRAGMA_MESSAGE("Using g++/Sparc sp_counted_base")
  25. #endif
  26. BOOST_SP_OBSOLETE()
  27. namespace boost
  28. {
  29. namespace detail
  30. {
  31. inline int32_t compare_and_swap( int32_t * dest_, int32_t compare_, int32_t swap_ )
  32. {
  33. __asm__ __volatile__( "cas [%1], %2, %0"
  34. : "+r" (swap_)
  35. : "r" (dest_), "r" (compare_)
  36. : "memory" );
  37. return swap_;
  38. }
  39. inline int32_t atomic_fetch_and_add( int32_t * pw, int32_t dv )
  40. {
  41. // long r = *pw;
  42. // *pw += dv;
  43. // return r;
  44. for( ;; )
  45. {
  46. int32_t r = *pw;
  47. if( __builtin_expect((compare_and_swap(pw, r, r + dv) == r), 1) )
  48. {
  49. return r;
  50. }
  51. }
  52. }
  53. inline void atomic_increment( int32_t * pw )
  54. {
  55. atomic_fetch_and_add( pw, 1 );
  56. }
  57. inline int32_t atomic_decrement( int32_t * pw )
  58. {
  59. return atomic_fetch_and_add( pw, -1 );
  60. }
  61. inline int32_t atomic_conditional_increment( int32_t * pw )
  62. {
  63. // long r = *pw;
  64. // if( r != 0 ) ++*pw;
  65. // return r;
  66. for( ;; )
  67. {
  68. int32_t r = *pw;
  69. if( r == 0 )
  70. {
  71. return r;
  72. }
  73. if( __builtin_expect( ( compare_and_swap( pw, r, r + 1 ) == r ), 1 ) )
  74. {
  75. return r;
  76. }
  77. }
  78. }
  79. class BOOST_SYMBOL_VISIBLE sp_counted_base
  80. {
  81. private:
  82. sp_counted_base( sp_counted_base const & );
  83. sp_counted_base & operator= ( sp_counted_base const & );
  84. int32_t use_count_; // #shared
  85. int32_t weak_count_; // #weak + (#shared != 0)
  86. public:
  87. sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
  88. {
  89. }
  90. virtual ~sp_counted_base() // nothrow
  91. {
  92. }
  93. // dispose() is called when use_count_ drops to zero, to release
  94. // the resources managed by *this.
  95. virtual void dispose() = 0; // nothrow
  96. // destroy() is called when weak_count_ drops to zero.
  97. virtual void destroy() // nothrow
  98. {
  99. delete this;
  100. }
  101. virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
  102. virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
  103. virtual void * get_untyped_deleter() = 0;
  104. void add_ref_copy()
  105. {
  106. atomic_increment( &use_count_ );
  107. }
  108. bool add_ref_lock() // true on success
  109. {
  110. return atomic_conditional_increment( &use_count_ ) != 0;
  111. }
  112. void release() // nothrow
  113. {
  114. if( atomic_decrement( &use_count_ ) == 1 )
  115. {
  116. dispose();
  117. weak_release();
  118. }
  119. }
  120. void weak_add_ref() // nothrow
  121. {
  122. atomic_increment( &weak_count_ );
  123. }
  124. void weak_release() // nothrow
  125. {
  126. if( atomic_decrement( &weak_count_ ) == 1 )
  127. {
  128. destroy();
  129. }
  130. }
  131. long use_count() const // nothrow
  132. {
  133. return const_cast< int32_t const volatile & >( use_count_ );
  134. }
  135. };
  136. } // namespace detail
  137. } // namespace boost
  138. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_SPARC_HPP_INCLUDED