Skip to content
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

Fix Win32 multithread dispatching bugs. #265

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion src/dispatch_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@
*/

#include "config.h"

#ifdef __GNUC__
#define EPOXY_THREADLOCAL __thread
#elif defined (_MSC_VER)
#define EPOXY_THREADLOCAL __declspec(thread)
#endif
#ifdef _WIN32
#define PLATFORM_HAS_EGL ENABLE_EGL
#define PLATFORM_HAS_GLX ENABLE_GLX
Expand Down
68 changes: 2 additions & 66 deletions src/dispatch_wgl.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@

#include "dispatch_common.h"

static bool first_context_current = false;
static bool already_switched_to_dispatch_table = false;

/**
* If we can determine the WGL extension support from the current
Expand Down Expand Up @@ -75,70 +73,8 @@ epoxy_has_wgl_extension(HDC hdc, const char *ext)
void
epoxy_handle_external_wglMakeCurrent(void)
{
if (!first_context_current) {
first_context_current = true;
} else {
if (!already_switched_to_dispatch_table) {
already_switched_to_dispatch_table = true;
gl_switch_to_dispatch_table();
wgl_switch_to_dispatch_table();
}

gl_init_dispatch_table();
wgl_init_dispatch_table();
}
}

/**
* This global symbol is apparently looked up by Windows when loading
* a DLL, but it doesn't declare the prototype.
*/
BOOL WINAPI
DllMain(HINSTANCE dll, DWORD reason, LPVOID reserved);

BOOL WINAPI
DllMain(HINSTANCE dll, DWORD reason, LPVOID reserved)
{
void *data;

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 */

case DLL_THREAD_ATTACH:
data = LocalAlloc(LPTR, gl_tls_size);
TlsSetValue(gl_tls_index, data);

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

break;

case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
data = TlsGetValue(gl_tls_index);
LocalFree(data);

data = TlsGetValue(wgl_tls_index);
LocalFree(data);

if (reason == DLL_PROCESS_DETACH) {
TlsFree(gl_tls_index);
TlsFree(wgl_tls_index);
}
break;
}

return TRUE;
gl_init_dispatch_table();
wgl_init_dispatch_table();
}

WRAPPER_VISIBILITY (BOOL)
Expand Down
29 changes: 12 additions & 17 deletions src/gen_dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ def write_thunks(self, func):
func.args_list))

def write_function_pointer(self, func):
self.outln('{0} epoxy_{1} = epoxy_{1}_global_rewrite_ptr;'.format(func.ptr_type, func.wrapped_name))
self.outln('{0} epoxy_{1} = EPOXY_DISPATCH_PTR(epoxy_{1});'.format(func.ptr_type, func.wrapped_name))
self.outln('')

def write_provider_enums(self):
Expand Down Expand Up @@ -816,25 +816,30 @@ def write_source(self, f):
self.write_thunks(func)
self.outln('')

self.outln('#define EPOXY_DISPATCH_PTR(name) name##_global_rewrite_ptr')

self.outln('#if USING_DISPATCH_TABLE')

self.outln('#undef EPOXY_DISPATCH_PTR')
self.outln('#define EPOXY_DISPATCH_PTR(name) name##_dispatch_table_thunk')

self.outln('static struct dispatch_table resolver_table = {')
for func in self.sorted_functions:
self.outln(' epoxy_{0}_dispatch_table_rewrite_ptr, /* {0} */'.format(func.wrapped_name))
self.outln('};')
self.outln('')

self.outln('uint32_t {0}_tls_index;'.format(self.target))
self.outln('uint32_t {0}_tls_size = sizeof(struct dispatch_table);'.format(self.target))
self.outln('')

self.outln('EPOXY_THREADLOCAL struct dispatch_table {0}_tls_data = {{'.format(self.target))
for func in self.sorted_functions:
self.outln(' epoxy_{0}_dispatch_table_rewrite_ptr, /* {0} */'.format(func.wrapped_name))
self.outln('};')
self.outln('static inline struct dispatch_table *')
self.outln('get_dispatch_table(void)')
self.outln('{')
self.outln(' return TlsGetValue({0}_tls_index);'.format(self.target))
self.outln(' return &{0}_tls_data;'.format(self.target))
self.outln('}')
self.outln('')

self.outln('void')
self.outln('{0}_init_dispatch_table(void)'.format(self.target))
self.outln('{')
Expand All @@ -843,16 +848,6 @@ def write_source(self, f):
self.outln('}')
self.outln('')

self.outln('void')
self.outln('{0}_switch_to_dispatch_table(void)'.format(self.target))
self.outln('{')

for func in self.sorted_functions:
self.outln(' epoxy_{0} = epoxy_{0}_dispatch_table_thunk;'.format(func.wrapped_name))

self.outln('}')
self.outln('')

self.outln('#endif /* !USING_DISPATCH_TABLE */')

for func in self.sorted_functions:
Expand Down