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/222955
Reviewed-by: Geoff Lang <geofflang@chromium.org>
Tested-by: Kenneth Russell <kbr@chromium.org>
diff --git a/src/libGLESv2/main.cpp b/src/libGLESv2/main.cpp
index 4444d1a..2135d8b 100644
--- a/src/libGLESv2/main.cpp
+++ b/src/libGLESv2/main.cpp
@@ -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 @@
return current;
}
+// Call this upon thread shutdown.
void DeallocateCurrent()
{
Current *current = reinterpret_cast<Current*>(GetTLSValue(currentTLS));
@@ -46,14 +74,14 @@
}
+#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 @@
case DLL_PROCESS_DETACH:
{
gl::DeallocateCurrent();
- DestroyTLSIndex(currentTLS);
+ gl::DestroyThreadLocalIndex();
}
break;
default:
@@ -81,6 +109,7 @@
return TRUE;
}
+#endif
namespace gl
{
@@ -168,4 +197,3 @@
}
}
-