sp_counted_base_gcc_x86.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_X86_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_X86_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_x86.hpp - g++ on 486+ or AMD64
  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 int atomic_exchange_and_add( int * pw, int dv )
  30. {
  31. // int r = *pw;
  32. // *pw += dv;
  33. // return r;
  34. int r;
  35. __asm__ __volatile__
  36. (
  37. "lock\n\t"
  38. "xadd %1, %0":
  39. "=m"( *pw ), "=r"( r ): // outputs (%0, %1)
  40. "m"( *pw ), "1"( dv ): // inputs (%2, %3 == %1)
  41. "memory", "cc" // clobbers
  42. );
  43. return r;
  44. }
  45. inline void atomic_increment( int * pw )
  46. {
  47. //atomic_exchange_and_add( pw, 1 );
  48. __asm__
  49. (
  50. "lock\n\t"
  51. "incl %0":
  52. "=m"( *pw ): // output (%0)
  53. "m"( *pw ): // input (%1)
  54. "cc" // clobbers
  55. );
  56. }
  57. inline int atomic_conditional_increment( int * pw )
  58. {
  59. // int rv = *pw;
  60. // if( rv != 0 ) ++*pw;
  61. // return rv;
  62. int rv, tmp;
  63. __asm__
  64. (
  65. "movl %0, %%eax\n\t"
  66. "0:\n\t"
  67. "test %%eax, %%eax\n\t"
  68. "je 1f\n\t"
  69. "movl %%eax, %2\n\t"
  70. "incl %2\n\t"
  71. "lock\n\t"
  72. "cmpxchgl %2, %0\n\t"
  73. "jne 0b\n\t"
  74. "1:":
  75. "=m"( *pw ), "=&a"( rv ), "=&r"( tmp ): // outputs (%0, %1, %2)
  76. "m"( *pw ): // input (%3)
  77. "cc" // clobbers
  78. );
  79. return rv;
  80. }
  81. class BOOST_SYMBOL_VISIBLE sp_counted_base
  82. {
  83. private:
  84. sp_counted_base( sp_counted_base const & );
  85. sp_counted_base & operator= ( sp_counted_base const & );
  86. int use_count_; // #shared
  87. int weak_count_; // #weak + (#shared != 0)
  88. public:
  89. sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
  90. {
  91. }
  92. virtual ~sp_counted_base() // nothrow
  93. {
  94. }
  95. // dispose() is called when use_count_ drops to zero, to release
  96. // the resources managed by *this.
  97. virtual void dispose() = 0; // nothrow
  98. // destroy() is called when weak_count_ drops to zero.
  99. virtual void destroy() // nothrow
  100. {
  101. delete this;
  102. }
  103. virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
  104. virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
  105. virtual void * get_untyped_deleter() = 0;
  106. void add_ref_copy()
  107. {
  108. atomic_increment( &use_count_ );
  109. }
  110. bool add_ref_lock() // true on success
  111. {
  112. return atomic_conditional_increment( &use_count_ ) != 0;
  113. }
  114. void release() // nothrow
  115. {
  116. if( atomic_exchange_and_add( &use_count_, -1 ) == 1 )
  117. {
  118. dispose();
  119. weak_release();
  120. }
  121. }
  122. void weak_add_ref() // nothrow
  123. {
  124. atomic_increment( &weak_count_ );
  125. }
  126. void weak_release() // nothrow
  127. {
  128. if( atomic_exchange_and_add( &weak_count_, -1 ) == 1 )
  129. {
  130. destroy();
  131. }
  132. }
  133. long use_count() const // nothrow
  134. {
  135. return static_cast<int const volatile &>( use_count_ );
  136. }
  137. };
  138. } // namespace detail
  139. } // namespace boost
  140. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_X86_HPP_INCLUDED