Commit 9d6d498c by Kenneth Russell

Refactor TLS management.

Use cross-platform APIs and constants, and conditionally compile the Windows-specific DllMain. BUG=angleproject:783 Change-Id: I8fd2708ab0925cb3207010eb0e759cfc055183ab Reviewed-on: https://chromium-review.googlesource.com/222955Reviewed-by: 's avatarGeoff Lang <geofflang@chromium.org> Tested-by: 's avatarKenneth Russell <kbr@chromium.org>
parent caa549c0
...@@ -24,6 +24,9 @@ ...@@ -24,6 +24,9 @@
# error Unsupported platform. # error Unsupported platform.
#endif #endif
// TODO(kbr): for POSIX platforms this will have to be changed to take
// in a destructor function pointer, to allow the thread-local storage
// to be properly deallocated upon thread exit.
TLSIndex CreateTLSIndex(); TLSIndex CreateTLSIndex();
bool DestroyTLSIndex(TLSIndex index); bool DestroyTLSIndex(TLSIndex index);
......
...@@ -11,15 +11,42 @@ ...@@ -11,15 +11,42 @@
#include "common/tls.h" #include "common/tls.h"
static TLSIndex currentTLS = TLS_OUT_OF_INDEXES; static TLSIndex currentTLS = TLS_INVALID_INDEX;
namespace gl namespace gl
{ {
// TODO(kbr): figure out how these are going to be managed on
// non-Windows platforms. These routines would need to be exported
// from ANGLE and called cooperatively when users create and destroy
// threads -- or the initialization of the TLS index, and allocation
// of thread-local data, will have to be done lazily. Will have to use
// destructor function with pthread_create_key on POSIX platforms to
// clean up thread-local data.
// Call this exactly once at process startup.
bool CreateThreadLocalIndex()
{
currentTLS = CreateTLSIndex();
if (currentTLS == TLS_INVALID_INDEX)
{
return false;
}
return true;
}
// Call this exactly once at process shutdown.
void DestroyThreadLocalIndex()
{
DestroyTLSIndex(currentTLS);
currentTLS = TLS_INVALID_INDEX;
}
// Call this upon thread startup.
Current *AllocateCurrent() Current *AllocateCurrent()
{ {
ASSERT(currentTLS != TLS_OUT_OF_INDEXES); ASSERT(currentTLS != TLS_INVALID_INDEX);
if (currentTLS == TLS_OUT_OF_INDEXES) if (currentTLS == TLS_INVALID_INDEX)
{ {
return NULL; return NULL;
} }
...@@ -37,6 +64,7 @@ Current *AllocateCurrent() ...@@ -37,6 +64,7 @@ Current *AllocateCurrent()
return current; return current;
} }
// Call this upon thread shutdown.
void DeallocateCurrent() void DeallocateCurrent()
{ {
Current *current = reinterpret_cast<Current*>(GetTLSValue(currentTLS)); Current *current = reinterpret_cast<Current*>(GetTLSValue(currentTLS));
...@@ -46,14 +74,14 @@ void DeallocateCurrent() ...@@ -46,14 +74,14 @@ void DeallocateCurrent()
} }
#ifdef ANGLE_PLATFORM_WINDOWS
extern "C" BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved) extern "C" BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved)
{ {
switch (reason) switch (reason)
{ {
case DLL_PROCESS_ATTACH: case DLL_PROCESS_ATTACH:
{ {
currentTLS = CreateTLSIndex(); if (!gl::CreateThreadLocalIndex())
if (currentTLS == TLS_OUT_OF_INDEXES)
{ {
return FALSE; return FALSE;
} }
...@@ -72,7 +100,7 @@ extern "C" BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved ...@@ -72,7 +100,7 @@ extern "C" BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved
case DLL_PROCESS_DETACH: case DLL_PROCESS_DETACH:
{ {
gl::DeallocateCurrent(); gl::DeallocateCurrent();
DestroyTLSIndex(currentTLS); gl::DestroyThreadLocalIndex();
} }
break; break;
default: default:
...@@ -81,6 +109,7 @@ extern "C" BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved ...@@ -81,6 +109,7 @@ extern "C" BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved
return TRUE; return TRUE;
} }
#endif
namespace gl namespace gl
{ {
...@@ -168,4 +197,3 @@ void error(GLenum errorCode) ...@@ -168,4 +197,3 @@ void error(GLenum errorCode)
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment