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

[raudio] Fix 3664: crash in raudio from multithreading issues #3907

Merged
merged 7 commits into from
Apr 20, 2024

Conversation

dertseha
Copy link
Contributor

@dertseha dertseha commented Apr 9, 2024

This pull request fixes #3664 .

However, it fixes things slightly different than originally thought/intended.

The triggering issue with UpdateMusicStream() was in as much mitigated by removing the entire hack code, as it was a no-op. See comment for details.

Included changes:

  • Remove hack from UpdateMusicStream() and PlayMusicStream()
  • Add mutex lock/unlock to all functions called from main thread that share buffer member access
    • This required separation of some of the functions, as they are called from both threads and mutex is not re-entrant. Suffix InLockedState was added to indicate this.
  • Fix for cleanup of order of buffer/converter; Initialisation sets up converter, then tracks buffer - deinit must do inverse

I could run all the audio examples and play with them. Still, this is only a small test for a change like this. Please review carefully.

dertseha added 7 commits April 9, 2024 10:00
First it needs to be taken out of the processing chain, then it can be released. The inverse of the initialization.
First it needs to be taken out of the processing chain, then it can be released. The inverse of the initialization.
This change is twofold:
* Add locks to UpdateMusicStream/UpdateAudioStream (second one needed separation)
* Remove unnecessary hack to restart music - inlining the statements resulted in a no-op

Especially the second part made it easier to ensure thread-safety overall
@dertseha dertseha changed the title Fix 3664: crash in raudio from multithreading issues [raudio] Fix 3664: crash in raudio from multithreading issues Apr 11, 2024
@@ -431,8 +431,10 @@ static bool SaveFileText(const char *fileName, char *text); // Save text
AudioBuffer *LoadAudioBuffer(ma_format format, ma_uint32 channels, ma_uint32 sampleRate, ma_uint32 sizeInFrames, int usage);
void UnloadAudioBuffer(AudioBuffer *buffer);

bool IsAudioBufferPlayingInLockedState(AudioBuffer *buffer);
Copy link
Owner

Choose a reason for hiding this comment

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

Why are those functions required? Could they just be avoided?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is what I mean in the description with

This required separation of some of the functions, as they are called from both threads and mutex is not re-entrant.

Calling lock twice within a call-stack will end up in a deadlock.

Copy link
Owner

Choose a reason for hiding this comment

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

Ok, I see, in that case, could those functions be kept internal to raudio module?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Those functions are "internal" - or what do you mean? Do you mean to make them static ?

@raysan5
Copy link
Owner

raysan5 commented Apr 17, 2024

@dertseha Thanks for the review, just added a comment

src/raudio.c Show resolved Hide resolved
@@ -431,8 +431,10 @@ static bool SaveFileText(const char *fileName, char *text); // Save text
AudioBuffer *LoadAudioBuffer(ma_format format, ma_uint32 channels, ma_uint32 sampleRate, ma_uint32 sizeInFrames, int usage);
void UnloadAudioBuffer(AudioBuffer *buffer);

bool IsAudioBufferPlayingInLockedState(AudioBuffer *buffer);
Copy link
Owner

Choose a reason for hiding this comment

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

Ok, I see, in that case, could those functions be kept internal to raudio module?

@raysan5 raysan5 merged commit d80a622 into raysan5:master Apr 20, 2024
@raysan5
Copy link
Owner

raysan5 commented Apr 20, 2024

@dertseha Thanks for the fix, I'm reviewing the exposure of the functions.

@dertseha dertseha deleted the fix-3664-crash-in-raudio branch May 1, 2024 05:15
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.

[raudio] Crash encountered with mod(.xm) playback
2 participants