Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CoreContext initiation #404

Merged
merged 2 commits into from Feb 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion autowiring/CoreContext.h
Expand Up @@ -455,7 +455,7 @@ class CoreContext:
/// <summary>
/// Invoked by a parent context when the parent has transitioned to the Running state
/// </summary>
void TryTransitionChildrenToRunState(void);
void TryTransitionChildrenState(void);

/// <summary>
/// Registers a factory _function_, a lambda which is capable of constructing decltype(fn())
Expand Down
28 changes: 20 additions & 8 deletions src/autowiring/CoreContext.cpp
Expand Up @@ -441,7 +441,8 @@ void CoreContext::Initiate(void) {
(*q)->Start(outstanding);
}

TryTransitionChildrenToRunState();
// Update state of children now that we are initated
TryTransitionChildrenState();
}

void CoreContext::InitiateCoreThreads(void) {
Expand Down Expand Up @@ -973,7 +974,7 @@ void CoreContext::AddPacketSubscriber(const AutoFilterDescriptor& rhs) {
Inject<AutoPacketFactory>()->AddSubscriber(rhs);
}

void CoreContext::TryTransitionChildrenToRunState(void) {
void CoreContext::TryTransitionChildrenState(void) {
std::unique_lock<std::mutex> lk(m_stateBlock->m_lock);

// We have to hold this to prevent dtors from running in a synchronized context
Expand All @@ -997,28 +998,39 @@ void CoreContext::TryTransitionChildrenToRunState(void) {
{
auto q = child->m_threads.begin();
child->m_state = State::Running;
childLk.unlock();

// Child had it's state changed
child->m_stateBlock->m_stateChanged.notify_all();

childLk.unlock();
auto outstanding = child->IncrementOutstandingThreadCount();

while (q != child->m_threads.end()) {
(*q)->Start(outstanding);
q++;
}
}

// Recursivly try to transition children
child->TryTransitionChildrenState();

break;
case State::CanRun:
case State::NotStarted:
// Children can run now that their parent has been initiated
child->m_state = State::CanRun;

// Child had it's state changed
child->m_stateBlock->m_stateChanged.notify_all();
break;
case State::CanRun:
case State::Running:
case State::Shutdown:
case State::Abandoned:
// No action need be taken for these states
return;
break;
}
}

// Recursivly try to transition children
child->TryTransitionChildrenToRunState();

// Permit a prior context to expire if needed
prior.reset();

Expand Down
101 changes: 89 additions & 12 deletions src/autowiring/test/CoreContextTest.cpp
Expand Up @@ -237,39 +237,116 @@ TEST_F(CoreContextTest, InitiateCausesDelayedHold) {
TEST_F(CoreContextTest, InitiateOrder) {
AutoCurrentContext testCtxt;
testCtxt->Initiate();
// Initiate inner to outer
{
auto outerCtxt = testCtxt->Create<void>();
auto middleCtxt = outerCtxt->Create<void>();
auto innerCtxt = middleCtxt->Create<void>();

middleCtxt->Initiate();
innerCtxt->Initiate();
middleCtxt->Initiate();
outerCtxt->Initiate();

EXPECT_TRUE(outerCtxt->IsRunning());
EXPECT_TRUE(middleCtxt->IsRunning());
EXPECT_TRUE(innerCtxt->IsRunning());
EXPECT_TRUE(outerCtxt->IsRunning()) << "Context not running after begin initiated";
EXPECT_TRUE(middleCtxt->IsRunning()) << "Context not running after begin initiated";
EXPECT_TRUE(innerCtxt->IsRunning()) << "Context not running after begin initiated";

outerCtxt->SignalShutdown(true);
middleCtxt->SignalShutdown(true);
innerCtxt->SignalShutdown(true);
}

// Initiate outer to inner
{
auto outerCtxt = testCtxt->Create<void>();
auto middleCtxt = outerCtxt->Create<void>();
auto innerCtxt = middleCtxt->Create<void>();

outerCtxt->Initiate();
middleCtxt->Initiate();
innerCtxt->Initiate();

EXPECT_TRUE(outerCtxt->IsRunning()) << "Context not running after begin initiated";
EXPECT_TRUE(middleCtxt->IsRunning()) << "Context not running after begin initiated";
EXPECT_TRUE(innerCtxt->IsRunning()) << "Context not running after begin initiated";

outerCtxt->SignalShutdown(true);
}

// Initiate middle, inner, then outer
{
auto outerCtxt = testCtxt->Create<void>();
auto middleCtxt = outerCtxt->Create<void>();
auto innerCtxt = middleCtxt->Create<void>();

middleCtxt->Initiate();
innerCtxt->Initiate();
outerCtxt->Initiate();

EXPECT_TRUE(outerCtxt->IsRunning());
EXPECT_TRUE(middleCtxt->IsRunning());
EXPECT_TRUE(innerCtxt->IsRunning());
EXPECT_TRUE(outerCtxt->IsRunning()) << "Context not running after begin initiated";
EXPECT_TRUE(middleCtxt->IsRunning()) << "Context not running after begin initiated";
EXPECT_TRUE(innerCtxt->IsRunning()) << "Context not running after begin initiated";

innerCtxt->SignalShutdown(true);
middleCtxt->SignalShutdown(true);
outerCtxt->SignalShutdown(true);
}
}
}

TEST_F(CoreContextTest, InitiateMultipleChildren) {
AutoCurrentContext testCtxt;
testCtxt->Initiate();
// Initiate all children
{
auto outerCtxt = testCtxt->Create<void>();
auto child1 = outerCtxt->Create<void>();
auto child2 = outerCtxt->Create<void>();
auto child3 = outerCtxt->Create<void>();

child1->Initiate();
child2->Initiate();
child3->Initiate();

outerCtxt->Initiate();

EXPECT_TRUE(child1->IsRunning());
EXPECT_TRUE(child2->IsRunning());
EXPECT_TRUE(child3->IsRunning());

outerCtxt->SignalShutdown(true);
}

// Don't initiate middle child
{
auto outerCtxt = testCtxt->Create<void>();
auto child1 = outerCtxt->Create<void>();
auto child2 = outerCtxt->Create<void>();
auto child3 = outerCtxt->Create<void>();

child1->Initiate();
child3->Initiate();

outerCtxt->Initiate();

EXPECT_TRUE(child1->IsRunning());
EXPECT_FALSE(child2->IsInitiated());
EXPECT_TRUE(child3->IsRunning());

outerCtxt->SignalShutdown(true);
}

// Don't initiate middle child and initiate parent first
{
auto outerCtxt = testCtxt->Create<void>();
auto child1 = outerCtxt->Create<void>();
auto child2 = outerCtxt->Create<void>();
auto child3 = outerCtxt->Create<void>();

outerCtxt->Initiate();

child1->Initiate();
child3->Initiate();

EXPECT_TRUE(child1->IsRunning());
EXPECT_FALSE(child2->IsInitiated());
EXPECT_TRUE(child3->IsRunning());

outerCtxt->SignalShutdown(true);
}
}