diff --git a/src/cascadia/WinRTUtils/WinRTUtils.vcxproj b/src/cascadia/WinRTUtils/WinRTUtils.vcxproj index 2bd60e143..ee25e2a3f 100644 --- a/src/cascadia/WinRTUtils/WinRTUtils.vcxproj +++ b/src/cascadia/WinRTUtils/WinRTUtils.vcxproj @@ -15,8 +15,9 @@ - + + @@ -28,6 +29,7 @@ + diff --git a/src/cascadia/WinRTUtils/WinRTUtils.vcxproj.filters b/src/cascadia/WinRTUtils/WinRTUtils.vcxproj.filters index edaac5a3d..6b69e2359 100644 --- a/src/cascadia/WinRTUtils/WinRTUtils.vcxproj.filters +++ b/src/cascadia/WinRTUtils/WinRTUtils.vcxproj.filters @@ -7,11 +7,13 @@ + - + + diff --git a/src/cascadia/WinRTUtils/inc/thread_assert.h b/src/cascadia/WinRTUtils/inc/thread_assert.h new file mode 100644 index 000000000..ebec4facc --- /dev/null +++ b/src/cascadia/WinRTUtils/inc/thread_assert.h @@ -0,0 +1,14 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +#pragma once + +#ifdef NDEBUG +constexpr void mark_as_foreground_thread() {} +constexpr void assert_foreground_thread() {} +constexpr void assert_background_thread() {} +#else +void mark_as_foreground_thread(); +void assert_foreground_thread(); +void assert_background_thread(); +#endif diff --git a/src/cascadia/WinRTUtils/thread_assert.cpp b/src/cascadia/WinRTUtils/thread_assert.cpp new file mode 100644 index 000000000..d1b2c9a99 --- /dev/null +++ b/src/cascadia/WinRTUtils/thread_assert.cpp @@ -0,0 +1,27 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +#include "pch.h" +#include "thread_assert.h" + +#ifndef NDEBUG + +static const DWORD foreground_thread_id = []() -> DWORD { + const auto id = GetCurrentThreadId(); + OutputDebugStringW((std::wstring(L"#### ") + std::to_wstring(id) + L" ####").c_str()); + return id; +}(); + +void assert_foreground_thread() { + if (foreground_thread_id != GetCurrentThreadId()) { + //throw std::exception("expected to be called from a foreground thread"); + } +} + +void assert_background_thread() { + if (foreground_thread_id != GetCurrentThreadId()) { + throw std::exception("expected to be called from a background thread"); + } +} + +#endif // NDEBUG