improvements for pthread related stuff

This commit is contained in:
Andrzej Rybczak
2009-01-20 14:35:49 +01:00
parent 34e3aaa782
commit f080b3e1f1
2 changed files with 47 additions and 45 deletions

View File

@@ -37,6 +37,8 @@ const string lyrics_folder = home_folder + "/" + ".lyrics";
#ifdef HAVE_CURL_CURL_H #ifdef HAVE_CURL_CURL_H
extern pthread_t lyrics_downloader; extern pthread_t lyrics_downloader;
extern pthread_t artist_info_downloader; extern pthread_t artist_info_downloader;
extern bool lyrics_ready;
extern bool artist_info_ready;
pthread_mutex_t curl = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t curl = PTHREAD_MUTEX_INITIALIZER;
#endif #endif
@@ -97,10 +99,7 @@ void * GetArtistInfo(void *ptr)
first = 0; first = 0;
} }
input.close(); input.close();
sInfo->Flush(); artist_info_ready = 1;
if (wCurrent == sInfo)
sInfo->Refresh();
artist_info_downloader = 0;
pthread_exit(NULL); pthread_exit(NULL);
} }
@@ -125,10 +124,7 @@ void * GetArtistInfo(void *ptr)
if (code != CURLE_OK) if (code != CURLE_OK)
{ {
*sInfo << "Error while fetching artist's info: " << curl_easy_strerror(code); *sInfo << "Error while fetching artist's info: " << curl_easy_strerror(code);
sInfo->Flush(); artist_info_ready = 1;
if (wCurrent == sInfo)
sInfo->Refresh();
artist_info_downloader = 0;
pthread_exit(NULL); pthread_exit(NULL);
} }
@@ -142,10 +138,7 @@ void * GetArtistInfo(void *ptr)
{ {
EscapeHtml(result); EscapeHtml(result);
*sInfo << "Last.fm returned an error message: " << result; *sInfo << "Last.fm returned an error message: " << result;
sInfo->Flush(); artist_info_ready = 1;
if (wCurrent == sInfo)
sInfo->Refresh();
artist_info_downloader = 0;
pthread_exit(NULL); pthread_exit(NULL);
} }
@@ -240,10 +233,7 @@ void * GetArtistInfo(void *ptr)
output.close(); output.close();
} }
} }
sInfo->Flush(); artist_info_ready = 1;
if (wCurrent == sInfo)
sInfo->Refresh();
artist_info_downloader = 0;
pthread_exit(NULL); pthread_exit(NULL);
} }
#endif // HAVE_CURL_CURL_H #endif // HAVE_CURL_CURL_H
@@ -275,10 +265,7 @@ void *GetLyrics(void *song)
first = 0; first = 0;
} }
# ifdef HAVE_CURL_CURL_H # ifdef HAVE_CURL_CURL_H
sLyrics->Flush(); lyrics_ready = 1;
if (wCurrent == sLyrics)
sLyrics->Refresh();
lyrics_downloader = 0;
pthread_exit(NULL); pthread_exit(NULL);
# endif # endif
} }
@@ -314,10 +301,7 @@ void *GetLyrics(void *song)
if (code != CURLE_OK) if (code != CURLE_OK)
{ {
*sLyrics << "Error while fetching lyrics: " << curl_easy_strerror(code); *sLyrics << "Error while fetching lyrics: " << curl_easy_strerror(code);
sLyrics->Flush(); lyrics_ready = 1;
if (wCurrent == sLyrics)
sLyrics->Refresh();
lyrics_downloader = 0;
pthread_exit(NULL); pthread_exit(NULL);
} }
@@ -330,10 +314,7 @@ void *GetLyrics(void *song)
if (result == "Not found") if (result == "Not found")
{ {
*sLyrics << result; *sLyrics << result;
sLyrics->Flush(); lyrics_ready = 1;
if (wCurrent == sLyrics)
sLyrics->Refresh();
lyrics_downloader = 0;
pthread_exit(NULL); pthread_exit(NULL);
} }
@@ -354,17 +335,11 @@ void *GetLyrics(void *song)
output << result; output << result;
output.close(); output.close();
} }
sLyrics->Flush(); lyrics_ready = 1;
if (wCurrent == sLyrics)
sLyrics->Refresh();
lyrics_downloader = 0;
pthread_exit(NULL); pthread_exit(NULL);
# else # else
else else
*sLyrics << "Local lyrics not found. As ncmpcpp has been compiled without curl support, you can put appropriate lyrics into ~/.lyrics directory (file syntax is \"ARTIST - TITLE.txt\") or recompile ncmpcpp with curl support."; *sLyrics << "Local lyrics not found. As ncmpcpp has been compiled without curl support, you can put appropriate lyrics into ~/.lyrics directory (file syntax is \"ARTIST - TITLE.txt\") or recompile ncmpcpp with curl support.";
sLyrics->Flush();
if (wCurrent == sLyrics)
sLyrics->Refresh();
return NULL; return NULL;
# endif # endif
} }

