Skip to content

Commit

Permalink
SGObject mutex to generic lock transition
Browse files Browse the repository at this point in the history
  • Loading branch information
lisitsyn committed Aug 21, 2011
1 parent 385681b commit ed18465
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
8 changes: 6 additions & 2 deletions src/shogun/base/SGObject.cpp
Expand Up @@ -104,7 +104,9 @@ CSGObject::CSGObject()
{
init();
set_global_objects();
pthread_mutex_init(&m_ref_mutex, NULL);
#ifdef HAVE_PTHREAD
PTHREAD_LOCK_INIT(m_ref_lock);
#endif

SG_GCDEBUG("SGObject created (%p)\n", this);
}
Expand All @@ -120,7 +122,9 @@ CSGObject::~CSGObject()
{
SG_GCDEBUG("SGObject destroyed (%p)\n", this);

pthread_mutex_destroy(&m_ref_mutex);
#ifdef HAVE_PTHREAD
PTHREAD_LOCK_DESTROY(m_ref_lock);
#endif
unset_global_objects();
delete m_parameters;
delete m_model_selection_parameters;
Expand Down
47 changes: 28 additions & 19 deletions src/shogun/base/SGObject.h
Expand Up @@ -18,13 +18,8 @@
#include <shogun/base/Parallel.h>
#include <shogun/base/Version.h>

#ifndef WIN32
#ifdef HAVE_PTHREAD
#include <pthread.h>
#else
#define pthread_mutex_init(x)
#define pthread_mutex_destroy(x)
#define pthread_mutex_lock(x)
#define pthread_mutex_unlock(x)
#endif

/** \namespace shogun
Expand Down Expand Up @@ -96,10 +91,14 @@ class CSGObject
*/
inline int32_t ref()
{
pthread_mutex_lock(&m_ref_mutex);
#ifdef HAVE_PTHREAD
PTHREAD_LOCK(m_ref_lock);
#endif
++m_refcount;
SG_GCDEBUG("ref() refcount %ld obj %s (%p) increased\n", m_refcount, this->get_name(), this);
pthread_mutex_unlock(&m_ref_mutex);
#ifdef HAVE_PTHREAD
PTHREAD_UNLOCK(m_ref_lock);
#endif
return m_refcount;
}

Expand All @@ -109,10 +108,14 @@ class CSGObject
*/
inline int32_t ref_count()
{
pthread_mutex_lock(&m_ref_mutex);
#ifdef HAVE_PTHREAD
PTHREAD_LOCK(m_ref_lock);
#endif
int32_t count=m_refcount;
#ifdef HAVE_PTHREAD
PTHREAD_UNLOCK(m_ref_lock);
#endif
SG_GCDEBUG("ref_count(): refcount %d, obj %s (%p)\n", count, this->get_name(), this);
pthread_mutex_unlock(&m_ref_mutex);
return count;
}

Expand All @@ -123,18 +126,24 @@ class CSGObject
*/
inline int32_t unref()
{
pthread_mutex_lock(&m_ref_mutex);
#ifdef HAVE_PTHREAD
PTHREAD_LOCK(m_ref_lock);
#endif
if (m_refcount==0 || --m_refcount==0)
{
SG_GCDEBUG("unref() refcount %ld, obj %s (%p) destroying\n", m_refcount, this->get_name(), this);
pthread_mutex_unlock(&m_ref_mutex);
#ifdef HAVE_PTHREAD
PTHREAD_UNLOCK(m_ref_lock);
#endif
delete this;
return 0;
}
else
{
SG_GCDEBUG("unref() refcount %ld obj %s (%p) decreased\n", m_refcount, this->get_name(), this);
pthread_mutex_unlock(&m_ref_mutex);
#ifdef HAVE_PTHREAD
PTHREAD_UNLOCK(m_ref_lock);
#endif
return m_refcount;
}
}
Expand Down Expand Up @@ -253,10 +262,10 @@ class CSGObject
index_t get_modsel_param_index(const char* param_name);

#ifdef TRACE_MEMORY_ALLOCS
static void list_memory_allocs()
{
::list_memory_allocs();
}
static void list_memory_allocs()
{
::list_memory_allocs();
}
#endif

protected:
Expand Down Expand Up @@ -342,8 +351,8 @@ class CSGObject

int32_t m_refcount;

#ifndef WIN32
pthread_mutex_t m_ref_mutex;
#ifdef HAVE_PTHREAD
PTHREAD_LOCK_T m_ref_lock;
#endif
};
}
Expand Down

0 comments on commit ed18465

Please sign in to comment.