weak_ptr made thread safe, shared->weak conversions, lightweight_mutex added.

[SVN r12786]
This commit is contained in:
Peter Dimov committed 2002-02-12 16:55:25 +00:00
1 parent cd8dea78e6
commit 1b69c14f45
8 files changed
+429 -43

No files matched your search

+49 -11
View File
@@ -23,9 +23,10 @@
#endif
#include <boost/checked_delete.hpp>
#include <boost/detail/atomic_count.hpp>
#include <boost/detail/lightweight_mutex.hpp>
#include <functional> // for std::less
#include <functional> // for std::less
#include <exception> // for std::exception
namespace boost
{
@@ -33,11 +34,19 @@ namespace boost
namespace detail
{
class counted_base
class bad_weak_to_shared_cast: public std::exception
{
public:
typedef atomic_count count_type;
virtual char const * what() const throw()
{
return "bad_weak_to_shared_cast";
}
};
class counted_base
{
public:
// pre: initial_use_count <= initial_weak_count
@@ -63,20 +72,31 @@ public:
{
}
void add_ref() // nothrow
void add_ref()
{
lightweight_mutex::scoped_lock lock(mtx_);
if(use_count_ == 0) throw bad_weak_to_shared_cast();
++use_count_;
++weak_count_;
}
void release() // nothrow
{
if(--use_count_ == 0)
long new_use_count;
long new_weak_count;
{
lightweight_mutex::scoped_lock lock(mtx_);
new_use_count = --use_count_;
new_weak_count = --weak_count_;
}
if(new_use_count == 0)
{
dispose();
}
if(--weak_count_ == 0)
if(new_weak_count == 0)
{
// not a direct 'delete this', because the inlined
// release() may use a different heap manager
@@ -86,12 +106,20 @@ public:
void weak_add_ref() // nothrow
{
lightweight_mutex::scoped_lock lock(mtx_);
++weak_count_;
}
void weak_release() // nothrow
{
if(--weak_count_ == 0)
long new_weak_count;
{
lightweight_mutex::scoped_lock lock(mtx_);
new_weak_count = --weak_count_;
}
if(new_weak_count == 0)
{
self_deleter_(this);
}
@@ -99,6 +127,7 @@ public:
long use_count() const // nothrow
{
lightweight_mutex::scoped_lock lock(mtx_);
return use_count_;
}
@@ -114,9 +143,9 @@ private:
// inv: use_count_ <= weak_count_
count_type use_count_;
count_type weak_count_;
long use_count_;
long weak_count_;
mutable lightweight_mutex mtx_;
void (*self_deleter_) (counted_base *);
};
@@ -191,6 +220,8 @@ public:
pi_->add_ref();
}
explicit shared_count(weak_count const & r); // throws bad_weak_to_shared_cast when r.use_count() == 0
shared_count & operator= (shared_count const & r) // nothrow
{
counted_base * tmp = r.pi_;
@@ -235,6 +266,8 @@ private:
counted_base * pi_;
friend class shared_count;
public:
weak_count(): pi_(new counted_base(0, 1)) // can throw
@@ -299,6 +332,11 @@ public:
}
};
inline shared_count::shared_count(weak_count const & r): pi_(r.pi_)
{
pi_->add_ref();
}
} // namespace detail
} // namespace boost