Skip to content

Commit

Permalink
use generic defines for pthread spinlock / mutexes
Browse files Browse the repository at this point in the history
  • Loading branch information
Soeren Sonnenburg committed Aug 22, 2011
1 parent e7aed82 commit 4d7f40a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
10 changes: 8 additions & 2 deletions src/shogun/base/SGObject.cpp
Expand Up @@ -8,6 +8,7 @@
* Copyright (C) 2008-2009 Fraunhofer Institute FIRST and Max Planck Society
*/

#include <shogun/lib/config.h>
#include <shogun/base/SGObject.h>
#include <shogun/io/SGIO.h>
#include <shogun/base/Parallel.h>
Expand Down Expand Up @@ -104,7 +105,6 @@ CSGObject::CSGObject()
{
init();
set_global_objects();
pthread_mutex_init(&m_ref_mutex, NULL);

SG_GCDEBUG("SGObject created (%p)\n", this);
}
Expand All @@ -120,7 +120,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 Expand Up @@ -383,6 +385,10 @@ extern CSet<shogun::MemoryBlock>* sg_mallocs;
#endif
void CSGObject::init()
{
#ifdef HAVE_PTHREAD
PTHREAD_LOCK_INIT(&m_ref_lock);
#endif

#ifdef TRACE_MEMORY_ALLOCS
if (sg_mallocs)
{
Expand Down
50 changes: 30 additions & 20 deletions src/shogun/base/SGObject.h
Expand Up @@ -11,22 +11,17 @@
#ifndef __SGOBJECT_H__
#define __SGOBJECT_H__

#include <shogun/lib/config.h>
#include <shogun/io/SGIO.h>
#include <shogun/lib/DataType.h>
#include <shogun/lib/ShogunException.h>
#include <shogun/lib/memory.h>
#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

#endif //HAVE_PTHREAD
/** \namespace shogun
* @brief all of classes and functions are contained in the shogun namespace
*/
Expand Down Expand Up @@ -96,10 +91,15 @@ class CSGObject
*/
inline int32_t ref()
{
pthread_mutex_lock(&m_ref_mutex);
#ifdef HAVE_PTHREAD
PTHREAD_LOCK(&m_ref_lock);
#endif //HAVE_PTHREAD
++m_refcount;
SG_GCDEBUG("ref() refcount %ld obj %s (%p) increased\n", m_refcount, this->get_name(), this);
pthread_mutex_unlock(&m_ref_mutex);
int32_t count=m_refcount;
#ifdef HAVE_PTHREAD
PTHREAD_UNLOCK(&m_ref_lock);
#endif //HAVE_PTHREAD
SG_GCDEBUG("ref() refcount %ld obj %s (%p) increased\n", count, this->get_name(), this);
return m_refcount;
}

Expand All @@ -109,10 +109,14 @@ class CSGObject
*/
inline int32_t ref_count()
{
pthread_mutex_lock(&m_ref_mutex);
#ifdef HAVE_PTHREAD
PTHREAD_LOCK(&m_ref_lock);
#endif //HAVE_PTHREAD
int32_t count=m_refcount;
#ifdef HAVE_PTHREAD
PTHREAD_UNLOCK(&m_ref_lock);
#endif //HAVE_PTHREAD
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,22 +127,28 @@ class CSGObject
*/
inline int32_t unref()
{
pthread_mutex_lock(&m_ref_mutex);
#ifdef HAVE_PTHREAD
PTHREAD_LOCK(&m_ref_lock);
#endif //HAVE_PTHREAD
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 //HAVE_PTHREAD
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 //HAVE_PTHREAD
return m_refcount;
}
}
#endif
#endif //USE_REFERENCE_COUNTING

/** Returns the name of the SGSerializable instance. It MUST BE
* the CLASS NAME without the prefixed `C'.
Expand Down Expand Up @@ -342,9 +352,9 @@ class CSGObject

int32_t m_refcount;

#ifndef WIN32
pthread_mutex_t m_ref_mutex;
#endif
#ifdef HAVE_PTHREAD
PTHREAD_LOCK_T m_ref_lock;
#endif //HAVE_PTHREAD
};
}
#endif // __SGOBJECT_H__

0 comments on commit 4d7f40a

Please sign in to comment.