split data_ready to lyrics_ready and artist_info_ready to avoid main loop lockup

This commit is contained in:
unK
2008-10-02 15:23:33 +02:00
parent e6b7e22485
commit 8133830af9
2 changed files with 29 additions and 26 deletions

View File

@@ -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

View File

@@ -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<string *>(result);
sLyrics->Add(*str_result);
lyrics_downloader = 0;
}
if (artist_info_downloader)
{
pthread_join(artist_info_downloader, &result);
str_result = static_cast<string *>(result);
sInfo->Add(*str_result);
artist_info_downloader = 0;
}
pthread_join(artist_info_downloader, &result);
str_result = static_cast<string *>(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<string *>(result);
sLyrics->Add(*str_result);
delete str_result;
lyrics_downloader = 0;
lyrics_ready = 0;
}
# endif
// lyrics end