sp_counted_base_gcc_ia64.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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/smart_ptr/detail/sp_obsolete.hpp>
  19. #include <boost/config.hpp>
  20. #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
  21. #include <boost/config/pragma_message.hpp>
  22. BOOST_PRAGMA_MESSAGE("Using g++/IA64 sp_counted_base")
  23. #endif
  24. BOOST_SP_OBSOLETE()
  25. namespace boost
  26. {
  27. namespace detail
  28. {
  29. inline void atomic_increment( int * pw )
  30. {
  31. // ++*pw;
  32. int tmp;
  33. // No barrier is required here but fetchadd always has an acquire or
  34. // release barrier associated with it. We choose release as it should be
  35. // cheaper.
  36. __asm__ ("fetchadd4.rel %0=%1,1" :
  37. "=r"(tmp), "=m"(*pw) :
  38. "m"( *pw ));
  39. }
  40. inline int atomic_decrement( int * pw )
  41. {
  42. // return --*pw;
  43. int rv;
  44. __asm__ (" fetchadd4.rel %0=%1,-1 ;; \n"
  45. " cmp.eq p7,p0=1,%0 ;; \n"
  46. "(p7) ld4.acq %0=%1 " :
  47. "=&r"(rv), "=m"(*pw) :
  48. "m"( *pw ) :
  49. "p7");
  50. return rv;
  51. }
  52. inline int atomic_conditional_increment( int * pw )
  53. {
  54. // if( *pw != 0 ) ++*pw;
  55. // return *pw;
  56. int rv, tmp, tmp2;
  57. __asm__ ("0: ld4 %0=%3 ;; \n"
  58. " cmp.eq p7,p0=0,%0 ;; \n"
  59. "(p7) br.cond.spnt 1f \n"
  60. " mov ar.ccv=%0 \n"
  61. " add %1=1,%0 ;; \n"
  62. " cmpxchg4.acq %2=%3,%1,ar.ccv ;; \n"
  63. " cmp.ne p7,p0=%0,%2 ;; \n"
  64. "(p7) br.cond.spnt 0b \n"
  65. " mov %0=%1 ;; \n"
  66. "1:" :
  67. "=&r"(rv), "=&r"(tmp), "=&r"(tmp2), "=m"(*pw) :
  68. "m"( *pw ) :
  69. "ar.ccv", "p7");
  70. return rv;
  71. }
  72. class BOOST_SYMBOL_VISIBLE sp_counted_base
  73. {
  74. private:
  75. sp_counted_base( sp_counted_base const & );
  76. sp_counted_base & operator= ( sp_counted_base const & );
  77. int use_count_; // #shared
  78. int weak_count_; // #weak + (#shared != 0)
  79. public:
  80. sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
  81. {
  82. }
  83. virtual ~sp_counted_base() // nothrow
  84. {
  85. }
  86. // dispose() is called when use_count_ drops to zero, to release
  87. // the resources managed by *this.
  88. virtual void dispose() = 0; // nothrow
  89. // destroy() is called when weak_count_ drops to zero.
  90. virtual void destroy() // nothrow
  91. {
  92. delete this;
  93. }
  94. virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
  95. virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
  96. virtual void * get_untyped_deleter() = 0;
  97. void add_ref_copy()
  98. {
  99. atomic_increment( &use_count_ );
  100. }
  101. bool add_ref_lock() // true on success
  102. {
  103. return atomic_conditional_increment( &use_count_ ) != 0;
  104. }
  105. void release() // nothrow
  106. {
  107. if( atomic_decrement( &use_count_ ) == 0 )
  108. {
  109. dispose();
  110. weak_release();
  111. }
  112. }
  113. void weak_add_ref() // nothrow
  114. {
  115. atomic_increment( &weak_count_ );
  116. }
  117. void weak_release() // nothrow
  118. {
  119. if( atomic_decrement( &weak_count_ ) == 0 )
  120. {
  121. destroy();
  122. }
  123. }
  124. long use_count() const // nothrow
  125. {
  126. return static_cast<int const volatile &>( use_count_ ); // TODO use ld.acq here
  127. }
  128. };
  129. } // namespace detail
  130. } // namespace boost
  131. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_IA64_HPP_INCLUDED