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

Discussion: Make pygame-ce thread safe with concurrent multi threading #3281

Open
damusss opened this issue Jan 5, 2025 · 3 comments
Open

Comments

@damusss
Copy link
Member

damusss commented Jan 5, 2025

Thread safety is a big topic like SDL3 porting, so I think a github issue can be helpful to discuss it, as discord messages get lost quickly.

@damusss
Copy link
Member Author

damusss commented Jan 5, 2025

Surface thread safety design proposal

Threads reading or writing to the same surface is something I can see happening easily. It's not inherently bad, but not the best practice or bad if they both write to it. Surface locking is not thread safe, so:

  • Add a SDL_mutex field in the surface object struct, that is created when the surface is created and is destroyed when it is freed
  • Create macros that when the python version is high enough and the gil is disabled lock the mutex before pixel access and unlock it after that

I know that atomic flags exist, but I'm not sure how reliable they are. If they are a better option we should use them.

@Starbuck5
Copy link
Member

pgSurface_Lock could also be made thread safe by making it a critical section.

@damusss
Copy link
Member Author

damusss commented Jan 6, 2025

pgSurface_Lock could also be made thread safe by making it a critical section.

Yeah it makes sense. SDL_Surface s created inside the functions cannot be modified by other threads anyways, only pygame surface objects so we can embed the mutex/atomic thread safety within that function. We "just" need to check how it is used right now and correct it if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants