Skip to content

Commit

Permalink
Move static initialization of InitRunLoopTLSKey as it fails on the st…
Browse files Browse the repository at this point in the history
…ore app dll.
  • Loading branch information
Diego Sandin committed Oct 16, 2013
1 parent 3c7e546 commit 0abad8f
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 15 deletions.
4 changes: 4 additions & 0 deletions Platform/WinRT/Readium/Readium/ThreadEmulation.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions Platform/WinRT/Readium/Readium/WinInitialization.cpp
Original file line number Diff line number Diff line change
@@ -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();
}

Expand Down
10 changes: 2 additions & 8 deletions Platform/WinRT/Readium/Readium/WinInitialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,8 @@ public ref class Initialization sealed
{

public:

static void InitializeSdk();

static void PopulateFilterManager();

private:
Initialization() {}

void InitializeSdk();
void PopulateFilterManager();
};


Expand Down
3 changes: 2 additions & 1 deletion ePub3/_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand All @@ -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__
Expand Down
13 changes: 11 additions & 2 deletions ePub3/utilities/run_loop.h
Original file line number Diff line number Diff line change
Expand Up @@ -507,17 +507,21 @@ class RunLoop : public PointerType<RunLoop>
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
Expand Down Expand Up @@ -579,6 +583,11 @@ class RunLoop : public PointerType<RunLoop>
std::atomic<bool> _stop;
std::atomic<bool> _resetHandles;
Observer::Activity _observerMask;


# if EPUB_PLATFORM(WINRT)
static DWORD RunLoopTLSKey;
# endif
#else
shared_list<Timer> _timers;
shared_list<Observer> _observers;
Expand Down
13 changes: 9 additions & 4 deletions ePub3/utilities/run_loop_common.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -58,6 +59,8 @@ static void _DestroyTLSRunLoop(void* data)
delete p;
}
#endif

# if !EPUB_PLATFORM(WINRT)
static void KillRunLoopTLSKey()
{
#if EPUB_OS(WINDOWS)
Expand All @@ -67,6 +70,7 @@ static void KillRunLoopTLSKey()
pthread_key_delete(RunLoopTLSKey);
#endif
}

INITIALIZER(InitRunLoopTLSKey)
{
#if EPUB_OS(WINDOWS)
Expand All @@ -81,6 +85,7 @@ INITIALIZER(InitRunLoopTLSKey)
pthread_key_create(&RunLoopTLSKey, _DestroyTLSRunLoop);
#endif
}
#endif

RunLoopPtr RunLoop::CurrentRunLoop()
{
Expand Down
22 changes: 22 additions & 0 deletions ePub3/utilities/run_loop_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0abad8f

Please sign in to comment.