X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fgps.c;h=81056e3c973a80f07188e9549e7132554e847862;hb=6d8031d73b7c1d874d7afa4cad2f248c4073764d;hp=644967b3b7ec79c2bc4e2c977138dd34086041b3;hpb=8c5927c52f4eefebaad3a6ecadc253ee9007ebb5;p=collectd.git diff --git a/src/gps.c b/src/gps.c index 644967b3..81056e3c 100644 --- a/src/gps.c +++ b/src/gps.c @@ -64,6 +64,7 @@ static cgps_data_t cgps_data = {NAN, NAN, NAN, NAN}; static pthread_t cgps_thread_id; static pthread_mutex_t cgps_data_lock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t cgps_thread_lock = PTHREAD_MUTEX_INITIALIZER; +static pthread_cond_t cgps_thread_cond = PTHREAD_COND_INITIALIZER; static int cgps_thread_shutdown = CGPS_FALSE; static int cgps_thread_running = CGPS_FALSE; @@ -72,27 +73,16 @@ static int cgps_thread_running = CGPS_FALSE; */ static int cgps_thread_pause(cdtime_t pTime) { - cdtime_t now; - now = cdtime (); - struct timespec pause_th; - CDTIME_T_TO_TIMESPEC (MS_TO_CDTIME_T(10), &pause_th); - while (CGPS_TRUE) - { - if ( (cdtime () - now) > pTime ) - { - break; - } + cdtime_t until = cdtime() + pTime; - pthread_mutex_lock (&cgps_thread_lock); - if (cgps_thread_shutdown == CGPS_TRUE) - { - return CGPS_FALSE; - } - pthread_mutex_unlock (&cgps_thread_lock); - nanosleep (&pause_th, NULL); - } + pthread_mutex_lock (&cgps_thread_lock); + pthread_cond_timedwait (&cgps_thread_cond, &cgps_thread_lock, + &CDTIME_T_TO_TIMESPEC (until)); + + int ret = !cgps_thread_shutdown; - return CGPS_TRUE; + pthread_mutex_lock (&cgps_thread_lock); + return ret; } /** @@ -310,7 +300,7 @@ static int cgps_init (void) CDTIME_T_TO_DOUBLE (cgps_config_data.timeout), CDTIME_T_TO_DOUBLE (cgps_config_data.pause_connect)); - status = plugin_thread_create (&cgps_thread_id, NULL, cgps_thread, NULL); + status = plugin_thread_create (&cgps_thread_id, NULL, cgps_thread, NULL, "gps"); if (status != 0) { ERROR ("gps plugin: pthread_create() failed."); @@ -329,6 +319,7 @@ static int cgps_shutdown (void) pthread_mutex_lock (&cgps_thread_lock); cgps_thread_shutdown = CGPS_TRUE; + pthread_cond_broadcast (&cgps_thread_cond); pthread_mutex_unlock (&cgps_thread_lock); pthread_join(cgps_thread_id, &res);