Skip to content

Commit

Permalink
destroy lock on free and fix race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
Soeren Sonnenburg committed Jul 2, 2012
1 parent 8bc0fdf commit 053bdde
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/shogun/lib/SGReferencedData.h
Expand Up @@ -80,10 +80,18 @@ class SGReferencedData
if (m_refcount == NULL)
return -1;

#ifdef HAVE_PTHREAD
PTHREAD_LOCK(&m_refcount->lock);
#endif
int32_t c = m_refcount->rc;
#ifdef HAVE_PTHREAD
PTHREAD_UNLOCK(&m_refcount->lock);
#endif

#ifdef DEBUG_SGVECTOR
SG_SGCDEBUG("ref_count(): refcount %d, data %p\n", m_refcount->rc, this);
SG_SGCDEBUG("ref_count(): refcount %d, data %p\n", c, this);
#endif
return m_refcount->rc;
return c;
}

protected:
Expand All @@ -106,14 +114,14 @@ class SGReferencedData
#ifdef HAVE_PTHREAD
PTHREAD_LOCK(&m_refcount->lock);
#endif
++(m_refcount->rc);
int32_t c = ++(m_refcount->rc);
#ifdef HAVE_PTHREAD
PTHREAD_UNLOCK(&m_refcount->lock);
#endif
#ifdef DEBUG_SGVECTOR
SG_SGCDEBUG("ref() refcount %ld data %p increased\n", m_refcount->rc, this);
SG_SGCDEBUG("ref() refcount %ld data %p increased\n", c, this);
#endif
return m_refcount->rc;
return c;
}

/** decrement reference counter and deallocate object if refcount is zero
Expand All @@ -133,8 +141,7 @@ class SGReferencedData
#ifdef HAVE_PTHREAD
PTHREAD_LOCK(&m_refcount->lock);
#endif
--(m_refcount->rc);
int32_t c = m_refcount->rc;
int32_t c = --(m_refcount->rc);
#ifdef HAVE_PTHREAD
PTHREAD_UNLOCK(&m_refcount->lock);
#endif
Expand All @@ -144,6 +151,9 @@ class SGReferencedData
SG_SGCDEBUG("unref() refcount %d data %p destroying\n", c, this);
#endif
free_data();
#ifdef HAVE_PTHREAD
PTHREAD_LOCK_DESTROY(&m_refcount->lock);
#endif
SG_FREE(m_refcount);
m_refcount=NULL;
return 0;
Expand Down

0 comments on commit 053bdde

Please sign in to comment.