sp_counted_base_gcc_ia64.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_IA64_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_IA64_HPP_INCLUDED
  3. //
  4. // detail/sp_counted_base_gcc_ia64.hpp - g++ on IA64
  5. //
  6. // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
  7. // Copyright 2004-2006 Peter Dimov
  8. // Copyright 2005 Ben Hutchings
  9. //
  10. // Distributed under the Boost Software License, Version 1.0. (See
  11. // accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. //
  14. //
  15. // Lock-free algorithm by Alexander Terekhov
  16. //
  17. #include <boost/smart_ptr/detail/sp_typeinfo_.hpp>
  18. #include <boost/config.hpp>
  19. namespace boost
  20. {
  21. namespace detail
  22. {
  23. inline void atomic_increment( int * pw )
  24. {
  25. // ++*pw;
  26. int tmp;
  27. // No barrier is required here but fetchadd always has an acquire or
  28. // release barrier associated with it. We choose release as it should be
  29. // cheaper.
  30. __asm__ ("fetchadd4.rel %0=%1,1" :
  31. "=r"(tmp), "=m"(*pw) :
  32. "m"( *pw ));
  33. }
  34. inline int atomic_decrement( int * pw )
  35. {
  36. // return --*pw;
  37. int rv;
  38. __asm__ (" fetchadd4.rel %0=%1,-1 ;; \n"
  39. " cmp.eq p7,p0=1,%0 ;; \n"
  40. "(p7) ld4.acq %0=%1 " :
  41. "=&r"(rv), "=m"(*pw) :
  42. "m"( *pw ) :
  43. "p7");
  44. return rv;
  45. }
  46. inline int atomic_conditional_increment( int * pw )
  47. {
  48. // if( *pw != 0 ) ++*pw;
  49. // return *pw;
  50. int rv, tmp, tmp2;
  51. __asm__ ("0: ld4 %0=%3 ;; \n"
  52. " cmp.eq p7,p0=0,%0 ;; \n"
  53. "(p7) br.cond.spnt 1f \n"
  54. " mov ar.ccv=%0 \n"
  55. " add %1=1,%0 ;; \n"
  56. " cmpxchg4.acq %2=%3,%1,ar.ccv ;; \n"
  57. " cmp.ne p7,p0=%0,%2 ;; \n"
  58. "(p7) br.cond.spnt 0b \n"
  59. " mov %0=%1 ;; \n"
  60. "1:" :
  61. "=&r"(rv), "=&r"(tmp), "=&r"(tmp2), "=m"(*pw) :
  62. "m"( *pw ) :
  63. "ar.ccv", "p7");
  64. return rv;
  65. }
  66. class BOOST_SYMBOL_VISIBLE sp_counted_base
  67. {
  68. private:
  69. sp_counted_base( sp_counted_base const & );
  70. sp_counted_base & operator= ( sp_counted_base const & );
  71. int use_count_; // #shared
  72. int weak_count_; // #weak + (#shared != 0)
  73. public:
  74. sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
  75. {
  76. }
  77. virtual ~sp_counted_base() // nothrow
  78. {
  79. }
  80. // dispose() is called when use_count_ drops to zero, to release
  81. // the resources managed by *this.
  82. virtual void dispose() = 0; // nothrow
  83. // destroy() is called when weak_count_ drops to zero.
  84. virtual void destroy() // nothrow
  85. {
  86. delete this;
  87. }
  88. virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
  89. virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
  90. virtual void * get_untyped_deleter() = 0;
  91. void add_ref_copy()
  92. {
  93. atomic_increment( &use_count_ );
  94. }
  95. bool add_ref_lock() // true on success
  96. {
  97. return atomic_conditional_increment( &use_count_ ) != 0;
  98. }
  99. void release() // nothrow
  100. {
  101. if( atomic_decrement( &use_count_ ) == 0 )
  102. {
  103. dispose();
  104. weak_release();
  105. }
  106. }
  107. void weak_add_ref() // nothrow
  108. {
  109. atomic_increment( &weak_count_ );
  110. }
  111. void weak_release() // nothrow
  112. {
  113. if( atomic_decrement( &weak_count_ ) == 0 )
  114. {
  115. destroy();
  116. }
  117. }
  118. long use_count() const // nothrow
  119. {
  120. return static_cast<int const volatile &>( use_count_ ); // TODO use ld.acq here
  121. }
  122. };
  123. } // namespace detail
  124. } // namespace boost
  125. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_IA64_HPP_INCLUDED