Skip to content

Commit

Permalink
Merge pull request #585 from leapmotion/feature-ismainthread
Browse files Browse the repository at this point in the history
Implement BasicThread::IsMainThread
  • Loading branch information
gtremper committed Jun 19, 2015
2 parents 021f8af + e0188e5 commit fbbee74
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions autowiring/BasicThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,11 @@ class BasicThread:
/// \include snippets/BasicThread_GetRunningTime.txt
/// </remarks>
void GetThreadTimes(std::chrono::milliseconds& kernelTime, std::chrono::milliseconds& userTime);

/// <returns>
/// True if the calling thread is the main thread
/// </returns>
static bool IsMainThread(void);
};

/// <summary>
Expand Down
6 changes: 6 additions & 0 deletions src/autowiring/BasicThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include "fast_pointer_cast.h"
#include ATOMIC_HEADER

static auto mainTID = std::this_thread::get_id();

BasicThread::BasicThread(const char* pName):
ContextMember(pName),
m_state(std::make_shared<BasicThreadStateBlock>())
Expand Down Expand Up @@ -185,3 +187,7 @@ void BasicThread::ForceCoreThreadReidentify(void) {
void ForceCoreThreadReidentify(void) {
BasicThread::ForceCoreThreadReidentify();
}

bool BasicThread::IsMainThread(void) {
return mainTID == std::this_thread::get_id();
}
12 changes: 12 additions & 0 deletions src/autowiring/test/BasicThreadTest.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (C) 2012-2015 Leap Motion, Inc. All rights reserved.
#include "stdafx.h"
#include <autowiring/BasicThread.h>
#include FUTURE_HEADER

class BasicThreadTest:
public testing::Test
Expand Down Expand Up @@ -64,3 +65,14 @@ TEST_F(BasicThreadTest, ValidateThreadTimes) {
ASSERT_LE(benchmark, spinsThenQuits->m_userTime * 10) <<
"Reported execution time could not possibly be correct, spin operation took less time to execute than should have been possible with the CPU";
}

TEST_F(BasicThreadTest, IsMainThread) {
ASSERT_TRUE(BasicThread::IsMainThread()) << "Main thread not correctly identified as the main thread";
std::future<bool> secondaryIsMain = std::async(
std::launch::async,
[] {
return BasicThread::IsMainThread();
}
);
ASSERT_FALSE(secondaryIsMain.get()) << "Secondary thread incorrectly identified as the main thread";
}

0 comments on commit fbbee74

Please sign in to comment.