diff options
author | Lars-Dominik Braun <lars@6xq.net> | 2018-03-15 12:53:56 +0100 |
---|---|---|
committer | Lars-Dominik Braun <lars@6xq.net> | 2018-03-15 12:53:56 +0100 |
commit | 3c4d8f65896253a82e19adcbe2808a863a99f74f (patch) | |
tree | fc7c3fbe15027e752c8a7da3d42c9a37d4d72c87 /src/ui.c | |
parent | 2e51a13fe816c0c0b02f7d7a19a4c739dcb66119 (diff) | |
download | pianobar-3c4d8f65896253a82e19adcbe2808a863a99f74f.tar.gz pianobar-3c4d8f65896253a82e19adcbe2808a863a99f74f.tar.bz2 pianobar-3c4d8f65896253a82e19adcbe2808a863a99f74f.zip |
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
Diffstat (limited to 'src/ui.c')
-rw-r--r-- | src/ui.c | 13 |
1 files changed, 9 insertions, 4 deletions
@@ -1,5 +1,5 @@ /* -Copyright (c) 2008-2013 +Copyright (c) 2008-2018 Lars-Dominik Braun <lars@6xq.net> 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 ); |