sp_counted_base_gcc_mips.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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/config.hpp>
  21. namespace boost
  22. {
  23. namespace detail
  24. {
  25. inline void atomic_increment( int * pw )
  26. {
  27. // ++*pw;
  28. int tmp;
  29. __asm__ __volatile__
  30. (
  31. "0:\n\t"
  32. ".set push\n\t"
  33. #if !defined(__mips_isa_rev) || (__mips_isa_rev < 6)
  34. ".set mips2\n\t"
  35. #endif
  36. "ll %0, %1\n\t"
  37. "addiu %0, 1\n\t"
  38. "sc %0, %1\n\t"
  39. ".set pop\n\t"
  40. "beqz %0, 0b":
  41. "=&r"( tmp ), "=m"( *pw ):
  42. "m"( *pw )
  43. );
  44. }
  45. inline int atomic_decrement( int * pw )
  46. {
  47. // return --*pw;
  48. int rv, tmp;
  49. __asm__ __volatile__
  50. (
  51. "0:\n\t"
  52. ".set push\n\t"
  53. #if !defined(__mips_isa_rev) || (__mips_isa_rev < 6)
  54. ".set mips2\n\t"
  55. #endif
  56. "ll %1, %2\n\t"
  57. "addiu %0, %1, -1\n\t"
  58. "sc %0, %2\n\t"
  59. ".set pop\n\t"
  60. "beqz %0, 0b\n\t"
  61. "addiu %0, %1, -1":
  62. "=&r"( rv ), "=&r"( tmp ), "=m"( *pw ):
  63. "m"( *pw ):
  64. "memory"
  65. );
  66. return rv;
  67. }
  68. inline int atomic_conditional_increment( int * pw )
  69. {
  70. // if( *pw != 0 ) ++*pw;
  71. // return *pw;
  72. int rv, tmp;
  73. __asm__ __volatile__
  74. (
  75. "0:\n\t"
  76. ".set push\n\t"
  77. #if !defined(__mips_isa_rev) || (__mips_isa_rev < 6)
  78. ".set mips2\n\t"
  79. #endif
  80. "ll %0, %2\n\t"
  81. "beqz %0, 1f\n\t"
  82. "addiu %1, %0, 1\n\t"
  83. "sc %1, %2\n\t"
  84. ".set pop\n\t"
  85. "beqz %1, 0b\n\t"
  86. "addiu %0, %0, 1\n\t"
  87. "1:":
  88. "=&r"( rv ), "=&r"( tmp ), "=m"( *pw ):
  89. "m"( *pw ):
  90. "memory"
  91. );
  92. return rv;
  93. }
  94. class BOOST_SYMBOL_VISIBLE sp_counted_base
  95. {
  96. private:
  97. sp_counted_base( sp_counted_base const & );
  98. sp_counted_base & operator= ( sp_counted_base const & );
  99. int use_count_; // #shared
  100. int weak_count_; // #weak + (#shared != 0)
  101. public:
  102. sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
  103. {
  104. }
  105. virtual ~sp_counted_base() // nothrow
  106. {
  107. }
  108. // dispose() is called when use_count_ drops to zero, to release
  109. // the resources managed by *this.
  110. virtual void dispose() = 0; // nothrow
  111. // destroy() is called when weak_count_ drops to zero.
  112. virtual void destroy() // nothrow
  113. {
  114. delete this;
  115. }
  116. virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
  117. virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
  118. virtual void * get_untyped_deleter() = 0;
  119. void add_ref_copy()
  120. {
  121. atomic_increment( &use_count_ );
  122. }
  123. bool add_ref_lock() // true on success
  124. {
  125. return atomic_conditional_increment( &use_count_ ) != 0;
  126. }
  127. void release() // nothrow
  128. {
  129. if( atomic_decrement( &use_count_ ) == 0 )
  130. {
  131. dispose();
  132. weak_release();
  133. }
  134. }
  135. void weak_add_ref() // nothrow
  136. {
  137. atomic_increment( &weak_count_ );
  138. }
  139. void weak_release() // nothrow
  140. {
  141. if( atomic_decrement( &weak_count_ ) == 0 )
  142. {
  143. destroy();
  144. }
  145. }
  146. long use_count() const // nothrow
  147. {
  148. return static_cast<int const volatile &>( use_count_ );
  149. }
  150. };
  151. } // namespace detail
  152. } // namespace boost
  153. #endif // #ifndef BOOST_DETAIL_SP_COUNTED_BASE_GCC_MIPS_HPP_INCLUDED