Skip to content

Fix possible data races between the main and GL threads#2973

Merged
gonetz merged 1 commit into
masterfrom
fix_data_races
Jun 30, 2026
Merged

Fix possible data races between the main and GL threads#2973
gonetz merged 1 commit into
masterfrom
fix_data_races

Conversation

@gonetz

@gonetz gonetz commented Jun 29, 2026

Copy link
Copy Markdown
Owner

opengl_Wrapper.h / .cpp — m_swapBuffersQueued → std::atomic

The counter was incremented from the main thread (SwapBuffers) and decremented from the GL command thread (ReduceSwapBuffersQueued), with no synchronization between the two writes. That's a data race — undefined behavior in C++. std::atomic makes every read/write an atomic operation; the existing ++, --, and comparison operators all work unchanged.

glsl_CombinerProgramBuilder.h / .cpp — s_cycleType and s_textureConvert → thread_local

These were plain static class members set at the top of buildCombinerProgram and then read throughout all the _write* sub-functions. If two threads ever call buildCombinerProgram concurrently, one would stomp the other's values mid-build, corrupting shader generation. Changing them to thread_local gives each thread its own copy, so concurrent builds are isolated. No call-site changes needed — all the existing CombinerProgramBuilder::s_cycleType reads throughout the Accurate and Fast builder files automatically pick up the correct per-thread value.

opengl_Wrapper.h / .cpp — m_swapBuffersQueued → std::atomic<int>

The counter was incremented from the main thread (SwapBuffers) and decremented from the GL command thread (ReduceSwapBuffersQueued), with no synchronization between the two writes. That's a data race — undefined behavior in C++. std::atomic<int> makes every read/write an atomic operation; the existing ++, --, and comparison operators all work unchanged.
glsl_CombinerProgramBuilder.h / .cpp — s_cycleType and s_textureConvert → thread_local

These were plain static class members set at the top of buildCombinerProgram and then read throughout all the _write* sub-functions. If two threads ever call buildCombinerProgram concurrently, one would stomp the other's values mid-build, corrupting shader generation. Changing them to thread_local gives each thread its own copy, so concurrent builds are isolated. No call-site changes needed — all the existing CombinerProgramBuilder::s_cycleType reads throughout the Accurate and Fast builder files automatically pick up the correct per-thread value.
@gonetz

gonetz commented Jun 29, 2026

Copy link
Copy Markdown
Owner Author

@fzurita please review

@fzurita

fzurita commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Yes, first issue is a real issue. Second issue with the static variables, I think that is unlikely to be triggered since I think we only ever have a single thread calling buildCombinerProgram if I recall correctly? Either way, I don't think it would hurt anything to make them local.

@gonetz

gonetz commented Jun 30, 2026

Copy link
Copy Markdown
Owner Author

Good, thanks!
I'll merge it then.

@gonetz
gonetz merged commit 6d0ed40 into master Jun 30, 2026
28 checks passed
@gonetz
gonetz deleted the fix_data_races branch June 30, 2026 15:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants