Merge branch 'collectd-5.7' into collectd-5.8
[collectd.git] / src / gps.c
index 8ebbf47..b22c3a2 100644 (file)
--- a/src/gps.c
+++ b/src/gps.c
  *   Marc Fournier <marc.fournier at camptocamp.com>
  **/
 
+#include "collectd.h"
 #include "common.h"
 #include "plugin.h"
 #include "utils_time.h"
-#include "collectd.h"
 
 #define CGPS_TRUE 1
 #define CGPS_FALSE 0
@@ -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;
 
@@ -71,24 +72,16 @@ static int cgps_thread_running = CGPS_FALSE;
  * Non blocking pause for the thread.
  */
 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_unlock(&cgps_thread_lock);
+  return ret;
 }
 
 /**
@@ -148,7 +141,12 @@ static void *cgps_thread(void *pData) {
         continue;
       }
 
-      if (gps_read(&gpsd_conn) == -1) {
+#if GPSD_API_MAJOR_VERSION > 6
+      if (gps_read(&gpsd_conn, NULL, 0) == -1)
+#else
+      if (gps_read(&gpsd_conn) == -1)
+#endif
+      {
         WARNING("gps plugin: incorrect data! (err_count: %d)", err_count);
         err_count++;
 
@@ -207,14 +205,10 @@ quit:
  */
 static void cgps_submit(const char *type, gauge_t value,
                         const char *type_instance) {
-  value_t values[1];
   value_list_t vl = VALUE_LIST_INIT;
 
-  values[0].gauge = value;
-
-  vl.values = values;
+  vl.values = &(value_t){.gauge = value};
   vl.values_len = 1;
-  sstrncpy(vl.host, hostname_g, sizeof(vl.host));
   sstrncpy(vl.plugin, "gps", sizeof(vl.plugin));
   sstrncpy(vl.type, type, sizeof(vl.type));
   sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
@@ -237,7 +231,7 @@ static int cgps_read(void) {
   cgps_submit("satellites", data_copy.sats_used, "used");
   cgps_submit("satellites", data_copy.sats_visible, "visible");
 
-  return (0);
+  return 0;
 }
 
 /**
@@ -273,7 +267,7 @@ static int cgps_config(oconfig_item_t *ci) {
     cgps_config_data.timeout = CGPS_DEFAULT_TIMEOUT;
   }
 
-  return (0);
+  return 0;
 }
 
 /**
@@ -293,13 +287,14 @@ 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.");
-    return (-1);
+    return -1;
   }
 
-  return (0);
+  return 0;
 }
 
 /**
@@ -310,13 +305,13 @@ 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);
   free(res);
 
   // Clean mutex:
-  pthread_mutex_unlock(&cgps_thread_lock);
   pthread_mutex_destroy(&cgps_thread_lock);
   pthread_mutex_unlock(&cgps_data_lock);
   pthread_mutex_destroy(&cgps_data_lock);
@@ -324,7 +319,7 @@ static int cgps_shutdown(void) {
   sfree(cgps_config_data.port);
   sfree(cgps_config_data.host);
 
-  return (0);
+  return 0;
 }
 
 /**