sp_counted_base_cw_x86.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_CW_X86_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_CW_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_cw_x86.hpp - CodeWarrion on 486+
  9. //
  10. // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
  11. // Copyright 2004-2005 Peter Dimov
  12. // Copyright 2005 Rene Rivera
  13. //
  14. // Distributed under the Boost Software License, Version 1.0. (See
  15. // accompanying file LICENSE_1_0.txt or copy at
  16. // http://www.boost.org/LICENSE_1_0.txt)
  17. //
  18. //
  19. // Lock-free algorithm by Alexander Terekhov
  20. //
  21. // Thanks to Ben Hitchings for the #weak + (#shared != 0)
  22. // formulation
  23. //
  24. #include <boost/smart_ptr/detail/sp_typeinfo_.hpp>
  25. #include <boost/config.hpp>
  26. namespace boost
  27. {
  28. namespace detail
  29. {
  30. inline int atomic_exchange_and_add( int * pw, int dv )
  31. {
  32. // int r = *pw;
  33. // *pw += dv;
  34. // return r;
  35. asm
  36. {
  37. mov esi, [pw]
  38. mov eax, dv
  39. lock xadd dword ptr [esi], eax
  40. }
  41. }
  42. inline void atomic_increment( int * pw )
  43. {
  44. //atomic_exchange_and_add( pw, 1 );
  45. asm
  46. {
  47. mov esi, [pw]
  48. lock inc dword ptr [esi]
  49. }
  50. }
  51. inline int atomic_conditional_increment( int * pw )
  52. {
  53. // int rv = *pw;
  54. // if( rv != 0 ) ++*pw;
  55. // return rv;
  56. asm
  57. {
  58. mov esi, [pw]
  59. mov eax, dword ptr [esi]
  60. L0:
  61. test eax, eax
  62. je L1
  63. mov ebx, eax
  64. inc ebx
  65. lock cmpxchg dword ptr [esi], ebx
  66. jne L0
  67. L1:
  68. }
  69. }
  70. class BOOST_SYMBOL_VISIBLE sp_counted_base
  71. {
  72. private:
  73. sp_counted_base( sp_counted_base const & );
  74. sp_counted_base & operator= ( sp_counted_base const & );
  75. int use_count_; // #shared
  76. int weak_count_; // #weak + (#shared != 0)
  77. public:
  78. sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
  79. {
  80. }
  81. virtual ~sp_counted_base() // nothrow
  82. {
  83. }
  84. // dispose() is called when use_count_ drops to zero, to release
  85. // the resources managed by *this.
  86. virtual void dispose() = 0; // nothrow
  87. // destroy() is called when weak_count_ drops to zero.
  88. virtual void destroy() // nothrow
  89. {
  90. delete this;
  91. }
  92. virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
  93. virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
  94. virtual void * get_untyped_deleter() = 0;
  95. void add_ref_copy()
  96. {
  97. atomic_increment( &use_count_ );
  98. }
  99. bool add_ref_lock() // true on success
  100. {
  101. return atomic_conditional_increment( &use_count_ ) != 0;
  102. }
  103. void release() // nothrow
  104. {
  105. if( atomic_exchange_and_add( &use_count_, -1 ) == 1 )
  106. {
  107. dispose();
  108. weak_release();
  109. }
  110. }
  111. void weak_add_ref() // nothrow
  112. {
  113. atomic_increment( &weak_count_ );
  114. }
  115. void weak_release() // nothrow
  116. {
  117. if( atomic_exchange_and_add( &weak_count_, -1 ) == 1 )
  118. {
  119. destroy();
  120. }
  121. }
  122. long use_count() const // nothrow
  123. {
  124. return static_cast<int const volatile &>( use_count_ );
  125. }
  126. };
  127. } // namespace detail
  128. } // namespace boost
  129. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_CW_X86_HPP_INCLUDED