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 @@
# error Unsupported platform.
#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();
bool DestroyTLSIndex(TLSIndex index);
......
......@@ -11,15 +11,42 @@
#include "common/tls.h"
static TLSIndex currentTLS = TLS_OUT_OF_INDEXES;
static TLSIndex currentTLS = TLS_INVALID_INDEX;
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()
{
ASSERT(currentTLS != TLS_OUT_OF_INDEXES);
if (currentTLS == TLS_OUT_OF_INDEXES)
ASSERT(currentTLS != TLS_INVALID_INDEX);
if (currentTLS == TLS_INVALID_INDEX)
{
return NULL;
}
......@@ -37,6 +64,7 @@ Current *AllocateCurrent()
return current;
}
// Call this upon thread shutdown.
void DeallocateCurrent()
{
Current *current = reinterpret_cast<Current*>(GetTLSValue(currentTLS));
......@@ -46,14 +74,14 @@ void DeallocateCurrent()
}
#ifdef ANGLE_PLATFORM_WINDOWS
extern "C" BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved)
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
{
currentTLS = CreateTLSIndex();
if (currentTLS == TLS_OUT_OF_INDEXES)
if (!gl::CreateThreadLocalIndex())
{
return FALSE;
}
......@@ -72,7 +100,7 @@ extern "C" BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved
case DLL_PROCESS_DETACH:
{
gl::DeallocateCurrent();
DestroyTLSIndex(currentTLS);
gl::DestroyThreadLocalIndex();
}
break;
default:
......@@ -81,6 +109,7 @@ extern "C" BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved
return TRUE;
}
#endif
namespace gl
{
......@@ -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