Skip to content

Commit

Permalink
Ensure expected ordering of playlist and playback events. Closes: #959.
Browse files Browse the repository at this point in the history
  • Loading branch information
jlindgren90 committed Apr 9, 2020
1 parent 989a97d commit 1ffac65
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
25 changes: 23 additions & 2 deletions src/libaudcore/eventqueue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ struct Event : public ListNode
};

static aud::mutex mutex;
static bool paused;
static List<Event> events;
static QueuedFunc queued_events;

Expand All @@ -54,7 +55,7 @@ static void events_execute(void *)
auto mh = mutex.take();

Event * event;
while ((event = events.head()))
while (!paused && (event = events.head()))
{
events.remove(event);

Expand All @@ -72,7 +73,7 @@ EXPORT void event_queue(const char * name, void * data,
{
auto mh = mutex.take();

if (!events.head())
if (!paused && !events.head())
queued_events.queue(events_execute, nullptr);

events.append(new Event(name, data, destroy));
Expand All @@ -97,6 +98,26 @@ EXPORT void event_queue_cancel(const char * name, void * data)
}
}

// this is only for use by the playlist, to ensure that queued playlist
// updates are processed before generic events
void event_queue_pause()
{
auto mh = mutex.take();
if (!paused)
queued_events.stop();

paused = true;
}

void event_queue_unpause()
{
auto mh = mutex.take();
if (paused && events.head())
queued_events.queue(events_execute, nullptr);

paused = false;
}

void event_queue_cancel_all()
{
auto mh = mutex.take();
Expand Down
2 changes: 2 additions & 0 deletions src/libaudcore/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ void eq_set_format(int new_channels, int new_rate);
void eq_filter(float * data, int samples);

/* eventqueue.cc */
void event_queue_pause();
void event_queue_unpause();
void event_queue_cancel_all();

/* fft.cc */
Expand Down
36 changes: 16 additions & 20 deletions src/libaudcore/playlist.cc
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ static void update(void *)
update_hooks = 0;
update_level = Playlist::NoUpdate;
update_state = UpdateState::None;
event_queue_unpause();

mh.unlock();

Expand All @@ -208,6 +209,16 @@ static void update(void *)
hook_call("playback stop", nullptr);
}

static void queue_update()
{
if (update_state < UpdateState::Queued)
{
event_queue_pause(); // give playlist updates priority
queued_update.queue(update, nullptr);
update_state = UpdateState::Queued;
}
}

static void queue_update_hooks(int hooks)
{
if ((hooks & PlaybackBegin))
Expand All @@ -216,12 +227,7 @@ static void queue_update_hooks(int hooks)
update_hooks &= ~PlaybackBegin;

update_hooks |= hooks;

if (update_state < UpdateState::Queued)
{
queued_update.queue(update, nullptr);
update_state = UpdateState::Queued;
}
queue_update();
}

static void queue_global_update(Playlist::UpdateLevel level, int flags = 0)
Expand All @@ -239,11 +245,7 @@ static void queue_global_update(Playlist::UpdateLevel level, int flags = 0)
}
else
{
if (update_state < UpdateState::Queued)
{
queued_update.queue(update, nullptr);
update_state = UpdateState::Queued;
}
queue_update();
}

update_level = aud::max(update_level, level);
Expand Down Expand Up @@ -325,10 +327,7 @@ static void scan_check_complete(PlaylistData * playlist)
playlist->scan_status = PlaylistData::NotScanning;

if (update_state == UpdateState::Delayed)
{
queued_update.queue(update, nullptr);
update_state = UpdateState::Queued;
}
queue_update();

event_queue_cancel("playlist scan complete");
event_queue("playlist scan complete", nullptr);
Expand Down Expand Up @@ -497,11 +496,7 @@ void pl_signal_entry_deleted(PlaylistEntry * entry) { scan_cancel(entry); }

void pl_signal_position_changed(Playlist::ID * id)
{
if (update_state < UpdateState::Queued)
{
queued_update.queue(update, nullptr);
update_state = UpdateState::Queued;
}
queue_update();

if (id == playing_id)
{
Expand Down Expand Up @@ -615,6 +610,7 @@ void playlist_clear_updates()
update_level = Playlist::NoUpdate;
update_hooks = 0;
update_state = UpdateState::None;
event_queue_unpause();
}

void playlist_end()
Expand Down

0 comments on commit 1ffac65

Please sign in to comment.