sp_counted_base_acc_ia64.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_ACC_IA64_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_ACC_IA64_HPP_INCLUDED
  3. //
  4. // detail/sp_counted_base_acc_ia64.hpp - aC++ on HP-UX IA64
  5. //
  6. // Copyright 2007 Baruch Zilber
  7. // Copyright 2007 Boris Gubenko
  8. //
  9. // Distributed under the Boost Software License, Version 1.0. (See
  10. // accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. //
  13. //
  14. // Lock-free algorithm by Alexander Terekhov
  15. //
  16. #include <boost/smart_ptr/detail/sp_typeinfo_.hpp>
  17. #include <boost/config.hpp>
  18. #include <machine/sys/inline.h>
  19. namespace boost
  20. {
  21. namespace detail
  22. {
  23. inline void atomic_increment( int * pw )
  24. {
  25. // ++*pw;
  26. _Asm_fetchadd(_FASZ_W, _SEM_REL, pw, +1, _LDHINT_NONE);
  27. }
  28. inline int atomic_decrement( int * pw )
  29. {
  30. // return --*pw;
  31. int r = static_cast<int>(_Asm_fetchadd(_FASZ_W, _SEM_REL, pw, -1, _LDHINT_NONE));
  32. if (1 == r)
  33. {
  34. _Asm_mf();
  35. }
  36. return r - 1;
  37. }
  38. inline int atomic_conditional_increment( int * pw )
  39. {
  40. // if( *pw != 0 ) ++*pw;
  41. // return *pw;
  42. int v = *pw;
  43. for (;;)
  44. {
  45. if (0 == v)
  46. {
  47. return 0;
  48. }
  49. _Asm_mov_to_ar(_AREG_CCV,
  50. v,
  51. (_UP_CALL_FENCE | _UP_SYS_FENCE | _DOWN_CALL_FENCE | _DOWN_SYS_FENCE));
  52. int r = static_cast<int>(_Asm_cmpxchg(_SZ_W, _SEM_ACQ, pw, v + 1, _LDHINT_NONE));
  53. if (r == v)
  54. {
  55. return r + 1;
  56. }
  57. v = r;
  58. }
  59. }
  60. class BOOST_SYMBOL_VISIBLE sp_counted_base
  61. {
  62. private:
  63. sp_counted_base( sp_counted_base const & );
  64. sp_counted_base & operator= ( sp_counted_base const & );
  65. int use_count_; // #shared
  66. int weak_count_; // #weak + (#shared != 0)
  67. public:
  68. sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
  69. {
  70. }
  71. virtual ~sp_counted_base() // nothrow
  72. {
  73. }
  74. // dispose() is called when use_count_ drops to zero, to release
  75. // the resources managed by *this.
  76. virtual void dispose() = 0; // nothrow
  77. // destroy() is called when weak_count_ drops to zero.
  78. virtual void destroy() // nothrow
  79. {
  80. delete this;
  81. }
  82. virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
  83. virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
  84. virtual void * get_untyped_deleter() = 0;
  85. void add_ref_copy()
  86. {
  87. atomic_increment( &use_count_ );
  88. }
  89. bool add_ref_lock() // true on success
  90. {
  91. return atomic_conditional_increment( &use_count_ ) != 0;
  92. }
  93. void release() // nothrow
  94. {
  95. if( atomic_decrement( &use_count_ ) == 0 )
  96. {
  97. dispose();
  98. weak_release();
  99. }
  100. }
  101. void weak_add_ref() // nothrow
  102. {
  103. atomic_increment( &weak_count_ );
  104. }
  105. void weak_release() // nothrow
  106. {
  107. if( atomic_decrement( &weak_count_ ) == 0 )
  108. {
  109. destroy();
  110. }
  111. }
  112. long use_count() const // nothrow
  113. {
  114. return static_cast<int const volatile &>( use_count_ ); // TODO use ld.acq here
  115. }
  116. };
  117. } // namespace detail
  118. } // namespace boost
  119. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_ACC_IA64_HPP_INCLUDED