Skip to content

Commit

Permalink
SGRefObjects not allowed to share RefCount object any more.
Browse files Browse the repository at this point in the history
Fixed unit test; moved threaded stress_test fo RefCount_unittest.
  • Loading branch information
tklein23 committed May 10, 2014
1 parent bdaf87e commit 7b7b515
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/shogun/base/SGRefObject.cpp
Expand Up @@ -30,8 +30,9 @@ SGRefObject::SGRefObject()
SGRefObject::SGRefObject(const SGRefObject& orig)
{
init();
m_refcount = orig.m_refcount;
SG_REF(this);
m_refcount = new RefCount(0);

SG_SGCDEBUG("SGRefObject copied (%p)\n", this)
}

SGRefObject::~SGRefObject()
Expand Down
54 changes: 54 additions & 0 deletions tests/unit/base/RefCount_unittest.cc
@@ -0,0 +1,54 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2014 Thoralf Klein
*/

#include <shogun/lib/RefCount.h>
#include <pthread.h>
#include <gtest/gtest.h>

using namespace shogun;

void * stress_test_helper(void * args)
{
RefCount * rc = (RefCount *) args;

for (index_t i = 0; i < 1000000; i++)
{
rc->ref();
rc->ref();
rc->unref();
rc->unref();
}

pthread_exit(0);
}

TEST(RefCount, stress_test)
{
RefCount * rc = new RefCount(0);
EXPECT_EQ(rc->ref_count(), 0);
rc->ref();
EXPECT_EQ(rc->ref_count(), 1);

pthread_t * threads = new pthread_t[10];

for (index_t i = 0; i < 10; i++)
{
pthread_create(&threads[i], NULL, stress_test_helper, static_cast<void *>(rc));
}

for (index_t i = 0; i < 10; i++)
{
pthread_join(threads[i], NULL);
}

EXPECT_EQ(rc->ref_count(), 1);
rc->unref();
EXPECT_EQ(rc->ref_count(), 0);
delete [] threads;
}
31 changes: 19 additions & 12 deletions tests/unit/base/SGObject_unittest.cc
Expand Up @@ -5,6 +5,7 @@
* (at your option) any later version.
*
* Written (W) 2013 Heiko Strathmann
* Written (W) 2014 Thoralf Klein
*/

#include <shogun/labels/BinaryLabels.h>
Expand Down Expand Up @@ -66,26 +67,32 @@ void* stress_test(void* args)
}

#ifdef USE_REFERENCE_COUNTING
TEST(SGObject,ref_unref)
TEST(SGObject,DISABLED_ref_copy_constructor)
{
CBinaryLabels* labs = new CBinaryLabels(10);
EXPECT_EQ(labs->ref_count(), 0);

SG_REF(labs);
EXPECT_EQ(labs->ref_count(), 1);

// TODO: This causes memory corruptions; disabled test until fixed
CBinaryLabels* labs_2 = new CBinaryLabels(*labs);
SG_UNREF(labs_2);

SG_UNREF(labs);
EXPECT_TRUE(labs == NULL);
}

TEST(SGObject,ref_unref_simple)
{
CBinaryLabels* labs = new CBinaryLabels(10);
EXPECT_EQ(labs->ref_count(), 0);

SG_REF(labs);
EXPECT_EQ(labs->ref_count(), 1);

pthread_t* threads = new pthread_t[10];
for (index_t i=0; i<10; i++)
{
pthread_create(&threads[i], NULL, stress_test, static_cast<void* >(labs));
}
for (index_t i=0; i<10; i++)
{
pthread_join(threads[i], NULL);
//SG_UNREF(labs);
}
SG_UNREF(labs);
EXPECT_TRUE(labs == NULL);
delete [] threads;
}
#endif

Expand Down

0 comments on commit 7b7b515

Please sign in to comment.