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

Implement BasicThread::IsMainThread #585

Merged
merged 1 commit into from
Jun 19, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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";
}