Skip to content

Commit

Permalink
Merge pull request #80 from leapmotion/bug-reentrantinitiate
Browse files Browse the repository at this point in the history
Fix CoreContext::Initiate reentrant deadlock
  • Loading branch information
gtremper committed Aug 25, 2014
2 parents 050be6f + a557150 commit 98a81c2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/autowiring/CoreContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,11 @@ std::vector<std::shared_ptr<BasicThread>> CoreContext::CopyBasicThreadList(void)
}

void CoreContext::Initiate(void) {
// First-pass check, used to prevent recursive deadlocks traceable to here that might
// result from entities trying to initiate subcontexts from CoreRunnable::Start
if(m_initiated || m_isShutdown)
return;

{
std::lock_guard<std::mutex> lk(m_stateBlock->m_lock);
if(m_initiated)
Expand Down
1 change: 1 addition & 0 deletions src/autowiring/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ set(AutowiringTest_SRCS
BoltTest.cpp
CoreContextTest.cpp
CoreJobTest.cpp
CoreRunnableTest.cpp
ContextCleanupTest.cpp
ContextEnumeratorTest.cpp
ContextMapTest.cpp
Expand Down
30 changes: 30 additions & 0 deletions src/autowiring/test/CoreRunnableTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (C) 2012-2014 Leap Motion, Inc. All rights reserved.
#include "stdafx.h"
#include <autowiring/autowiring.h>
#include <autowiring/CoreRunnable.h>

class CoreRunnableTest:
public testing::Test
{};

class StartsSubcontextWhileStarting:
public CoreRunnable
{
public:
AutoCreateContext m_myContext;

bool Start(std::shared_ptr<Object> outstanding) override {
m_myContext->Initiate();
return true;
}

void Stop(bool graceful) override {}
bool IsRunning(void) const override { return false; }
bool ShouldStop(void) const override { return true; }
void Wait(void) override {}
};

TEST_F(CoreRunnableTest, CanStartSubcontextWhileInitiating) {
AutoRequired<StartsSubcontextWhileStarting>();
AutoCurrentContext()->Initiate();
}

0 comments on commit 98a81c2

Please sign in to comment.