Skip to content

Commit

Permalink
Use atomics for the reference counting. Fixes #4.
Browse files Browse the repository at this point in the history
  • Loading branch information
blue42u committed Apr 27, 2019
1 parent d9e672c commit 09ec532
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
13 changes: 12 additions & 1 deletion symtabAPI/h/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,18 @@ class SYMTAB_EXPORT Type : public Serializable, public TYPE_ANNOTATABLE_CLASS
**/
bool updatingSize;

unsigned int refCount;
// Boost's atomic doesn't support operator =, although std::atomic does.
// This wrapper class makes up for the deficiency
template<typename T>
struct stdatomic : public boost::atomic<T> {
stdatomic(const T& v) : boost::atomic<T>(v) {};
~stdatomic() {};
stdatomic& operator=(const stdatomic<T>& other) {
this->store(other.load());
return *this;
}
};
stdatomic<unsigned int> refCount;

static boost::atomic<typeId_t> USER_TYPE_ID;

Expand Down
10 changes: 6 additions & 4 deletions symtabAPI/src/Type.C
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,15 @@ bool Type::setSize(unsigned int size)

void Type::incrRefCount()
{
++refCount;
refCount.fetch_add(1, boost::memory_order_relaxed);
}

void Type::decrRefCount()
{
--refCount;
if(refCount == 0) delete this;
if(refCount.fetch_sub(1, boost::memory_order_relaxed) == 1) {
boost::atomic_thread_fence(boost::memory_order_acquire);
delete this;
}
}

std::string &Type::getName()
Expand Down Expand Up @@ -1921,7 +1923,7 @@ Serializable * Type::serialize_impl(SerializerBase *s, const char *tag) THROW_SP
if (s->isInput())
{
updatingSize = false;
refCount = 0;
refCount.store(0, boost::memory_order_relaxed);

// Ensure that unique type id is the next (increasingly negative) type ID available for user defined types.
updateUniqueTypeId(ID_);
Expand Down

0 comments on commit 09ec532

Please sign in to comment.