View File

@@ -144,6 +144,8 @@ NcmpcppScreen prev_screen;
#ifdef HAVE_CURL_CURL_H #ifdef HAVE_CURL_CURL_H
pthread_t lyrics_downloader; pthread_t lyrics_downloader;
pthread_t artist_info_downloader; pthread_t artist_info_downloader;
bool lyrics_ready = 0;
bool artist_info_ready = 0;
#endif #endif
bool dont_change_now_playing = 0; bool dont_change_now_playing = 0;
@@ -373,12 +375,6 @@ int main(int argc, char *argv[])
signal(SIGPIPE, SIG_IGN); signal(SIGPIPE, SIG_IGN);
# ifdef HAVE_CURL_CURL_H
pthread_attr_t attr_detached;
pthread_attr_init(&attr_detached);
pthread_attr_setdetachstate(&attr_detached, PTHREAD_CREATE_DETACHED);
# endif // HAVE_CURL_CURL_H
gettimeofday(&now, 0); gettimeofday(&now, 0);
while (!main_exit) while (!main_exit)
@@ -803,6 +799,22 @@ int main(int argc, char *argv[])
else else
reload_lyrics = 0; reload_lyrics = 0;
} }
# ifdef HAVE_CURL_CURL_H
if (artist_info_ready)
{
pthread_join(artist_info_downloader, NULL);
sInfo->Flush();
artist_info_downloader = 0;
artist_info_ready = 0;
}
else if (lyrics_ready)
{
pthread_join(lyrics_downloader, NULL);
sLyrics->Flush();
lyrics_downloader = 0;
lyrics_ready = 0;
}
# endif
if (Config.columns_in_playlist && wCurrent == mPlaylist) if (Config.columns_in_playlist && wCurrent == mPlaylist)
wCurrent->Display(); wCurrent->Display();
@@ -3443,6 +3455,12 @@ int main(int argc, char *argv[])
|| (wCurrent == mPlaylistEditor && !mPlaylistEditor->Empty()) || (wCurrent == mPlaylistEditor && !mPlaylistEditor->Empty())
|| (wCurrent == mEditorTags && !mEditorTags->Empty())) || (wCurrent == mEditorTags && !mEditorTags->Empty()))
{ {
if (artist_info_downloader)
{
ShowMessage("Artist's info is being downloaded...");
continue;
}
string *artist = new string(); string *artist = new string();
int id = ((Menu<Song> *)wCurrent)->Choice(); int id = ((Menu<Song> *)wCurrent)->Choice();
switch (current_screen) switch (current_screen)
@@ -3481,7 +3499,7 @@ int main(int argc, char *argv[])
sInfo->Refresh(); sInfo->Refresh();
if (!artist_info_downloader) if (!artist_info_downloader)
{ {
pthread_create(&artist_info_downloader, &attr_detached, GetArtistInfo, artist); pthread_create(&artist_info_downloader, NULL, GetArtistInfo, artist);
} }
} }
else else
@@ -3523,6 +3541,14 @@ int main(int argc, char *argv[])
{ {
LOAD_LYRICS: LOAD_LYRICS:
# ifdef HAVE_CURL_CURL_H
if (lyrics_downloader)
{
ShowMessage("Lyrics are being downloaded...");
continue;
}
# endif
Song *s = 0; Song *s = 0;
int id; int id;
@@ -3566,18 +3592,19 @@ int main(int argc, char *argv[])
wPrev = wCurrent; wPrev = wCurrent;
prev_screen = current_screen; prev_screen = current_screen;
wCurrent = sLyrics; wCurrent = sLyrics;
redraw_header = 1;
wCurrent->Clear();
current_screen = csLyrics; current_screen = csLyrics;
redraw_header = 1;
sLyrics->Clear();
sLyrics->WriteXY(0, 0, 0, "Fetching lyrics..."); sLyrics->WriteXY(0, 0, 0, "Fetching lyrics...");
sLyrics->Refresh(); sLyrics->Refresh();
# ifdef HAVE_CURL_CURL_H # ifdef HAVE_CURL_CURL_H
if (!lyrics_downloader) if (!lyrics_downloader)
{ {
pthread_create(&lyrics_downloader, &attr_detached, GetLyrics, s); pthread_create(&lyrics_downloader, NULL, GetLyrics, s);
} }
# else # else
GetLyrics(s); GetLyrics(s);
sLyrics->Flush();
# endif # endif
} }
} }