More global device locking

Thu, 19 Mar 2015 20:32:23 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Thu, 19 Mar 2015 20:32:23 +0100
changeset 337
5d3670aafd1b
parent 336
217f330b05cb
child 338
8e29d142d67d

More global device locking

thermferm/devices.c file | annotate | diff | comparison | revisions
--- a/thermferm/devices.c	Wed Mar 18 14:45:43 2015 +0100
+++ b/thermferm/devices.c	Thu Mar 19 20:32:23 2015 +0100
@@ -178,6 +178,10 @@
 
     now = time(NULL);
 
+#ifdef HAVE_WIRINGPI_H
+    piLock(LOCK_DEVICES);
+#endif
+
     for (device = Config.devices; device; device = device->next) {
 	if (! strcmp(uuid, device->uuid)) {
 	    /*
@@ -185,14 +189,8 @@
 	     * every 2 minutes because commands can have temporary
 	     * disconnects, or have radio problems.
 	     */
-#ifdef HAVE_WIRINGPI_H
-	    piLock(LOCK_DEVICES);
-#endif
 	    my_timestamp = device->timestamp;
 	    my_value = device->value;
-#ifdef HAVE_WIRINGPI_H
-	    piUnlock(LOCK_DEVICES);
-#endif
 	    if ((value != my_value) || (((int)now - (int)my_timestamp) > 120)) {
 
 #ifdef HAVE_WIRINGPI_H
@@ -202,13 +200,14 @@
 		    for (i = 0; i < strlen(buf); i++)
 			if (buf[i] == '-')
 			    buf[i] = ',';
+		    piUnlock(LOCK_DEVICES);
 	    	    enableTransmit(device->gpiopin);	    
 		    rc = toggleSwitch(buf);
 		    disableTransmit();
+		    piLock(LOCK_DEVICES);
 		    syslog(LOG_NOTICE, "RC433 command %s rc=%d", buf, rc);
 		    if (debug)
 			fprintf(stdout, "RC433 command %s rc=%d\n", buf, rc);
-		    piLock(LOCK_DEVICES);
                     device->value = value;
 		    device->timestamp = time(NULL);
 		    piUnlock(LOCK_DEVICES);
@@ -228,10 +227,16 @@
 		}
 #endif
 	    } else {
+#ifdef HAVE_WIRINGPI_H
+		piUnlock(LOCK_DEVICES);
+#endif
 		return 0;
 	    }
 	}
     }
+#ifdef HAVE_WIRINGPI_H
+    piUnlock(LOCK_DEVICES);
+#endif
 
     return 0;
 }	   

mercurial