From 6f570624df0fd0bf63f7d1481533d130e2af3eca Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 3 Sep 2018 14:19:14 +0300 Subject: [PATCH] Fix thread-safety for C++/CLI. GitOrigin-RevId: 934f0b05cc46cc0731f5f9f24c6db019a1e6a605 --- td/tl/tl_dotnet_object.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/td/tl/tl_dotnet_object.h b/td/tl/tl_dotnet_object.h index b60f28638..e763eb610 100644 --- a/td/tl/tl_dotnet_object.h +++ b/td/tl/tl_dotnet_object.h @@ -100,13 +100,36 @@ auto CLRCALL FromUnmanaged(td::td_api::object_ptr &from) -> decltype(FromUnma return FromUnmanaged(*from.get()); } +#if TD_CLI +template +ref class CallFromUnmanagedRes { +public: + [System::ThreadStaticAttribute] + static property ResT res; +}; + +template +struct CallFromUnmanaged { + template + void operator()(T &val) const { + CallFromUnmanagedRes::res = FromUnmanaged(val); + } +}; +#endif + template inline ResT DoFromUnmanaged(T &from) { +#if TD_WINRT ResT res; downcast_call(from, [&](auto &from_downcasted) { res = FromUnmanaged(from_downcasted); }); return res; +#elif TD_CLI + CallFromUnmanaged res; + downcast_call(from, res); + return CallFromUnmanagedRes::res; +#endif } inline BaseObject^ FromUnmanaged(td::td_api::Function &from) {