diff --git a/Platform/WinRT/Readium/Readium/ThreadEmulation.h b/Platform/WinRT/Readium/Readium/ThreadEmulation.h index 37deabca9..ded321357 100644 --- a/Platform/WinRT/Readium/Readium/ThreadEmulation.h +++ b/Platform/WinRT/Readium/Readium/ThreadEmulation.h @@ -34,6 +34,10 @@ namespace ThreadEmulation { + #ifndef TLS_OUT_OF_INDEXES + # define TLS_OUT_OF_INDEXES ((DWORD)0xffffffff) + #endif + #ifndef CREATE_SUSPENDED #define CREATE_SUSPENDED 0x00000004 #endif diff --git a/Platform/WinRT/Readium/Readium/WinInitialization.cpp b/Platform/WinRT/Readium/Readium/WinInitialization.cpp index 0fea1aa44..c60872d4d 100644 --- a/Platform/WinRT/Readium/Readium/WinInitialization.cpp +++ b/Platform/WinRT/Readium/Readium/WinInitialization.cpp @@ -1,9 +1,13 @@ #include "WinInitialization.h" +#include "run_loop.h" BEGIN_READIUM_API void Initialization::InitializeSdk() { +#if EPUB_PLATFORM(WINRT) + ePub3::RunLoop::InitRunLoopTLSKey(); +#endif ePub3::InitializeSdk(); } diff --git a/Platform/WinRT/Readium/Readium/WinInitialization.h b/Platform/WinRT/Readium/Readium/WinInitialization.h index 9981232f2..56ea90c4c 100644 --- a/Platform/WinRT/Readium/Readium/WinInitialization.h +++ b/Platform/WinRT/Readium/Readium/WinInitialization.h @@ -10,14 +10,8 @@ public ref class Initialization sealed { public: - - static void InitializeSdk(); - - static void PopulateFilterManager(); - -private: - Initialization() {} - + void InitializeSdk(); + void PopulateFilterManager(); }; diff --git a/ePub3/_config.h b/ePub3/_config.h index 10932d24b..d2a559cc5 100644 --- a/ePub3/_config.h +++ b/ePub3/_config.h @@ -152,6 +152,7 @@ typedef signed long ssize_t; # endif #endif +#if !EPUB_PLATFORM(WINRT) #if EPUB_COMPILER(MSVC) # pragma section(".CRT$XCU",read) # define INITIALIZER(f) \ @@ -163,7 +164,7 @@ typedef signed long ssize_t; static void f(void) __attribute__((constructor)); \ static void f(void) #endif - +#endif // MSVC doesn't have this macro #ifndef __PRETTY_FUNCTION__ # define __PRETTY_FUNCTION__ __FUNCTION__ diff --git a/ePub3/utilities/run_loop.h b/ePub3/utilities/run_loop.h index f59e3a595..5f7695200 100644 --- a/ePub3/utilities/run_loop.h +++ b/ePub3/utilities/run_loop.h @@ -507,17 +507,21 @@ class RunLoop : public PointerType EPUB3_EXPORT void WakeUp(); +# if EPUB_OS(WINDOWS) && EPUB_PLATFORM(WINRT) + static void InitRunLoopTLSKey(); + static void KillRunLoopTLSKey(); +# endif + protected: /// /// Internal Run function which takes an explicit timeout duration type. EPUB3_EXPORT ExitReason RunInternal(bool returnAfterSourceHandled, std::chrono::nanoseconds& timeout); - /// /// Obtains the run loop for the current thread. EPUB3_EXPORT RunLoop(); - + private: /// /// No copy constructor @@ -579,6 +583,11 @@ class RunLoop : public PointerType std::atomic _stop; std::atomic _resetHandles; Observer::Activity _observerMask; + + +# if EPUB_PLATFORM(WINRT) + static DWORD RunLoopTLSKey; +# endif #else shared_list _timers; shared_list _observers; diff --git a/ePub3/utilities/run_loop_common.ipp b/ePub3/utilities/run_loop_common.ipp index 7dd3c93cf..266cb43a4 100644 --- a/ePub3/utilities/run_loop_common.ipp +++ b/ePub3/utilities/run_loop_common.ipp @@ -41,12 +41,13 @@ using namespace ThreadEmulation; #endif EPUB3_BEGIN_NAMESPACE - -#if EPUB_OS(WINDOWS) -#ifndef TLS_OUT_OF_INDEXES +#if EPUB_OS(WINDOWS) +# if !EPUB_PLATFORM(WINRT) +# ifndef TLS_OUT_OF_INDEXES # define TLS_OUT_OF_INDEXES ((DWORD)0xffffffff) -#endif +# endif DWORD RunLoopTLSKey = TLS_OUT_OF_INDEXES; +# endif #else static pthread_key_t RunLoopTLSKey; #endif @@ -58,6 +59,8 @@ static void _DestroyTLSRunLoop(void* data) delete p; } #endif + +# if !EPUB_PLATFORM(WINRT) static void KillRunLoopTLSKey() { #if EPUB_OS(WINDOWS) @@ -67,6 +70,7 @@ static void KillRunLoopTLSKey() pthread_key_delete(RunLoopTLSKey); #endif } + INITIALIZER(InitRunLoopTLSKey) { #if EPUB_OS(WINDOWS) @@ -81,6 +85,7 @@ INITIALIZER(InitRunLoopTLSKey) pthread_key_create(&RunLoopTLSKey, _DestroyTLSRunLoop); #endif } +#endif RunLoopPtr RunLoop::CurrentRunLoop() { diff --git a/ePub3/utilities/run_loop_windows.cpp b/ePub3/utilities/run_loop_windows.cpp index 998682817..b85730650 100644 --- a/ePub3/utilities/run_loop_windows.cpp +++ b/ePub3/utilities/run_loop_windows.cpp @@ -794,4 +794,26 @@ void RunLoop::Timer::SetNextFireDateDuration(Clock::duration& when) #endif } +#if EPUB_PLATFORM(WINRT) + +DWORD RunLoop::RunLoopTLSKey = TLS_OUT_OF_INDEXES; + +void RunLoop::KillRunLoopTLSKey() { + if (RunLoop::RunLoopTLSKey != TLS_OUT_OF_INDEXES) { + TlsFree(RunLoop::RunLoopTLSKey); + } +} + +void RunLoop::InitRunLoopTLSKey() { + RunLoopTLSKey = TlsAlloc(); + if (RunLoopTLSKey == TLS_OUT_OF_INDEXES) + { + OutputDebugString(L"No TLS Indexes for RunLoop!\n"); + std::terminate(); + } + atexit(RunLoop::KillRunLoopTLSKey); +} + +#endif + EPUB3_END_NAMESPACE