1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
/*=========================================================================
Program: OpenIGTLink Library
Language: C++
Copyright (c) Insight Software Consortium. All rights reserved.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#include "igtlConditionVariable.h"
#include "igtlMultiThreader.h"
#include "igtlOSUtil.h"
#include "igtlTestConfig.h"
#include "string.h"
void* TestThreadCounter(void* ptr);
void* TestThreadWaiting1(void* ptr);
void* TestThreadWaiting2(void* ptr);
typedef struct {
igtl::SimpleMutexLock* glock;
igtl::ConditionVariable::Pointer conditionVar;
bool isBroadCast;
int iFinalCount;
bool condition;
std::vector<bool> threads;
} ThreadData;
TEST(ConditionVariableTest, SingleChildThreadTest)
{
// From this test we can see that, the wait function really works,
// the main thread waits until the spawned thread is finished, otherwize the icount1 will be 0.
igtl::ConditionVariable::Pointer conditionVar = igtl::ConditionVariable::New();
igtl::SimpleMutexLock *localMutex = new igtl::SimpleMutexLock();
ThreadData td;
td.threads = std::vector<bool>(1);
td.conditionVar = conditionVar;
td.glock = localMutex;
td.isBroadCast = false;
td.iFinalCount = 0;
td.condition = false;
igtl::MultiThreader::Pointer threader = igtl::MultiThreader::New();
threader->SpawnThread((igtl::ThreadFunctionType) &TestThreadWaiting1, &td);
threader->SpawnThread((igtl::ThreadFunctionType) &TestThreadCounter, &td);
while (td.threads[0] == false)
{
igtl::Sleep(20);
}
std::cerr<<"The child thread is released";
EXPECT_EQ(td.iFinalCount, 10);
}
TEST(ConditionVariableTest, MultiChildThreadTest)
{
igtl::ConditionVariable::Pointer conditionVar = igtl::ConditionVariable::New();
igtl::SimpleMutexLock *localMutex = new igtl::SimpleMutexLock();
ThreadData td;
td.conditionVar = conditionVar;
td.glock = localMutex;
td.isBroadCast = false;
td.threads = std::vector<bool>(2,false);
td.iFinalCount = 0;
td.condition = false;
igtl::MultiThreader::Pointer threader = igtl::MultiThreader::New();
threader->SpawnThread((igtl::ThreadFunctionType) &TestThreadWaiting1, &td);
threader->SpawnThread((igtl::ThreadFunctionType) &TestThreadCounter, &td);
igtl::Sleep(1000);
threader->SpawnThread((igtl::ThreadFunctionType) &TestThreadWaiting2, &td);
threader->SpawnThread((igtl::ThreadFunctionType) &TestThreadCounter, &td);
while (td.threads[0] == false || td.threads[1] == false)
igtl::Sleep(20);
EXPECT_EQ(td.iFinalCount, 20);
std::cerr<<"The child threads are released";
}
TEST(ConditionVariableTest, Broadcast)
{
igtl::ConditionVariable::Pointer conditionVar = igtl::ConditionVariable::New();
igtl::SimpleMutexLock *localMutex = new igtl::SimpleMutexLock();
ThreadData td;
td.conditionVar = conditionVar;
td.glock = localMutex;
td.isBroadCast = true;
td.threads = std::vector<bool>(2);
td.iFinalCount = 0;
td.condition = false;
igtl::MultiThreader::Pointer threader = igtl::MultiThreader::New();
threader->SpawnThread((igtl::ThreadFunctionType) &TestThreadWaiting1, &td);
threader->SpawnThread((igtl::ThreadFunctionType) &TestThreadCounter, &td);
igtl::Sleep(1000);
threader->SpawnThread((igtl::ThreadFunctionType) &TestThreadWaiting2, &td);
threader->SpawnThread((igtl::ThreadFunctionType) &TestThreadCounter, &td);
while (td.threads[0] == false || td.threads[1] == false)
igtl::Sleep(20);
EXPECT_LT(td.iFinalCount, 20);
std::cerr<<"The child threads are released";
}
void* TestThreadWaiting1(void* ptr)
{
igtl::MultiThreader::ThreadInfo* info =
static_cast<igtl::MultiThreader::ThreadInfo*>(ptr);
ThreadData* td = static_cast<ThreadData*>(info->UserData);
td->glock->Lock();
while(!td->condition)
{
td->conditionVar->Wait(td->glock);
}
td->threads[0]= true;
td->glock->Unlock();
return NULL;
}
void* TestThreadWaiting2(void* ptr)
{
igtl::MultiThreader::ThreadInfo* info =
static_cast<igtl::MultiThreader::ThreadInfo*>(ptr);
ThreadData* td = static_cast<ThreadData*>(info->UserData);
td->glock->Lock();
while(!td->condition)
{
td->conditionVar->Wait(td->glock);
}
td->threads[1]= true;
td->glock->Unlock();
return NULL;
}
void* TestThreadCounter(void* ptr)
{
//------------------------------------------------------------
// Get thread information
igtl::MultiThreader::ThreadInfo* info =
static_cast<igtl::MultiThreader::ThreadInfo*>(ptr);
ThreadData* td = static_cast<ThreadData*>(info->UserData);
//igtl::SimpleMutexLock *glock = td->glocks[0];
int i = 0;
while(++i <=10)
{
std::cerr<<i<<" in the first child thread"<<std::endl;
igtl::Sleep(200);
td->iFinalCount++;
};
td->condition = true;
if (td->isBroadCast)
{
td->conditionVar->Broadcast();
}
else
{
td->conditionVar->Signal();
}
return NULL;
}
int main(int argc, char **argv)
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
|