sp_counted_base_pt.hpp 2.9 KB

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