peerplays-fc/src/shared_ptr.cpp

31 lines
810 B
C++
Raw Normal View History

2012-09-08 02:50:37 +00:00
#include <fc/shared_ptr.hpp>
#include <boost/atomic.hpp>
#include <boost/memory_order.hpp>
2012-09-09 23:44:49 +00:00
#include <assert.h>
2012-09-08 02:50:37 +00:00
namespace fc {
retainable::retainable()
2012-12-18 19:08:40 +00:00
:_ref_count(1) {
static_assert( sizeof(_ref_count) == sizeof(boost::atomic<int32_t>), "failed to reserve enough space" );
}
2012-09-08 02:50:37 +00:00
2012-09-09 23:44:49 +00:00
retainable::~retainable() {
2012-12-18 19:08:40 +00:00
assert( _ref_count <= 0 );
2012-09-09 23:44:49 +00:00
assert( _ref_count == 0 );
}
2012-09-08 02:50:37 +00:00
void retainable::retain() {
((boost::atomic<int32_t>*)&_ref_count)->fetch_add(1, boost::memory_order_relaxed );
}
void retainable::release() {
boost::atomic_thread_fence(boost::memory_order_acquire);
2012-12-18 19:08:40 +00:00
if( 1 == ((boost::atomic<int32_t>*)&_ref_count)->fetch_sub(1, boost::memory_order_release ) ) {
2012-09-08 02:50:37 +00:00
delete this;
}
}
int32_t retainable::retain_count()const {
return _ref_count;
}
}