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

SDL: Call multiplayer->update() from the user code thread #716

Merged
merged 2 commits into from
Oct 7, 2021

Conversation

ali1234
Copy link
Contributor

@ali1234 ali1234 commented Sep 29, 2021

The multiplayer update scans for messages and passes them in to the
user defined function on_message(). This should be done in the user
code thread for two reasons:

  1. Calling it from a different thread means on_message() and update()
    are being run in different threads, and therefore all code in both of
    them has to be thread safe. This is pretty hard to do with the 32blit
    API and not really in the spirit of what this is supposed to be.

  2. on_message() is a user-supplied function and so bugs can make it
    hang. We can detect hangs in update() and render() thanks to running
    them on the user thread (via blit::tick()). Running on_message() on
    that thread allows to detect hangs there too.

@@ -269,6 +269,7 @@ void System::loop()
blit::joystick.y = shadow_joystick[1];
SDL_UnlockMutex(m_input);
blit::tick(::now());
blit_multiplayer->update();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know the indentation is a horrible mess, but that's definitely a bit wonky. (I guess github is getting 2 spaces from the editorconfig file)

@ali1234 ali1234 force-pushed the multiplayer_threadsafe_sdl branch 2 times, most recently from 8618be9 to b2cf450 Compare September 29, 2021 20:32
@ali1234
Copy link
Contributor Author

ali1234 commented Sep 29, 2021

This didn't actually fix the problems I'm seeing with multiplayer.

Also it will lock the SDL framerate to 50 fps. I'm not sure if calling render outside the user thread does any better job there though.

The multiplayer update scans for messages and passes them in to the
user defined function on_message(). This should be done in the user
code thread for two reasons:

1. Calling it from a different thread means on_message() and update()
are being run in different threads, and therefore all code in both of
them has to be thread safe. This is pretty hard to do with the 32blit
API and not really in the spirit of what this is supposed to be.

2. on_message() is a user-supplied function and so bugs can make it
hang. We can detect hangs in update() and render() thanks to running
them on the user thread (via blit::tick()). Running on_message() on
that thread allows to detect hangs there too.
Someone moved the call to render out of the thread loop and into
the texture update function. Same rationale for putting it back.
It should not overlap other user code and it should be possible
to detect if the user code hangs.
@Gadgetoid
Copy link
Contributor

Looks like I put render into update_texture since it had gone awol: 34981bb but I have no recollection where or why.

All this threading, event posting and semaphore waiting makes my head spin!

Do the last two commits fix your multiplayer woes?

@ali1234
Copy link
Contributor Author

ali1234 commented Oct 7, 2021

Multiplayer does seem to be completely solid after this change, but I'm not totally sure this is the reason, or if it is just because I fixed all the bugs in my code. On the other hand I have not noticed anything behaving worse with this change, and it is technically the right thing to do. The idea behind the thread is that any code written by the user (init, update, render, on_message) can potentially go into an infinite loop and we want to detect it. So for that reason it is (supposed to) all run on the user thread.

@Gadgetoid Gadgetoid merged commit e78a346 into 32blit:master Oct 7, 2021
@Gadgetoid
Copy link
Contributor

Thank you!

Looking forward to one day getting some multiplayer blit action 😆

@ali1234 ali1234 deleted the multiplayer_threadsafe_sdl branch October 22, 2021 22:04
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.

3 participants