From faa0c41cf2b908d571bcd472bfe877ffac56d581 Mon Sep 17 00:00:00 2001 From: gjz010 Date: Tue, 16 Nov 2021 12:22:13 +0800 Subject: [PATCH 1/5] Allowing static build on Win32. --- meson.build | 2 ++ src/dispatch_wgl.c | 3 +++ src/gen_dispatch.py | 11 +++++++++++ 3 files changed, 16 insertions(+) diff --git a/meson.build b/meson.build index e0228d1e..1410a84b 100644 --- a/meson.build +++ b/meson.build @@ -151,6 +151,8 @@ if libtype == 'shared' conf.set('EPOXY_PUBLIC', '__attribute__((visibility("default"))) extern') visibility_cflags += [ '-fvisibility=hidden' ] endif +else + conf.set('EPOXY_STATIC_BUILD', true) endif # The inline keyword is available only for C++ in MSVC. diff --git a/src/dispatch_wgl.c b/src/dispatch_wgl.c index 7baf1305..dcb83855 100644 --- a/src/dispatch_wgl.c +++ b/src/dispatch_wgl.c @@ -89,6 +89,8 @@ epoxy_handle_external_wglMakeCurrent(void) } } +#ifndef EPOXY_STATIC_BUILD + /** * This global symbol is apparently looked up by Windows when loading * a DLL, but it doesn't declare the prototype. @@ -140,6 +142,7 @@ DllMain(HINSTANCE dll, DWORD reason, LPVOID reserved) return TRUE; } +#endif WRAPPER_VISIBILITY (BOOL) WRAPPER(epoxy_wglMakeCurrent)(HDC hdc, HGLRC hglrc) diff --git a/src/gen_dispatch.py b/src/gen_dispatch.py index 3daad842..bc4f6dd7 100755 --- a/src/gen_dispatch.py +++ b/src/gen_dispatch.py @@ -784,8 +784,10 @@ def write_source(self, f): self.outln('') self.outln('#ifdef __GNUC__') self.outln('#define EPOXY_NOINLINE __attribute__((noinline))') + self.outln('#define EPOXY_THREADLOCAL __thread') self.outln('#elif defined (_MSC_VER)') self.outln('#define EPOXY_NOINLINE __declspec(noinline)') + self.outln('#define EPOXY_THREADLOCAL __declspec(thread)') self.outln('#endif') self.outln('struct dispatch_table {') @@ -824,6 +826,14 @@ def write_source(self, f): self.outln('};') self.outln('') + self.outln('#ifdef EPOXY_STATIC_BUILD') + self.outln('EPOXY_THREADLOCAL struct dispatch_table {0}_tls_data;'.format(self.target)) + self.outln('static inline struct dispatch_table *') + self.outln('get_dispatch_table(void)') + self.outln('{') + self.outln(' return &{0}_tls_data;'.format(self.target)) + self.outln('}') + self.outln('#else') 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('') @@ -833,6 +843,7 @@ def write_source(self, f): self.outln('{') self.outln(' return TlsGetValue({0}_tls_index);'.format(self.target)) self.outln('}') + self.outln('#endif') self.outln('') self.outln('void') From 6b50c4b256cfb8b5230e1503846a7e8df714d71c Mon Sep 17 00:00:00 2001 From: gjz010 Date: Tue, 16 Nov 2021 22:39:15 +0800 Subject: [PATCH 2/5] Fixing thread race and multithread issue using dispatch table. --- src/dispatch_common.h | 6 +++- src/dispatch_wgl.c | 73 +++---------------------------------------- src/gen_dispatch.py | 37 +++++++++++++++------- 3 files changed, 34 insertions(+), 82 deletions(-) diff --git a/src/dispatch_common.h b/src/dispatch_common.h index a1369431..3449b228 100644 --- a/src/dispatch_common.h +++ b/src/dispatch_common.h @@ -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 diff --git a/src/dispatch_wgl.c b/src/dispatch_wgl.c index dcb83855..08ecae74 100644 --- a/src/dispatch_wgl.c +++ b/src/dispatch_wgl.c @@ -27,8 +27,8 @@ #include "dispatch_common.h" -static bool first_context_current = false; -static bool already_switched_to_dispatch_table = false; +//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 @@ -75,75 +75,10 @@ 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(); - } + gl_init_dispatch_table(); + wgl_init_dispatch_table(); } -#ifndef EPOXY_STATIC_BUILD - -/** - * 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; -} -#endif - WRAPPER_VISIBILITY (BOOL) WRAPPER(epoxy_wglMakeCurrent)(HDC hdc, HGLRC hglrc) { diff --git a/src/gen_dispatch.py b/src/gen_dispatch.py index bc4f6dd7..41704b68 100755 --- a/src/gen_dispatch.py +++ b/src/gen_dispatch.py @@ -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): @@ -784,10 +784,8 @@ def write_source(self, f): self.outln('') self.outln('#ifdef __GNUC__') self.outln('#define EPOXY_NOINLINE __attribute__((noinline))') - self.outln('#define EPOXY_THREADLOCAL __thread') self.outln('#elif defined (_MSC_VER)') self.outln('#define EPOXY_NOINLINE __declspec(noinline)') - self.outln('#define EPOXY_THREADLOCAL __declspec(thread)') self.outln('#endif') self.outln('struct dispatch_table {') @@ -818,21 +816,21 @@ 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('#ifdef EPOXY_STATIC_BUILD') - self.outln('EPOXY_THREADLOCAL struct dispatch_table {0}_tls_data;'.format(self.target)) - self.outln('static inline struct dispatch_table *') - self.outln('get_dispatch_table(void)') - self.outln('{') - self.outln(' return &{0}_tls_data;'.format(self.target)) - self.outln('}') + self.outln('#else') self.outln('uint32_t {0}_tls_index;'.format(self.target)) self.outln('uint32_t {0}_tls_size = sizeof(struct dispatch_table);'.format(self.target)) @@ -843,9 +841,21 @@ def write_source(self, f): self.outln('{') self.outln(' return TlsGetValue({0}_tls_index);'.format(self.target)) self.outln('}') - self.outln('#endif') + self.outln('#endif /* EPOXY_STATIC_BUILD */') 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 &{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('{') @@ -854,14 +864,17 @@ 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 */') From 4f8fba0febe09504aeb62d01f5cdba755472e332 Mon Sep 17 00:00:00 2001 From: gjz010 Date: Tue, 16 Nov 2021 22:57:50 +0800 Subject: [PATCH 3/5] Remove commented unused variables. --- src/dispatch_wgl.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/dispatch_wgl.c b/src/dispatch_wgl.c index 08ecae74..dc1b0c44 100644 --- a/src/dispatch_wgl.c +++ b/src/dispatch_wgl.c @@ -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 From 337d38649a609966031d323318fd77ab0e2e2e46 Mon Sep 17 00:00:00 2001 From: gjz010 Date: Tue, 16 Nov 2021 23:16:40 +0800 Subject: [PATCH 4/5] More cleanup. Removed the STATIC_BUILD flag since the patch is not static-build specific. --- meson.build | 2 -- src/gen_dispatch.py | 27 --------------------------- 2 files changed, 29 deletions(-) diff --git a/meson.build b/meson.build index 1410a84b..e0228d1e 100644 --- a/meson.build +++ b/meson.build @@ -151,8 +151,6 @@ if libtype == 'shared' conf.set('EPOXY_PUBLIC', '__attribute__((visibility("default"))) extern') visibility_cflags += [ '-fvisibility=hidden' ] endif -else - conf.set('EPOXY_STATIC_BUILD', true) endif # The inline keyword is available only for C++ in MSVC. diff --git a/src/gen_dispatch.py b/src/gen_dispatch.py index 41704b68..15b7d4a5 100755 --- a/src/gen_dispatch.py +++ b/src/gen_dispatch.py @@ -828,22 +828,6 @@ def write_source(self, f): self.outln(' epoxy_{0}_dispatch_table_rewrite_ptr, /* {0} */'.format(func.wrapped_name)) self.outln('};') self.outln('') - """ - self.outln('#ifdef EPOXY_STATIC_BUILD') - - self.outln('#else') - 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('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('}') - self.outln('#endif /* EPOXY_STATIC_BUILD */') - self.outln('') - """ self.outln('EPOXY_THREADLOCAL struct dispatch_table {0}_tls_data = {{'.format(self.target)) for func in self.sorted_functions: @@ -864,17 +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 */') From e2f02953c52be41153e35a3ad62243e35974528b Mon Sep 17 00:00:00 2001 From: gjz010 Date: Tue, 16 Nov 2021 23:27:51 +0800 Subject: [PATCH 5/5] Adjusting empty lines. --- src/gen_dispatch.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/gen_dispatch.py b/src/gen_dispatch.py index 15b7d4a5..ed0fb953 100755 --- a/src/gen_dispatch.py +++ b/src/gen_dispatch.py @@ -848,8 +848,6 @@ def write_source(self, f): self.outln('}') self.outln('') - self.outln('') - self.outln('#endif /* !USING_DISPATCH_TABLE */') for func in self.sorted_functions: