From 8133830af93e8d5f4acbc9ab7e85abd34528f19d Mon Sep 17 00:00:00 2001 From: unK Date: Thu, 2 Oct 2008 15:23:33 +0200 Subject: [PATCH] split data_ready to lyrics_ready and artist_info_ready to avoid main loop lockup --- src/lyrics.cpp | 19 ++++++++++--------- src/ncmpcpp.cpp | 36 +++++++++++++++++++----------------- 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/src/lyrics.cpp b/src/lyrics.cpp index dd74c26b..a4dc973a 100644 --- a/src/lyrics.cpp +++ b/src/lyrics.cpp @@ -31,7 +31,8 @@ const string lyrics_folder = home_folder + "/" + ".lyrics"; #ifdef HAVE_CURL_CURL_H pthread_mutex_t curl = PTHREAD_MUTEX_INITIALIZER; -bool data_ready = 0; +bool artist_info_ready = 0; +bool lyrics_ready = 0; #endif namespace @@ -87,7 +88,7 @@ void * GetArtistInfo(void *ptr) *result += line + "\n"; input.close(); *result = result->substr(0, result->length()-1); - data_ready = 1; + artist_info_ready = 1; pthread_exit(result); } @@ -112,7 +113,7 @@ void * GetArtistInfo(void *ptr) if (code != CURLE_OK) { *result = "Error while fetching artist's info: " + string(curl_easy_strerror(code)); - data_ready = 1; + artist_info_ready = 1; pthread_exit(result); } @@ -126,7 +127,7 @@ void * GetArtistInfo(void *ptr) { EscapeHtml(*result); *result = "Last.fm returned an error message: " + *result; - data_ready = 1; + artist_info_ready = 1; pthread_exit(result); } @@ -205,7 +206,7 @@ void * GetArtistInfo(void *ptr) } } - data_ready = 1; + artist_info_ready = 1; pthread_exit(result); } #endif // HAVE_CURL_CURL_H @@ -231,7 +232,7 @@ void * GetLyrics(void *song) input.close(); *result = result->substr(0, result->length()-1); # ifdef HAVE_CURL_CURL_H - data_ready = 1; + lyrics_ready = 1; pthread_exit(result); # endif } @@ -262,7 +263,7 @@ void * GetLyrics(void *song) if (code != CURLE_OK) { *result = "Error while fetching lyrics: " + string(curl_easy_strerror(code)); - data_ready = 1; + lyrics_ready = 1; pthread_exit(result); } @@ -274,7 +275,7 @@ void * GetLyrics(void *song) if (*result == "Not found") { - data_ready = 1; + lyrics_ready = 1; pthread_exit(result); } @@ -292,7 +293,7 @@ void * GetLyrics(void *song) output.close(); } - data_ready = 1; + lyrics_ready = 1; pthread_exit(result); # else else diff --git a/src/ncmpcpp.cpp b/src/ncmpcpp.cpp index 0035576f..469ed318 100644 --- a/src/ncmpcpp.cpp +++ b/src/ncmpcpp.cpp @@ -109,7 +109,8 @@ NcmpcppScreen prev_screen; #ifdef HAVE_CURL_CURL_H pthread_t lyrics_downloader; pthread_t artist_info_downloader; -extern bool data_ready; +extern bool artist_info_ready; +extern bool lyrics_ready; #endif bool dont_change_now_playing = 0; @@ -677,26 +678,27 @@ int main(int argc, char *argv[]) reload_lyrics = 0; } # ifdef HAVE_CURL_CURL_H - if (data_ready) + if (artist_info_ready) { void *result; string *str_result = 0; - if (lyrics_downloader) - { - pthread_join(lyrics_downloader, &result); - str_result = static_cast(result); - sLyrics->Add(*str_result); - lyrics_downloader = 0; - } - if (artist_info_downloader) - { - pthread_join(artist_info_downloader, &result); - str_result = static_cast(result); - sInfo->Add(*str_result); - artist_info_downloader = 0; - } + pthread_join(artist_info_downloader, &result); + str_result = static_cast(result); + sInfo->Add(*str_result); delete str_result; - data_ready = 0; + artist_info_downloader = 0; + artist_info_ready = 0; + } + else if (lyrics_ready) + { + void *result; + string *str_result = 0; + pthread_join(lyrics_downloader, &result); + str_result = static_cast(result); + sLyrics->Add(*str_result); + delete str_result; + lyrics_downloader = 0; + lyrics_ready = 0; } # endif // lyrics end