sp_counted_base_pt.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
  23. #include <boost/config/pragma_message.hpp>
  24. BOOST_PRAGMA_MESSAGE("Using pthread_mutex sp_counted_base")
  25. #endif
  26. namespace boost
  27. {
  28. namespace detail
  29. {
  30. class BOOST_SYMBOL_VISIBLE sp_counted_base
  31. {
  32. private:
  33. sp_counted_base( sp_counted_base const & );
  34. sp_counted_base & operator= ( sp_counted_base const & );
  35. boost::int_least32_t use_count_; // #shared
  36. boost::int_least32_t weak_count_; // #weak + (#shared != 0)
  37. mutable pthread_mutex_t m_;
  38. public:
  39. sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
  40. {
  41. // HPUX 10.20 / DCE has a nonstandard pthread_mutex_init
  42. #if defined(__hpux) && defined(_DECTHREADS_)
  43. BOOST_VERIFY( pthread_mutex_init( &m_, pthread_mutexattr_default ) == 0 );
  44. #else
  45. BOOST_VERIFY( pthread_mutex_init( &m_, 0 ) == 0 );
  46. #endif
  47. }
  48. virtual ~sp_counted_base() // nothrow
  49. {
  50. BOOST_VERIFY( pthread_mutex_destroy( &m_ ) == 0 );
  51. }
  52. // dispose() is called when use_count_ drops to zero, to release
  53. // the resources managed by *this.
  54. virtual void dispose() = 0; // nothrow
  55. // destroy() is called when weak_count_ drops to zero.
  56. virtual void destroy() // nothrow
  57. {
  58. delete this;
  59. }
  60. virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
  61. virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
  62. virtual void * get_untyped_deleter() = 0;
  63. void add_ref_copy()
  64. {
  65. BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 );
  66. ++use_count_;
  67. BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 );
  68. }
  69. bool add_ref_lock() // true on success
  70. {
  71. BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 );
  72. bool r = use_count_ == 0? false: ( ++use_count_, true );
  73. BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 );
  74. return r;
  75. }
  76. void release() // nothrow
  77. {
  78. BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 );
  79. boost::int_least32_t new_use_count = --use_count_;
  80. BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 );
  81. if( new_use_count == 0 )
  82. {
  83. dispose();
  84. weak_release();
  85. }
  86. }
  87. void weak_add_ref() // nothrow
  88. {
  89. BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 );
  90. ++weak_count_;
  91. BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 );
  92. }
  93. void weak_release() // nothrow
  94. {
  95. BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 );
  96. boost::int_least32_t new_weak_count = --weak_count_;
  97. BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 );
  98. if( new_weak_count == 0 )
  99. {
  100. destroy();
  101. }
  102. }
  103. long use_count() const // nothrow
  104. {
  105. BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 );
  106. boost::int_least32_t r = use_count_;
  107. BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 );
  108. return r;
  109. }
  110. };
  111. } // namespace detail
  112. } // namespace boost
  113. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_PT_HPP_INCLUDED