[XAudio2] share Xbox audio device enumeration with desktop and Windows 8.1 compatibility
[xbmc.git] / xbmc / platform / win10 / AsyncHelpers.h
blob7ad9320dd532bd8832855add5412e154484ecf54
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #pragma once
11 #include <ppl.h>
12 #include <ppltasks.h>
13 #include <sdkddkver.h>
15 #ifndef TARGET_WINDOWS_STORE
16 #include <winrt/windows.foundation.h>
17 #endif
19 namespace winrt
21 using namespace Windows::Foundation;
24 inline bool is_sta()
26 #ifdef NTDDI_WIN10_CO // Windows SDK 10.0.22000.0 or newer
27 return winrt::impl::is_sta_thread();
28 #else
29 return winrt::impl::is_sta();
30 #endif
33 inline void Wait(const winrt::IAsyncAction& asyncOp)
35 if (asyncOp.Status() == winrt::AsyncStatus::Completed)
36 return;
38 if (!is_sta())
39 return asyncOp.get();
41 auto __sync = std::make_shared<Concurrency::event>();
42 asyncOp.Completed([&](auto&&, auto&&) {
43 __sync->set();
44 });
45 __sync->wait();
48 template <typename TResult, typename TProgress> inline
49 TResult Wait(const winrt::IAsyncOperationWithProgress<TResult, TProgress>& asyncOp)
51 if (asyncOp.Status() == winrt::AsyncStatus::Completed)
52 return asyncOp.GetResults();
54 if (!is_sta())
55 return asyncOp.get();
57 auto __sync = std::make_shared<Concurrency::event>();
58 asyncOp.Completed([&](auto&&, auto&&) {
59 __sync->set();
60 });
61 __sync->wait();
63 return asyncOp.GetResults();
66 template <typename TResult> inline
67 TResult Wait(const winrt::IAsyncOperation<TResult>& asyncOp)
69 if (asyncOp.Status() == winrt::AsyncStatus::Completed)
70 return asyncOp.GetResults();
72 if (!is_sta())
73 return asyncOp.get();
75 auto __sync = std::make_shared<Concurrency::event>();
76 asyncOp.Completed([&](auto&&, auto&&)
78 __sync->set();
79 });
80 __sync->wait();
82 return asyncOp.GetResults();
85 template <typename TResult> inline
86 TResult Wait(const Concurrency::task<TResult>& asyncOp)
88 if (asyncOp.is_done())
89 return asyncOp.get();
91 if (!is_sta()) // blocking suspend is allowed
92 return asyncOp.get();
94 auto _sync = std::make_shared<Concurrency::event>();
95 asyncOp.then([&](TResult result)
97 _sync->set();
98 });
99 _sync->wait();
101 return asyncOp.get();