From 676923757c4f96c5c34931da5566d5b5be3e15eb Mon Sep 17 00:00:00 2001 From: Arthur Chaloin Date: Sat, 27 Feb 2021 13:09:48 +0100 Subject: [PATCH] Fix race condition in fluid_player_callback (#783) Co-authored-by: Arthur Chaloin --- include/fluidsynth/midi.h | 1 + src/midi/fluid_midi.c | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/include/fluidsynth/midi.h b/include/fluidsynth/midi.h index 7189b740..721b1a62 100644 --- a/include/fluidsynth/midi.h +++ b/include/fluidsynth/midi.h @@ -246,6 +246,7 @@ enum fluid_player_status { FLUID_PLAYER_READY, /**< Player is ready */ FLUID_PLAYER_PLAYING, /**< Player is currently playing */ + FLUID_PLAYER_STOPPING, /**< Player is stopping, but hasn't finished yet */ FLUID_PLAYER_DONE /**< Player is finished playing */ }; diff --git a/src/midi/fluid_midi.c b/src/midi/fluid_midi.c index 04f1d1d8..a5bac502 100644 --- a/src/midi/fluid_midi.c +++ b/src/midi/fluid_midi.c @@ -2092,6 +2092,7 @@ fluid_player_callback(void *data, unsigned int msec) if(fluid_player_get_status(player) != FLUID_PLAYER_PLAYING) { fluid_synth_all_notes_off(synth, -1); + fluid_atomic_int_compare_and_exchange(&player->status, FLUID_PLAYER_STOPPING, FLUID_PLAYER_DONE); return 1; } do @@ -2154,7 +2155,7 @@ fluid_player_callback(void *data, unsigned int msec) } while(loadnextfile); - fluid_atomic_int_set(&player->status, status); + fluid_atomic_int_compare_and_exchange(&player->status, FLUID_PLAYER_PLAYING, status); return 1; } @@ -2193,7 +2194,7 @@ fluid_player_play(fluid_player_t *player) int fluid_player_stop(fluid_player_t *player) { - fluid_atomic_int_set(&player->status, FLUID_PLAYER_DONE); + fluid_atomic_int_set(&player->status, FLUID_PLAYER_STOPPING); fluid_player_seek(player, fluid_player_get_current_tick(player)); return FLUID_OK; }