sp_counted_base_gcc_x86.hpp 3.8 KB

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