Skip to content
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
2 changes: 1 addition & 1 deletion strings/base_coroutine_foundation.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ namespace winrt::impl

void await_suspend(std::experimental::coroutine_handle<> handle) const
{
async.Completed([handle, context = impl::sta_apartment_context()](auto&& ...)
async.Completed([handle, context = impl::apartment_context()](auto&& ...)
{
impl::resume_apartment(context, handle);
});
Expand Down
23 changes: 15 additions & 8 deletions strings/base_coroutine_threadpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@ namespace winrt::impl
return (0 == WINRT_CoGetApartmentType(&aptType, &aptTypeQualifier)) && ((aptType == 0 /*APTTYPE_STA*/) || (aptType == 3 /*APTTYPE_MAINSTA*/));
}

inline auto sta_apartment_context()
inline bool requires_apartment_context() noexcept
{
return is_sta() ? capture<IContextCallback>(WINRT_CoGetObjectContext) : nullptr;
int32_t aptType;
int32_t aptTypeQualifier;
return (0 == WINRT_CoGetApartmentType(&aptType, &aptTypeQualifier)) && ((aptType == 0 /*APTTYPE_STA*/) || (aptType == 2 /*APTTYPE_NA*/) || (aptType == 3 /*APTTYPE_MAINSTA*/));
}

inline auto apartment_context()
{
return requires_apartment_context() ? capture<IContextCallback>(WINRT_CoGetObjectContext) : nullptr;
}

inline int32_t __stdcall resume_apartment_callback(com_callback_args* args) noexcept
Expand All @@ -32,18 +39,18 @@ namespace winrt::impl
return 0;
};

inline auto resume_apartment(com_ptr<IContextCallback> const& sta_context, std::experimental::coroutine_handle<> handle)
inline auto resume_apartment(com_ptr<IContextCallback> const& context, std::experimental::coroutine_handle<> handle)
{
if (sta_context)
if (context)
{
com_callback_args args{};
args.data = handle.address();

check_hresult(sta_context->ContextCallback(resume_apartment_callback, &args, guid_of<ICallbackWithNoReentrancyToApplicationSTA>(), 5, nullptr));
check_hresult(context->ContextCallback(resume_apartment_callback, &args, guid_of<ICallbackWithNoReentrancyToApplicationSTA>(), 5, nullptr));
}
else
{
if (is_sta())
if (requires_apartment_context())
{
resume_background(handle);
}
Expand Down Expand Up @@ -284,10 +291,10 @@ WINRT_EXPORT namespace winrt

void await_suspend(std::experimental::coroutine_handle<> handle) const
{
impl::resume_apartment(sta_context, handle);
impl::resume_apartment(context, handle);
}

com_ptr<impl::IContextCallback> sta_context = impl::sta_apartment_context();
com_ptr<impl::IContextCallback> context = impl::apartment_context();
};

[[nodiscard]] inline auto resume_after(Windows::Foundation::TimeSpan duration) noexcept
Expand Down