Skip to content

Commit

Permalink
Implement BasicThread::IsMainThread
Browse files Browse the repository at this point in the history
Add support to discriminate between the main thread and other threads on the system.  Some systems have special handling for the main thread--IE, certain APIs can only be called safely from it--thus it's important that we have a way to inform such systems as to whether a particular thread is the main thread.
  • Loading branch information
codemercenary committed Jun 18, 2015
1 parent 92472f8 commit 9260a34
Show file tree
Hide file tree
Showing 5 changed files with 37 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
8 changes: 8 additions & 0 deletions src/autowiring/BasicThreadUnix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (C) 2012-2015 Leap Motion, Inc. All rights reserved.
#include "stdafx.h"
#include "BasicThread.h"
#include <unistd.h>

bool BasicThread::IsMainThread(void) {
return getpid() == gettid();
}
10 changes: 10 additions & 0 deletions src/autowiring/BasicThreadWin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (C) 2012-2015 Leap Motion, Inc. All rights reserved.
#include "stdafx.h"
#include "BasicThread.h"
#include <Windows.h>

static int s_mainTID = GetCurrentThreadId();

bool BasicThread::IsMainThread(void) {
return GetCurrentThreadId() == s_mainTID;
}
2 changes: 2 additions & 0 deletions src/autowiring/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ add_conditional_sources(

add_windows_sources(Autowiring_SRCS
auto_future_win.h
BasicThreadWin.cpp
CoreThreadWin.cpp
SystemThreadPoolWin.cpp
SystemThreadPoolWin.h
Expand All @@ -203,6 +204,7 @@ add_mac_sources(Autowiring_SRCS

add_unix_sources(Autowiring_SRCS
InterlockedExchangeUnix.cpp
BasicThreadUnix.cpp
thread_specific_ptr_unix.h
)

Expand Down
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 9260a34

Please sign in to comment.