sp_counted_base_acc_ia64.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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/smart_ptr/detail/sp_obsolete.hpp>
  18. #include <boost/config.hpp>
  19. #include <machine/sys/inline.h>
  20. #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
  21. #include <boost/config/pragma_message.hpp>
  22. BOOST_PRAGMA_MESSAGE("Using HP aCC++/HP-UX/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. _Asm_fetchadd(_FASZ_W, _SEM_REL, pw, +1, _LDHINT_NONE);
  33. }
  34. inline int atomic_decrement( int * pw )
  35. {
  36. // return --*pw;
  37. int r = static_cast<int>(_Asm_fetchadd(_FASZ_W, _SEM_REL, pw, -1, _LDHINT_NONE));
  38. if (1 == r)
  39. {
  40. _Asm_mf();
  41. }
  42. return r - 1;
  43. }
  44. inline int atomic_conditional_increment( int * pw )
  45. {
  46. // if( *pw != 0 ) ++*pw;
  47. // return *pw;
  48. int v = *pw;
  49. for (;;)
  50. {
  51. if (0 == v)
  52. {
  53. return 0;
  54. }
  55. _Asm_mov_to_ar(_AREG_CCV,
  56. v,
  57. (_UP_CALL_FENCE | _UP_SYS_FENCE | _DOWN_CALL_FENCE | _DOWN_SYS_FENCE));
  58. int r = static_cast<int>(_Asm_cmpxchg(_SZ_W, _SEM_ACQ, pw, v + 1, _LDHINT_NONE));
  59. if (r == v)
  60. {
  61. return r + 1;
  62. }
  63. v = r;
  64. }
  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_ACC_IA64_HPP_INCLUDED