enable error checking of mutex, log mutex errors
authorJean-Philippe Orsini <jeanfi@gmail.com>
Mon, 4 Aug 2014 18:49:53 +0000 (20:49 +0200)
committerJean-Philippe Orsini <jeanfi@gmail.com>
Mon, 4 Aug 2014 18:49:53 +0000 (20:49 +0200)
src/lib/Makefile.am
src/lib/Makefile.in
src/lib/pmutex.c [new file with mode: 0644]
src/lib/pmutex.h [new file with mode: 0644]
src/main.c
src/server/server.c

index df0c5eb..da1dbfb 100644 (file)
@@ -1,19 +1,20 @@
 noinst_LIBRARIES = libpsensor.a
 
 libpsensor_a_SOURCES = \
-       amd.h \
-       bool.h \
-       color.h color.c \
-       cpu.h \
+       amd.h\
+       bool.h\
+       color.h color.c\
+       cpu.h\
        hdd.h hdd_hddtemp.c\
-       lmsensor.h lmsensor.c \
-       plog.h plog.c \
+       lmsensor.h lmsensor.c\
        measure.h measure.c \
-       ptime.h ptime.c \
-       nvidia.h \
+       nvidia.h\
+       plog.h plog.c\
+       pmutex.h pmutex.c\
        psensor.h psensor.c\
-       pio.h pio.c \
-       slog.c slog.h \
+       ptime.h ptime.c\
+       pio.h pio.c\
+       slog.c slog.h\
        url.c url.h
 
 AM_CPPFLAGS = -Wall -Werror
index b15bd9d..f81cc57 100644 (file)
@@ -79,20 +79,22 @@ ARFLAGS = cru
 libpsensor_a_AR = $(AR) $(ARFLAGS)
 libpsensor_a_LIBADD =
 am__libpsensor_a_SOURCES_DIST = amd.h bool.h color.h color.c cpu.h \
-       hdd.h hdd_hddtemp.c lmsensor.h lmsensor.c plog.h plog.c \
-       measure.h measure.c ptime.h ptime.c nvidia.h psensor.h \
-       psensor.c pio.h pio.c slog.c slog.h url.c url.h hdd_atasmart.c \
-       nvidia.c amd.c cpu.c psensor_json.h psensor_json.c
+       hdd.h hdd_hddtemp.c lmsensor.h lmsensor.c measure.h measure.c \
+       nvidia.h plog.h plog.c pmutex.h pmutex.c psensor.h psensor.c \
+       ptime.h ptime.c pio.h pio.c slog.c slog.h url.c url.h \
+       hdd_atasmart.c nvidia.c amd.c cpu.c psensor_json.h \
+       psensor_json.c
 @ATASMART_TRUE@am__objects_1 = hdd_atasmart.$(OBJEXT)
 @NVIDIA_TRUE@am__objects_2 = nvidia.$(OBJEXT)
 @LIBATIADL_TRUE@am__objects_3 = amd.$(OBJEXT)
 @GTOP_TRUE@am__objects_4 = cpu.$(OBJEXT)
 @JSON_TRUE@am__objects_5 = psensor_json.$(OBJEXT)
 am_libpsensor_a_OBJECTS = color.$(OBJEXT) hdd_hddtemp.$(OBJEXT) \
-       lmsensor.$(OBJEXT) plog.$(OBJEXT) measure.$(OBJEXT) \
-       ptime.$(OBJEXT) psensor.$(OBJEXT) pio.$(OBJEXT) slog.$(OBJEXT) \
-       url.$(OBJEXT) $(am__objects_1) $(am__objects_2) \
-       $(am__objects_3) $(am__objects_4) $(am__objects_5)
+       lmsensor.$(OBJEXT) measure.$(OBJEXT) plog.$(OBJEXT) \
+       pmutex.$(OBJEXT) psensor.$(OBJEXT) ptime.$(OBJEXT) \
+       pio.$(OBJEXT) slog.$(OBJEXT) url.$(OBJEXT) $(am__objects_1) \
+       $(am__objects_2) $(am__objects_3) $(am__objects_4) \
+       $(am__objects_5)
 libpsensor_a_OBJECTS = $(am_libpsensor_a_OBJECTS)
 DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)
 depcomp = $(SHELL) $(top_srcdir)/depcomp
