From 3c4d8f65896253a82e19adcbe2808a863a99f74f Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Thu, 15 Mar 2018 12:53:56 +0100 Subject: Properly protect player struct with mutex The volatile keyword neither guarantees atomic access nor memory visibility[1]. Although this is usually not a problem on x86, it is incorrect to rely on this. Use mutex locks to protect all shared player variables and enforce memory visibility. [1] https://wiki.sei.cmu.edu/confluence/display/c/CON02-C.+Do+not+use+volatile+as+a+synchronization+primitive --- src/ui.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'src/ui.c') diff --git a/src/ui.c b/src/ui.c index 0512f36..5c4e42d 100644 --- a/src/ui.c +++ b/src/ui.c @@ -1,5 +1,5 @@ /* -Copyright (c) 2008-2013 +Copyright (c) 2008-2018 Lars-Dominik Braun Permission is hereby granted, free of charge, to any person obtaining a copy @@ -814,7 +814,7 @@ size_t BarUiListSongs (const BarApp_t * const app, */ void BarUiStartEventCmd (const BarSettings_t *settings, const char *type, const PianoStation_t *curStation, const PianoSong_t *curSong, - const player_t * const player, PianoStation_t *stations, + player_t * const player, PianoStation_t *stations, PianoReturn_t pRet, CURLcode wRet) { pid_t chld; int pipeFd[2]; @@ -855,6 +855,11 @@ void BarUiStartEventCmd (const BarSettings_t *settings, const char *type, songStation = PianoFindStationById (stations, curSong->stationId); } + pthread_mutex_lock (&player->lock); + const unsigned int songDuration = player->songDuration; + const unsigned int songPlayed = player->songPlayed; + pthread_mutex_unlock (&player->lock); + fprintf (pipeWriteFd, "artist=%s\n" "title=%s\n" @@ -880,8 +885,8 @@ void BarUiStartEventCmd (const BarSettings_t *settings, const char *type, PianoErrorToStr (pRet), wRet, curl_easy_strerror (wRet), - player->songDuration, - player->songPlayed, + songDuration, + songPlayed, curSong == NULL ? PIANO_RATE_NONE : curSong->rating, curSong == NULL ? "" : curSong->detailUrl ); -- cgit v1.2.3