From 9260a3443cba764174e152dd3de2dbbf31e42864 Mon Sep 17 00:00:00 2001 From: Jason Lokerson Date: Wed, 17 Jun 2015 23:23:57 -0700 Subject: [PATCH] Implement BasicThread::IsMainThread 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. --- autowiring/BasicThread.h | 5 +++++ src/autowiring/BasicThreadUnix.cpp | 8 ++++++++ src/autowiring/BasicThreadWin.cpp | 10 ++++++++++ src/autowiring/CMakeLists.txt | 2 ++ src/autowiring/test/BasicThreadTest.cpp | 12 ++++++++++++ 5 files changed, 37 insertions(+) create mode 100644 src/autowiring/BasicThreadUnix.cpp create mode 100644 src/autowiring/BasicThreadWin.cpp diff --git a/autowiring/BasicThread.h b/autowiring/BasicThread.h index f67d1e6d3..7013d18d8 100644 --- a/autowiring/BasicThread.h +++ b/autowiring/BasicThread.h @@ -308,6 +308,11 @@ class BasicThread: /// \include snippets/BasicThread_GetRunningTime.txt /// void GetThreadTimes(std::chrono::milliseconds& kernelTime, std::chrono::milliseconds& userTime); + + /// + /// True if the calling thread is the main thread + /// + static bool IsMainThread(void); }; /// diff --git a/src/autowiring/BasicThreadUnix.cpp b/src/autowiring/BasicThreadUnix.cpp new file mode 100644 index 000000000..e58996546 --- /dev/null +++ b/src/autowiring/BasicThreadUnix.cpp @@ -0,0 +1,8 @@ +// Copyright (C) 2012-2015 Leap Motion, Inc. All rights reserved. +#include "stdafx.h" +#include "BasicThread.h" +#include + +bool BasicThread::IsMainThread(void) { + return getpid() == gettid(); +} diff --git a/src/autowiring/BasicThreadWin.cpp b/src/autowiring/BasicThreadWin.cpp new file mode 100644 index 000000000..af8f054d5 --- /dev/null +++ b/src/autowiring/BasicThreadWin.cpp @@ -0,0 +1,10 @@ +// Copyright (C) 2012-2015 Leap Motion, Inc. All rights reserved. +#include "stdafx.h" +#include "BasicThread.h" +#include + +static int s_mainTID = GetCurrentThreadId(); + +bool BasicThread::IsMainThread(void) { + return GetCurrentThreadId() == s_mainTID; +} \ No newline at end of file diff --git a/src/autowiring/CMakeLists.txt b/src/autowiring/CMakeLists.txt index 45b0247cb..497a6d849 100644 --- a/src/autowiring/CMakeLists.txt +++ b/src/autowiring/CMakeLists.txt @@ -189,6 +189,7 @@ add_conditional_sources( add_windows_sources(Autowiring_SRCS auto_future_win.h + BasicThreadWin.cpp CoreThreadWin.cpp SystemThreadPoolWin.cpp SystemThreadPoolWin.h @@ -203,6 +204,7 @@ add_mac_sources(Autowiring_SRCS add_unix_sources(Autowiring_SRCS InterlockedExchangeUnix.cpp + BasicThreadUnix.cpp thread_specific_ptr_unix.h ) diff --git a/src/autowiring/test/BasicThreadTest.cpp b/src/autowiring/test/BasicThreadTest.cpp index ce22fd25d..37c4dc580 100644 --- a/src/autowiring/test/BasicThreadTest.cpp +++ b/src/autowiring/test/BasicThreadTest.cpp @@ -1,6 +1,7 @@ // Copyright (C) 2012-2015 Leap Motion, Inc. All rights reserved. #include "stdafx.h" #include +#include FUTURE_HEADER class BasicThreadTest: public testing::Test @@ -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 secondaryIsMain = std::async( + std::launch::async, + [] { + return BasicThread::IsMainThread(); + } + ); + ASSERT_FALSE(secondaryIsMain.get()) << "Secondary thread incorrectly identified as the main thread"; +}