@@ -273,11 +275,11 @@ top_builddir = @top_builddir@
 top_srcdir = @top_srcdir@
 noinst_LIBRARIES = libpsensor.a
 libpsensor_a_SOURCES = amd.h bool.h color.h color.c cpu.h hdd.h \
-       hdd_hddtemp.c lmsensor.h lmsensor.c plog.h plog.c measure.h \
-       measure.c ptime.h ptime.c nvidia.h psensor.h psensor.c pio.h \
-       pio.c slog.c slog.h url.c url.h $(am__append_1) \
-       $(am__append_4) $(am__append_7) $(am__append_8) \
-       $(am__append_11)
+       hdd_hddtemp.c lmsensor.h lmsensor.c measure.h measure.c \
+       nvidia.h plog.h plog.c pmutex.h pmutex.c psensor.h psensor.c \
+       ptime.h ptime.c pio.h pio.c slog.c slog.h url.c url.h \
+       $(am__append_1) $(am__append_4) $(am__append_7) \
+       $(am__append_8) $(am__append_11)
 AM_CPPFLAGS = -Wall -Werror $(am__append_3) $(am__append_6) \
        $(am__append_10) $(am__append_13)
 EXTRA_DIST = $(libpsensor_a_SOURCES) \
@@ -343,6 +345,7 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/nvidia.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pio.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/plog.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pmutex.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/psensor.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/psensor_json.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ptime.Po@am__quote@
diff --git a/src/lib/pmutex.c b/src/lib/pmutex.c
new file mode 100644 (file)
index 0000000..ec529bd
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * Copyright (C) 2010-2014 jeanfi@gmail.com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+
+#include <stdio.h>
+
+#include <plog.h>
+#include <pmutex.h>
+
+int pmutex_lock(pthread_mutex_t *m)
+{
+       int ret;
+
+       ret = pthread_mutex_lock(m);
+
+       if (ret)
+               log_err("pmutex_lock: %p %d", m, ret);
+
+       return ret;
+}
+
+int pmutex_unlock(pthread_mutex_t *m)
+{
+       int ret;
+
+       ret = pthread_mutex_unlock(m);
+
+       if (ret)
+               log_err("pmutex_unlock: %p %d", m, ret);
+
+       return ret;
+}
+
+int pmutex_init(pthread_mutex_t *m)
+{
+       pthread_mutexattr_t attr;
+       int ret;
+
+       pthread_mutexattr_init(&attr);
+       pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
+
+       ret = pthread_mutex_init(m, &attr);
+
+       if (ret)
+               log_err("pmutex_init: %p %d", m, ret);
+
+       return ret;
+}
diff --git a/src/lib/pmutex.h b/src/lib/pmutex.h
new file mode 100644 (file)
index 0000000..f27c6e5
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2010-2014 jeanfi@gmail.com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+#ifndef _P_PMUTEX_H_
+#define _P_PMUTEX_H_
+
+#include <pthread.h>
+
+int pmutex_lock(pthread_mutex_t *);
+int pmutex_unlock(pthread_mutex_t *);
+int pmutex_init(pthread_mutex_t *);
+
+#endif
index 6ac4846..8d57f58 100644 (file)
@@ -38,6 +38,7 @@
 #include "ui_color.h"
 #include "lmsensor.h"
 #include "notify_cmd.h"
+#include <pmutex.h>
 #include "slog.h"
 #include "ui_pref.h"
 #include "ui_graph.h"
