diff options
-rw-r--r-- | deadbeef.h | 3 | ||||
-rw-r--r-- | plugins.c | 1 | ||||
-rw-r--r-- | plugins/lastfm/lastfm.c | 11 | ||||
-rw-r--r-- | threading.h | 6 | ||||
-rw-r--r-- | threading_pthread.c | 28 |
5 files changed, 36 insertions, 13 deletions
@@ -123,7 +123,8 @@ typedef struct { // process control void (*quit) (void); // threading - void (*thread_start) (void (*fn)(uintptr_t ctx), uintptr_t ctx); + int (*thread_start) (void (*fn)(uintptr_t ctx), uintptr_t ctx); + int (*thread_join) (int tid); uintptr_t (*mutex_create) (void); void (*mutex_free) (uintptr_t mtx); int (*mutex_lock) (uintptr_t mtx); @@ -38,6 +38,7 @@ DB_functions_t deadbeef_api = { .quit = plug_quit, // threading .thread_start = thread_start, + .thread_join = thread_join, .mutex_create = mutex_create, .mutex_free = mutex_free, .mutex_lock = mutex_lock, diff --git a/plugins/lastfm/lastfm.c b/plugins/lastfm/lastfm.c index 15c9508e..190fa1a0 100644 --- a/plugins/lastfm/lastfm.c +++ b/plugins/lastfm/lastfm.c @@ -46,6 +46,7 @@ static char lfm_submission_url[256]; static uintptr_t lfm_mutex; static uintptr_t lfm_cond; static int lfm_stopthread; +static int lfm_tid; DB_plugin_t * lastfm_load (DB_functions_t *api) { @@ -381,9 +382,8 @@ lfm_thread (uintptr_t ctx) { fprintf (stderr, "cond signalled!\n"); if (lfm_stopthread) { deadbeef->mutex_unlock (lfm_mutex); + deadbeef->cond_signal (lfm_cond); fprintf (stderr, "lfm_thread end\n"); - deadbeef->cond_free (lfm_cond); - deadbeef->mutex_free (lfm_mutex); return; } deadbeef->mutex_unlock (lfm_mutex); @@ -491,7 +491,7 @@ lastfm_start (void) { lfm_stopthread = 0; lfm_mutex = deadbeef->mutex_create (); lfm_cond = deadbeef->cond_create (); - deadbeef->thread_start (lfm_thread, 0); + lfm_tid = deadbeef->thread_start (lfm_thread, 0); // subscribe to frameupdate event deadbeef->ev_subscribe (DB_PLUGIN (&plugin), DB_EV_SONGSTARTED, DB_CALLBACK (lastfm_songstarted), 0); deadbeef->ev_subscribe (DB_PLUGIN (&plugin), DB_EV_SONGFINISHED, DB_CALLBACK (lastfm_songfinished), 0); @@ -560,7 +560,10 @@ lastfm_stop (void) { deadbeef->ev_unsubscribe (DB_PLUGIN (&plugin), DB_EV_SONGFINISHED, DB_CALLBACK (lastfm_songfinished), 0); lfm_stopthread = 1; deadbeef->cond_signal (lfm_cond); - fprintf (stderr, "signalled to stop thread\n"); + deadbeef->thread_join (lfm_tid); + lfm_tid = -1; + deadbeef->cond_free (lfm_cond); + deadbeef->mutex_free (lfm_mutex); return 0; } diff --git a/threading.h b/threading.h index cb71b933..f899e73f 100644 --- a/threading.h +++ b/threading.h @@ -20,7 +20,11 @@ #include <stdint.h> -void thread_start (void (*fn)(uintptr_t ctx), uintptr_t ctx); +int +thread_start (void (*fn)(uintptr_t ctx), uintptr_t ctx); + +int +thread_join (int tid); uintptr_t mutex_create (void); void mutex_free (uintptr_t mtx); diff --git a/threading_pthread.c b/threading_pthread.c index 7bb3d50b..0934ab4a 100644 --- a/threading_pthread.c +++ b/threading_pthread.c @@ -20,26 +20,40 @@ #include <stdlib.h> #include "threading.h" -void +int thread_start (void (*fn)(uintptr_t ctx), uintptr_t ctx) { pthread_t tid; pthread_attr_t attr; int s = pthread_attr_init (&attr); if (s) { - printf ("pthread_attr_init failed\n"); - return; + fprintf (stderr, "pthread_attr_init failed\n"); + return -1; } if (pthread_create (&tid, &attr, (void *(*)(void *))fn, (void*)ctx)) { - printf ("pthread_create failed\n"); - return; + fprintf (stderr, "pthread_create failed\n"); + return -1; } s = pthread_attr_destroy (&attr); if (s) { - printf ("pthread_attr_destroy failed\n"); - return; + fprintf (stderr, "pthread_attr_destroy failed\n"); + pthread_cancel (tid); + return -1; + } + return tid; +} + +int +thread_join (int tid) { + void *retval; + int s = pthread_join ((pthread_t)tid, &retval); + if (s) { + fprintf (stderr, "pthread_join failed\n"); + return -1; } + return 0; } + uintptr_t mutex_create (void) { pthread_mutex_t *mtx = malloc (sizeof (pthread_mutex_t)); |