Skip to content

wgl: Fix TLS initialization when statically linked #304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions src/dispatch_wgl.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include "dispatch_common.h"

static INIT_ONCE initialized = INIT_ONCE_STATIC_INIT;
static bool first_context_current = false;
static bool already_switched_to_dispatch_table = false;

Expand Down Expand Up @@ -62,6 +63,29 @@ epoxy_has_wgl_extension(HDC hdc, const char *ext)
return epoxy_extension_in_string(getext(hdc), ext);
}

BOOL
epoxy_initialize_tls(PINIT_ONCE once, PVOID param, PVOID *context)
{
void *data;

gl_tls_index = TlsAlloc();
if (gl_tls_index == TLS_OUT_OF_INDEXES)
return FALSE;
wgl_tls_index = TlsAlloc();
if (wgl_tls_index == TLS_OUT_OF_INDEXES)
return FALSE;

first_context_current = false;

data = LocalAlloc(LPTR, gl_tls_size);
TlsSetValue(gl_tls_index, data);

data = LocalAlloc(LPTR, wgl_tls_size);
TlsSetValue(wgl_tls_index, data);

return TRUE;
}

/**
* Does the work necessary to update the win32 per-thread dispatch
* tables when wglMakeCurrent() is called.
Expand All @@ -78,6 +102,8 @@ epoxy_handle_external_wglMakeCurrent(void)
if (!first_context_current) {
first_context_current = true;
} else {
InitOnceExecuteOnce(&initialized, epoxy_initialize_tls, NULL, NULL);

if (!already_switched_to_dispatch_table) {
already_switched_to_dispatch_table = true;
gl_switch_to_dispatch_table();
Expand All @@ -103,16 +129,8 @@ DllMain(HINSTANCE dll, DWORD reason, LPVOID reserved)

switch (reason) {
case DLL_PROCESS_ATTACH:
gl_tls_index = TlsAlloc();
if (gl_tls_index == TLS_OUT_OF_INDEXES)
return FALSE;
wgl_tls_index = TlsAlloc();
if (wgl_tls_index == TLS_OUT_OF_INDEXES)
return FALSE;

first_context_current = false;

/* FALLTHROUGH */
InitOnceExecuteOnce(&initialized, epoxy_initialize_tls, NULL, NULL);
break;

case DLL_THREAD_ATTACH:
data = LocalAlloc(LPTR, gl_tls_size);
Expand Down