sp_counted_base_aix.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. namespace boost
  26. {
  27. namespace detail
  28. {
  29. inline void atomic_increment( int32_t* pw )
  30. {
  31. // ++*pw;
  32. fetch_and_add( pw, 1 );
  33. }
  34. inline int32_t atomic_decrement( int32_t * pw )
  35. {
  36. // return --*pw;
  37. int32_t originalValue;
  38. __lwsync();
  39. originalValue = fetch_and_add( pw, -1 );
  40. __isync();
  41. return (originalValue - 1);
  42. }
  43. inline int32_t atomic_conditional_increment( int32_t * pw )
  44. {
  45. // if( *pw != 0 ) ++*pw;
  46. // return *pw;
  47. int32_t tmp = fetch_and_add( pw, 0 );
  48. for( ;; )
  49. {
  50. if( tmp == 0 ) return 0;
  51. if( compare_and_swap( pw, &tmp, tmp + 1 ) ) return (tmp + 1);
  52. }
  53. }
  54. class BOOST_SYMBOL_VISIBLE sp_counted_base
  55. {
  56. private:
  57. sp_counted_base( sp_counted_base const & );
  58. sp_counted_base & operator= ( sp_counted_base const & );
  59. int32_t use_count_; // #shared
  60. int32_t weak_count_; // #weak + (#shared != 0)
  61. public:
  62. sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
  63. {
  64. }
  65. virtual ~sp_counted_base() // nothrow
  66. {
  67. }
  68. // dispose() is called when use_count_ drops to zero, to release
  69. // the resources managed by *this.
  70. virtual void dispose() = 0; // nothrow
  71. // destroy() is called when weak_count_ drops to zero.
  72. virtual void destroy() // nothrow
  73. {
  74. delete this;
  75. }
  76. virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
  77. virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
  78. virtual void * get_untyped_deleter() = 0;
  79. void add_ref_copy()
  80. {
  81. atomic_increment( &use_count_ );
  82. }
  83. bool add_ref_lock() // true on success
  84. {
  85. return atomic_conditional_increment( &use_count_ ) != 0;
  86. }
  87. void release() // nothrow
  88. {
  89. if( atomic_decrement( &use_count_ ) == 0 )
  90. {
  91. dispose();
  92. weak_release();
  93. }
  94. }
  95. void weak_add_ref() // nothrow
  96. {
  97. atomic_increment( &weak_count_ );
  98. }
  99. void weak_release() // nothrow
  100. {
  101. if( atomic_decrement( &weak_count_ ) == 0 )
  102. {
  103. destroy();
  104. }
  105. }
  106. long use_count() const // nothrow
  107. {
  108. return fetch_and_add( const_cast<int32_t*>(&use_count_), 0 );
  109. }
  110. };
  111. } // namespace detail
  112. } // namespace boost
  113. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_AIX_HPP_INCLUDED