@@ -144,7 +145,7 @@ static void update_measures(struct ui_psensor *ui)
        cfg = ui->config;
 
        while (1) {
-               pthread_mutex_lock(&ui->sensors_mutex);
+               pmutex_lock(&ui->sensors_mutex);
 
                sensors = ui->sensors;
                if (!sensors)
@@ -167,7 +168,7 @@ static void update_measures(struct ui_psensor *ui)
 
                period = cfg->sensor_update_interval;
 
-               pthread_mutex_unlock(&ui->sensors_mutex);
+               pmutex_unlock(&ui->sensors_mutex);
 
                sleep(period);
        }
@@ -207,7 +208,7 @@ gboolean ui_refresh_thread(gpointer data)
        ret = TRUE;
        cfg = ui->config;
 
-       pthread_mutex_lock(&ui->sensors_mutex);
+       pmutex_lock(&ui->sensors_mutex);
 
        graph_update(ui->sensors, ui->w_graph, ui->config, ui->main_window);
 
@@ -227,7 +228,7 @@ gboolean ui_refresh_thread(gpointer data)
                ret = FALSE;
        }
 
-       pthread_mutex_unlock(&ui->sensors_mutex);
+       pmutex_unlock(&ui->sensors_mutex);
 
        if (ret == FALSE)
                g_timeout_add(1000 * ui->graph_update_interval,
@@ -399,7 +400,7 @@ static void cb_activate(GApplication *application,
  */
 static void cleanup(struct ui_psensor *ui)
 {
-       pthread_mutex_lock(&ui->sensors_mutex);
+       pmutex_lock(&ui->sensors_mutex);
 
        log_debug("Cleanup...");
 
@@ -424,7 +425,7 @@ static void cleanup(struct ui_psensor *ui)
 
        ui_status_cleanup();
 
-       pthread_mutex_unlock(&ui->sensors_mutex);
+       pmutex_unlock(&ui->sensors_mutex);
 
        config_cleanup();
 
@@ -564,7 +565,7 @@ int main(int argc, char **argv)
 
        gtk_init(NULL, NULL);
 
-       pthread_mutex_init(&ui.sensors_mutex, NULL);
+       pmutex_init(&ui.sensors_mutex);
 
        ui.config = config_load();
 
index 55649b8..297d083 100644 (file)
@@ -44,6 +44,7 @@
 
 #include <plog.h>
 #include "psensor_json.h"
+#include <pmutex.h>
 #include "url.h"
 #include "server.h"
 #include "slog.h"
@@ -74,7 +75,7 @@ static struct option long_options[] = {
 
 static struct server_data server_data;
 
-static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t mutex;
 
 static int server_stop_requested;
 
@@ -298,9 +299,9 @@ static int cbk_http_request(void *cls,
 
        nurl = url_normalize(url);
 
-       pthread_mutex_lock(&mutex);
+       pmutex_lock(&mutex);
        response = create_response(nurl, method, &resp_code);
-       pthread_mutex_unlock(&mutex);
+       pmutex_unlock(&mutex);
 
        ret = MHD_queue_response(connection, resp_code, response);
        MHD_destroy_response(response);
@@ -388,6 +389,8 @@ int main(int argc, char *argv[])
        if (!log_file)
                log_file = strdup(DEFAULT_LOG_FILE);
 
+       pmutex_init(&mutex);
+
        log_open(log_file);
 
        psensor_init();
@@ -426,7 +429,7 @@ int main(int argc, char *argv[])
        }
 
        while (!server_stop_requested) {
-               pthread_mutex_lock(&mutex);
+               pmutex_lock(&mutex);
 
 #ifdef HAVE_GTOP
                sysinfo_update(&server_data.psysinfo);
@@ -436,7 +439,7 @@ int main(int argc, char *argv[])
 
                psensor_log_measures(server_data.sensors);
 
-               pthread_mutex_unlock(&mutex);
+               pmutex_unlock(&mutex);
                sleep(5);
        }