sp_counted_base_gcc_mips.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #ifndef BOOST_DETAIL_SP_COUNTED_BASE_GCC_MIPS_HPP_INCLUDED
  2. #define BOOST_DETAIL_SP_COUNTED_BASE_GCC_MIPS_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_mips.hpp - g++ on MIPS
  9. //
  10. // Copyright (c) 2009, Spirent Communications, Inc.
  11. //
  12. // Distributed under the Boost Software License, Version 1.0. (See
  13. // accompanying file LICENSE_1_0.txt or copy at
  14. // http://www.boost.org/LICENSE_1_0.txt)
  15. //
  16. //
  17. // Lock-free algorithm by Alexander Terekhov
  18. //
  19. #include <boost/smart_ptr/detail/sp_typeinfo_.hpp>
  20. #include <boost/smart_ptr/detail/sp_obsolete.hpp>
  21. #include <boost/config.hpp>
  22. #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
  23. #include <boost/config/pragma_message.hpp>
  24. BOOST_PRAGMA_MESSAGE("Using g++/MIPS sp_counted_base")
  25. #endif
  26. BOOST_SP_OBSOLETE()
  27. namespace boost
  28. {
  29. namespace detail
  30. {
  31. inline void atomic_increment( int * pw )
  32. {
  33. // ++*pw;
  34. int tmp;
  35. __asm__ __volatile__
  36. (
  37. "0:\n\t"
  38. ".set push\n\t"
  39. #if !defined(__mips_isa_rev) || (__mips_isa_rev < 6)
  40. ".set mips2\n\t"
  41. #endif
  42. "ll %0, %1\n\t"
  43. "addiu %0, 1\n\t"
  44. "sc %0, %1\n\t"
  45. ".set pop\n\t"
  46. "beqz %0, 0b":
  47. "=&r"( tmp ), "=m"( *pw ):
  48. "m"( *pw )
  49. );
  50. }
  51. inline int atomic_decrement( int * pw )
  52. {
  53. // return --*pw;
  54. int rv, tmp;
  55. __asm__ __volatile__
  56. (
  57. "0:\n\t"
  58. ".set push\n\t"
  59. #if !defined(__mips_isa_rev) || (__mips_isa_rev < 6)
  60. ".set mips2\n\t"
  61. #endif
  62. "ll %1, %2\n\t"
  63. "addiu %0, %1, -1\n\t"
  64. "sc %0, %2\n\t"
  65. ".set pop\n\t"
  66. "beqz %0, 0b\n\t"
  67. "addiu %0, %1, -1":
  68. "=&r"( rv ), "=&r"( tmp ), "=m"( *pw ):
  69. "m"( *pw ):
  70. "memory"
  71. );
  72. return rv;
  73. }
  74. inline int atomic_conditional_increment( int * pw )
  75. {
  76. // if( *pw != 0 ) ++*pw;
  77. // return *pw;
  78. int rv, tmp;
  79. __asm__ __volatile__
  80. (
  81. "0:\n\t"
  82. ".set push\n\t"
  83. #if !defined(__mips_isa_rev) || (__mips_isa_rev < 6)
  84. ".set mips2\n\t"
  85. #endif
  86. "ll %0, %2\n\t"
  87. "beqz %0, 1f\n\t"
  88. "addiu %1, %0, 1\n\t"
  89. "sc %1, %2\n\t"
  90. ".set pop\n\t"
  91. "beqz %1, 0b\n\t"
  92. "addiu %0, %0, 1\n\t"
  93. "1:":
  94. "=&r"( rv ), "=&r"( tmp ), "=m"( *pw ):
  95. "m"( *pw ):
  96. "memory"
  97. );
  98. return rv;
  99. }
  100. class BOOST_SYMBOL_VISIBLE sp_counted_base
  101. {
  102. private:
  103. sp_counted_base( sp_counted_base const & );
  104. sp_counted_base & operator= ( sp_counted_base const & );
  105. int use_count_; // #shared
  106. int weak_count_; // #weak + (#shared != 0)
  107. public:
  108. sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
  109. {
  110. }
  111. virtual ~sp_counted_base() // nothrow
  112. {
  113. }
  114. // dispose() is called when use_count_ drops to zero, to release
  115. // the resources managed by *this.
  116. virtual void dispose() = 0; // nothrow
  117. // destroy() is called when weak_count_ drops to zero.
  118. virtual void destroy() // nothrow
  119. {
  120. delete this;
  121. }
  122. virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
  123. virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
  124. virtual void * get_untyped_deleter() = 0;
  125. void add_ref_copy()
  126. {
  127. atomic_increment( &use_count_ );
  128. }
  129. bool add_ref_lock() // true on success
  130. {
  131. return atomic_conditional_increment( &use_count_ ) != 0;
  132. }
  133. void release() // nothrow
  134. {
  135. if( atomic_decrement( &use_count_ ) == 0 )
  136. {
  137. dispose();
  138. weak_release();
  139. }
  140. }
  141. void weak_add_ref() // nothrow
  142. {
  143. atomic_increment( &weak_count_ );
  144. }
  145. void weak_release() // nothrow
  146. {
  147. if( atomic_decrement( &weak_count_ ) == 0 )
  148. {
  149. destroy();
  150. }
  151. }
  152. long use_count() const // nothrow
  153. {
  154. return static_cast<int const volatile &>( use_count_ );
  155. }
  156. };
  157. } // namespace detail
  158. } // namespace boost
  159. #endif // #ifndef BOOST_DETAIL_SP_COUNTED_BASE_GCC_MIPS_HPP_INCLUDED