sp_counted_base_pt.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_PT_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_PT_HPP_INCLUDED
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. //
  8. // detail/sp_counted_base_pt.hpp
  9. //
  10. // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
  11. // Copyright 2004-2005 Peter Dimov
  12. //
  13. // Distributed under the Boost Software License, Version 1.0. (See
  14. // accompanying file LICENSE_1_0.txt or copy at
  15. // http://www.boost.org/LICENSE_1_0.txt)
  16. //
  17. #include <boost/smart_ptr/detail/sp_typeinfo_.hpp>
  18. #include <boost/assert.hpp>
  19. #include <boost/config.hpp>
  20. #include <boost/cstdint.hpp>
  21. #include <pthread.h>
  22. namespace boost
  23. {
  24. namespace detail
  25. {
  26. class BOOST_SYMBOL_VISIBLE sp_counted_base
  27. {
  28. private:
  29. sp_counted_base( sp_counted_base const & );
  30. sp_counted_base & operator= ( sp_counted_base const & );
  31. boost::int_least32_t use_count_; // #shared
  32. boost::int_least32_t weak_count_; // #weak + (#shared != 0)
  33. mutable pthread_mutex_t m_;
  34. public:
  35. sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
  36. {
  37. // HPUX 10.20 / DCE has a nonstandard pthread_mutex_init
  38. #if defined(__hpux) && defined(_DECTHREADS_)
  39. BOOST_VERIFY( pthread_mutex_init( &m_, pthread_mutexattr_default ) == 0 );
  40. #else
  41. BOOST_VERIFY( pthread_mutex_init( &m_, 0 ) == 0 );
  42. #endif
  43. }
  44. virtual ~sp_counted_base() // nothrow
  45. {
  46. BOOST_VERIFY( pthread_mutex_destroy( &m_ ) == 0 );
  47. }
  48. // dispose() is called when use_count_ drops to zero, to release
  49. // the resources managed by *this.
  50. virtual void dispose() = 0; // nothrow
  51. // destroy() is called when weak_count_ drops to zero.
  52. virtual void destroy() // nothrow
  53. {
  54. delete this;
  55. }
  56. virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
  57. virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
  58. virtual void * get_untyped_deleter() = 0;
  59. void add_ref_copy()
  60. {
  61. BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 );
  62. ++use_count_;
  63. BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 );
  64. }
  65. bool add_ref_lock() // true on success
  66. {
  67. BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 );
  68. bool r = use_count_ == 0? false: ( ++use_count_, true );
  69. BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 );
  70. return r;
  71. }
  72. void release() // nothrow
  73. {
  74. BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 );
  75. boost::int_least32_t new_use_count = --use_count_;
  76. BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 );
  77. if( new_use_count == 0 )
  78. {
  79. dispose();
  80. weak_release();
  81. }
  82. }
  83. void weak_add_ref() // nothrow
  84. {
  85. BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 );
  86. ++weak_count_;
  87. BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 );
  88. }
  89. void weak_release() // nothrow
  90. {
  91. BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 );
  92. boost::int_least32_t new_weak_count = --weak_count_;
  93. BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 );
  94. if( new_weak_count == 0 )
  95. {
  96. destroy();
  97. }
  98. }
  99. long use_count() const // nothrow
  100. {
  101. BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 );
  102. boost::int_least32_t r = use_count_;
  103. BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 );
  104. return r;
  105. }
  106. };
  107. } // namespace detail
  108. } // namespace boost
  109. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_PT_HPP_INCLUDED