mirror of
https://github.com/TorqueGameEngines/Torque3D.git
synced 2026-02-13 11:43:49 +00:00
Ported thread pool and refcount tests. Failures, not sure why yet.
This commit is contained in:
parent
5f42f63078
commit
96011623ef
3 changed files with 241 additions and 278 deletions
204
Engine/source/platform/threads/test/threadSafeRefCountTest.cpp
Normal file
204
Engine/source/platform/threads/test/threadSafeRefCountTest.cpp
Normal file
|
|
@ -0,0 +1,204 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// Copyright (c) 2014 GarageGames, LLC
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to
|
||||
// deal in the Software without restriction, including without limitation the
|
||||
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
// sell copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
// IN THE SOFTWARE.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifdef TORQUE_TESTS_ENABLED
|
||||
#include "testing/unitTesting.h"
|
||||
#include "platform/threads/threadSafeRefCount.h"
|
||||
#include "platform/threads/thread.h"
|
||||
#include "core/util/tVector.h"
|
||||
#include "console/console.h"
|
||||
|
||||
TEST(ThreadSafeRefCount, Serial)
|
||||
{
|
||||
struct TestObject : public ThreadSafeRefCount<TestObject>
|
||||
{
|
||||
bool &flag;
|
||||
TestObject(bool &f) : flag(f)
|
||||
{
|
||||
flag = false;
|
||||
}
|
||||
~TestObject()
|
||||
{
|
||||
flag = true;
|
||||
}
|
||||
};
|
||||
typedef ThreadSafeRef<TestObject> TestObjectRef;
|
||||
|
||||
bool deleted = false;
|
||||
TestObjectRef ref1 = new TestObject(deleted);
|
||||
ASSERT_FALSE(deleted);
|
||||
EXPECT_FALSE(ref1->isShared());
|
||||
EXPECT_TRUE(ref1 != NULL);
|
||||
|
||||
TestObjectRef ref2 = ref1;
|
||||
EXPECT_TRUE(ref1->isShared());
|
||||
EXPECT_TRUE(ref2->isShared());
|
||||
EXPECT_EQ(ref1, ref2);
|
||||
|
||||
ref1 = NULL;
|
||||
EXPECT_FALSE(ref2->isShared());
|
||||
|
||||
ref2 = NULL;
|
||||
ASSERT_TRUE(deleted);
|
||||
}
|
||||
|
||||
TEST(ThreadSafeRefCount, Concurrent)
|
||||
{
|
||||
enum
|
||||
{
|
||||
NUM_ADD_REFS_PER_THREAD = 10,
|
||||
NUM_EXTRA_REFS_PER_THREAD = 10,
|
||||
NUM_THREADS = 10
|
||||
};
|
||||
|
||||
class TestObject : public ThreadSafeRefCount<TestObject> {};
|
||||
typedef ThreadSafeRef<TestObject> TestObjectRef;
|
||||
TestObjectRef mRef;
|
||||
|
||||
class TestThread : public Thread
|
||||
{
|
||||
public:
|
||||
TestObjectRef mRef;
|
||||
Vector<TestObjectRef> mExtraRefs;
|
||||
|
||||
TestThread(TestObjectRef ref) : mRef(ref) {}
|
||||
|
||||
void run(void* arg)
|
||||
{
|
||||
if (!arg)
|
||||
{
|
||||
// Create references.
|
||||
for (U32 i = 0; i < NUM_ADD_REFS_PER_THREAD; i++)
|
||||
mRef->addRef();
|
||||
|
||||
mExtraRefs.setSize(NUM_EXTRA_REFS_PER_THREAD);
|
||||
for (U32 i = 0; i < NUM_EXTRA_REFS_PER_THREAD; i++)
|
||||
mExtraRefs[i] = mRef;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Clear references.
|
||||
mExtraRefs.clear();
|
||||
for (U32 i = 0; i < NUM_ADD_REFS_PER_THREAD; i++)
|
||||
mRef->release();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
mRef = new TestObject;
|
||||
EXPECT_EQ(mRef->getRefCount(), 2); // increments of 2
|
||||
|
||||
Vector<TestThread*> threads;
|
||||
threads.setSize(NUM_THREADS);
|
||||
|
||||
// Create threads.
|
||||
for (U32 i = 0; i < NUM_THREADS; i++)
|
||||
threads[i] = new TestThread(mRef);
|
||||
|
||||
// Run phase 1: create references.
|
||||
for (U32 i = 0; i < NUM_THREADS; i++)
|
||||
threads[i]->start(NULL);
|
||||
|
||||
// Wait for completion.
|
||||
for (U32 i = 0; i < NUM_THREADS; i++)
|
||||
threads[i]->join();
|
||||
|
||||
Con::printf("REF: %i", mRef->getRefCount());
|
||||
EXPECT_EQ(mRef->getRefCount(), 2 + ((NUM_ADD_REFS_PER_THREAD + NUM_EXTRA_REFS_PER_THREAD) * NUM_THREADS * 2));
|
||||
|
||||
// Run phase 2: release references.
|
||||
for (U32 i = 0; i < NUM_THREADS; i++)
|
||||
threads[i]->start((void*) 1);
|
||||
|
||||
// Wait for completion.
|
||||
for (U32 i = 0; i < NUM_THREADS; i++)
|
||||
{
|
||||
threads[i]->join();
|
||||
delete threads[i];
|
||||
}
|
||||
|
||||
EXPECT_EQ(mRef->getRefCount(), 2); // increments of two
|
||||
|
||||
mRef = NULL;
|
||||
}
|
||||
|
||||
TEST(ThreadSafeRefCount, Tagging)
|
||||
{
|
||||
struct TestObject : public ThreadSafeRefCount<TestObject> {};
|
||||
typedef ThreadSafeRef<TestObject> TestObjectRef;
|
||||
|
||||
TestObjectRef ref;
|
||||
EXPECT_FALSE(ref.isTagged());
|
||||
EXPECT_TRUE(bool(ref));
|
||||
EXPECT_FALSE(bool(ref.ptr()));
|
||||
|
||||
EXPECT_TRUE(ref.trySetFromTo(ref, NULL));
|
||||
EXPECT_FALSE(ref.isTagged());
|
||||
|
||||
EXPECT_TRUE(ref.trySetFromTo(ref, NULL, TestObjectRef::TAG_Set));
|
||||
EXPECT_TRUE(ref.isTagged());
|
||||
EXPECT_TRUE(ref.trySetFromTo(ref, NULL, TestObjectRef::TAG_Set));
|
||||
EXPECT_TRUE(ref.isTagged());
|
||||
|
||||
EXPECT_TRUE(ref.trySetFromTo(ref, NULL, TestObjectRef::TAG_Unset));
|
||||
EXPECT_FALSE(ref.isTagged());
|
||||
EXPECT_TRUE(ref.trySetFromTo(ref, NULL, TestObjectRef::TAG_Unset));
|
||||
EXPECT_FALSE(ref.isTagged());
|
||||
|
||||
EXPECT_TRUE(ref.trySetFromTo(ref, NULL, TestObjectRef::TAG_SetOrFail));
|
||||
EXPECT_TRUE(ref.isTagged());
|
||||
EXPECT_FALSE(ref.trySetFromTo(ref, NULL, TestObjectRef::TAG_SetOrFail));
|
||||
EXPECT_TRUE(ref.isTagged());
|
||||
EXPECT_FALSE(ref.trySetFromTo(ref, NULL, TestObjectRef::TAG_FailIfSet));
|
||||
|
||||
EXPECT_TRUE(ref.trySetFromTo(ref, NULL, TestObjectRef::TAG_UnsetOrFail));
|
||||
EXPECT_FALSE(ref.isTagged());
|
||||
EXPECT_FALSE(ref.trySetFromTo(ref, NULL, TestObjectRef::TAG_UnsetOrFail));
|
||||
EXPECT_FALSE(ref.isTagged());
|
||||
EXPECT_FALSE(ref.trySetFromTo(ref, NULL, TestObjectRef::TAG_FailIfUnset));
|
||||
|
||||
TestObjectRef objectA = new TestObject;
|
||||
TestObjectRef objectB = new TestObject;
|
||||
|
||||
EXPECT_FALSE(objectA->isShared());
|
||||
EXPECT_FALSE(objectB->isShared());
|
||||
|
||||
ref = objectA;
|
||||
EXPECT_FALSE(ref.isTagged());
|
||||
EXPECT_TRUE(ref == objectA);
|
||||
EXPECT_TRUE(ref == objectA.ptr());
|
||||
EXPECT_TRUE(objectA->isShared());
|
||||
|
||||
EXPECT_TRUE(ref.trySetFromTo(objectA, objectB, TestObjectRef::TAG_Set));
|
||||
EXPECT_TRUE(ref.isTagged());
|
||||
EXPECT_EQ(ref, objectB);
|
||||
EXPECT_EQ(ref, objectB.ptr());
|
||||
EXPECT_TRUE(objectB->isShared());
|
||||
EXPECT_FALSE(objectA->isShared());
|
||||
|
||||
EXPECT_TRUE(ref.trySetFromTo(ref, objectA));
|
||||
EXPECT_TRUE(ref.isTagged());
|
||||
EXPECT_EQ(ref, objectA);
|
||||
EXPECT_EQ(ref, objectA.ptr());
|
||||
}
|
||||
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue