sp_counted_base_aix.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_AIX_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_AIX_HPP_INCLUDED
  3. //
  4. // detail/sp_counted_base_aix.hpp
  5. // based on: detail/sp_counted_base_w32.hpp
  6. //
  7. // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
  8. // Copyright 2004-2005 Peter Dimov
  9. // Copyright 2006 Michael van der Westhuizen
  10. //
  11. // Distributed under the Boost Software License, Version 1.0. (See
  12. // accompanying file LICENSE_1_0.txt or copy at
  13. // http://www.boost.org/LICENSE_1_0.txt)
  14. //
  15. //
  16. // Lock-free algorithm by Alexander Terekhov
  17. //
  18. // Thanks to Ben Hitchings for the #weak + (#shared != 0)
  19. // formulation
  20. //
  21. #include <boost/smart_ptr/detail/sp_typeinfo_.hpp>
  22. #include <boost/config.hpp>
  23. #include <builtins.h>
  24. #include <sys/atomic_op.h>
  25. #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
  26. #include <boost/config/pragma_message.hpp>
  27. BOOST_PRAGMA_MESSAGE("Using AIX sp_counted_base")
  28. #endif
  29. namespace boost
  30. {
  31. namespace detail
  32. {
  33. inline void atomic_increment( int32_t* pw )
  34. {
  35. // ++*pw;
  36. fetch_and_add( pw, 1 );
  37. }
  38. inline int32_t atomic_decrement( int32_t * pw )
  39. {
  40. // return --*pw;
  41. int32_t originalValue;
  42. __lwsync();
  43. originalValue = fetch_and_add( pw, -1 );
  44. __isync();
  45. return (originalValue - 1);
  46. }
  47. inline int32_t atomic_conditional_increment( int32_t * pw )
  48. {
  49. // if( *pw != 0 ) ++*pw;
  50. // return *pw;
  51. int32_t tmp = fetch_and_add( pw, 0 );
  52. for( ;; )
  53. {
  54. if( tmp == 0 ) return 0;
  55. if( compare_and_swap( pw, &tmp, tmp + 1 ) ) return (tmp + 1);
  56. }
  57. }
  58. class BOOST_SYMBOL_VISIBLE sp_counted_base
  59. {
  60. private:
  61. sp_counted_base( sp_counted_base const & );
  62. sp_counted_base & operator= ( sp_counted_base const & );
  63. int32_t use_count_; // #shared
  64. int32_t weak_count_; // #weak + (#shared != 0)
  65. public:
  66. sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
  67. {
  68. }
  69. virtual ~sp_counted_base() // nothrow
  70. {
  71. }
  72. // dispose() is called when use_count_ drops to zero, to release
  73. // the resources managed by *this.
  74. virtual void dispose() = 0; // nothrow
  75. // destroy() is called when weak_count_ drops to zero.
  76. virtual void destroy() // nothrow
  77. {
  78. delete this;
  79. }
  80. virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
  81. virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
  82. virtual void * get_untyped_deleter() = 0;
  83. void add_ref_copy()
  84. {
  85. atomic_increment( &use_count_ );
  86. }
  87. bool add_ref_lock() // true on success
  88. {
  89. return atomic_conditional_increment( &use_count_ ) != 0;
  90. }
  91. void release() // nothrow
  92. {
  93. if( atomic_decrement( &use_count_ ) == 0 )
  94. {
  95. dispose();
  96. weak_release();
  97. }
  98. }
  99. void weak_add_ref() // nothrow
  100. {
  101. atomic_increment( &weak_count_ );
  102. }
  103. void weak_release() // nothrow
  104. {
  105. if( atomic_decrement( &weak_count_ ) == 0 )
  106. {
  107. destroy();
  108. }
  109. }
  110. long use_count() const // nothrow
  111. {
  112. return fetch_and_add( const_cast<int32_t*>(&use_count_), 0 );
  113. }
  114. };
  115. } // namespace detail
  116. } // namespace boost
  117. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_AIX_HPP_INCLUDED