Running threads and locking doesn't depend on wiringPi anymore.

Sat, 23 Mar 2024 16:01:24 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 23 Mar 2024 16:01:24 +0100
changeset 646
e3edc783006b
parent 645
49eb753a958b
child 647
fcd85176ea2e

Running threads and locking doesn't depend on wiringPi anymore.

thermferm/devices.c file | annotate | diff | comparison | revisions
thermferm/panel.c file | annotate | diff | comparison | revisions
thermferm/server.c file | annotate | diff | comparison | revisions
thermferm/simulator.c file | annotate | diff | comparison | revisions
thermferm/thermferm.c file | annotate | diff | comparison | revisions
--- a/thermferm/devices.c	Sat Mar 23 12:18:14 2024 +0100
+++ b/thermferm/devices.c	Sat Mar 23 16:01:24 2024 +0100
@@ -27,6 +27,8 @@
 #include "xutil.h"
 
 
+int			my_devices_state = 0;
+
 extern sys_config	Config;
 extern int		my_shutdown;
 extern pthread_mutex_t  mutexes[5];
@@ -730,6 +732,7 @@
     time_t		now;
 #endif
 
+    my_devices_state = 1;
     syslog(LOG_NOTICE, "Thread my_devices_loop started");
 
 #ifdef HAVE_WIRINGPI_H
@@ -995,6 +998,7 @@
     }
 
     syslog(LOG_NOTICE, "Thread my_devices_loop stopped");
+    my_devices_state = 0;
     return 0;
 }
 
--- a/thermferm/panel.c	Sat Mar 23 12:18:14 2024 +0100
+++ b/thermferm/panel.c	Sat Mar 23 16:01:24 2024 +0100
@@ -53,6 +53,7 @@
 extern int		slcdHandle;
 extern pthread_mutex_t  mutexes[5];
 
+int			my_panel_state = 0;
 int			Key_Enter = FALSE;
 int			Key_Enter_Long = FALSE;
 int			Key_Up = FALSE;
@@ -101,15 +102,13 @@
 }
 
 
-#ifdef HAVE_WIRINGPI_H
-PI_THREAD (my_panel_loop)
-#else
 void *my_panel_loop(void *threadid)
-#endif
 {
     int		Enter = 0, Up = 0, Down = 0, Backlight = LCD_SLEEP, AnyKey = FALSE;
     time_t	Last = (time_t)0, Now;
 
+    my_panel_state = 1;
+
 #ifdef HAVE_WIRINGPI_H
     pinMode(PANEL_LED, OUTPUT);
     pinMode(PANEL_ENTER, INPUT);
@@ -232,6 +231,7 @@
     }
 
     syslog(LOG_NOTICE, "Thread my_panel_loop stopped");
+    my_panel_state = 0;
     return 0;
 }
 
--- a/thermferm/server.c	Sat Mar 23 12:18:14 2024 +0100
+++ b/thermferm/server.c	Sat Mar 23 16:01:24 2024 +0100
@@ -44,11 +44,12 @@
 extern pthread_mutex_t	mutexes[5];
 
 
-int			s;		/* connected socket			*/
-int			ls;		/* listen socket			*/
+int			my_server_state = 0;	/* Thread running state		*/
+int			s;			/* connected socket		*/
+int			ls;			/* listen socket		*/
 
-struct sockaddr_in	myaddr_in;	/* for local socket address             */
-struct sockaddr_in	peeraddr_in;	/* for peer socket address              */
+struct sockaddr_in	myaddr_in;		/* for local socket address	*/
+struct sockaddr_in	peeraddr_in;		/* for peer socket address	*/
 
 struct hostent		*hp;
 
@@ -2143,15 +2144,12 @@
 
 
 
-#ifdef HAVE_WIRINGPI_H
-PI_THREAD (my_server_loop)
-#else
 void *my_server_loop(void *threadid)
-#endif
 {
     socklen_t   addrlen;
     int         optval = 1;
 
+    my_server_state = 1;
     syslog(LOG_NOTICE, "Thread my_server_loop started");
     if (debug)
 	fprintf(stdout, "Thread my_server_loop started\n");
@@ -2166,30 +2164,35 @@
     if (ls == -1) {
 	syslog(LOG_NOTICE, "Can't create listen socket: %s", strerror(errno));
 	fprintf(stderr, "Can't create listen socket: %s\n", strerror(errno));
+	my_server_state = 0;
 	return 0;
     }
 
     if (setsockopt(ls, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval)) == -1) {
 	syslog(LOG_NOTICE, "Can't setsockopt SO_KEEPALIVE socket: %s", strerror(errno));
 	close(ls);
+	my_server_state = 0;
 	return 0;
     }
 
     if (setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) == -1) {
 	syslog(LOG_NOTICE, "Can't setsockopt SO_REUSEADDR socket: %s", strerror(errno));
 	close(ls);
+	my_server_state = 0;
 	return 0;
     }
 
     if (bind(ls, (struct sockaddr *)&myaddr_in, sizeof(struct sockaddr_in)) == -1) {
 	syslog(LOG_NOTICE, "Can't bind to listen socket: %s", strerror(errno));
 	close(ls);
+	my_server_state = 0;
 	return 0;
     }
 
     if (listen(ls, 5) == -1) {
 	syslog(LOG_NOTICE, "Can't listen on listen socket: %s", strerror(errno));
 	close(ls);
+	my_server_state = 0;
 	return 0;
     }
 
@@ -2213,6 +2216,7 @@
 	    syslog(LOG_NOTICE, "my_server_loop accept failed %s", strerror(errno));
 	    if (debug)
 		fprintf(stdout, "my_server_loop accept failed %s\n", strerror(errno));
+	    my_server_state = 0;
 	    return 0;
 	}
 
@@ -2222,6 +2226,7 @@
 	    syslog(LOG_NOTICE, "Thread my_server_loop stopped");
 	    if (debug)
 		fprintf(stdout, "Thread my_server_loop stopped\n");
+	    my_server_state = 0;
 	    return 0;
 	}
 	usleep(100000);
--- a/thermferm/simulator.c	Sat Mar 23 12:18:14 2024 +0100
+++ b/thermferm/simulator.c	Sat Mar 23 16:01:24 2024 +0100
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * Copyright (C) 2014-2019
+ * Copyright (C) 2014-2024
  *   
  * Michiel Broek <mbroek at mbse dot eu>
  *
@@ -23,22 +23,20 @@
 #include "thermferm.h"
 #include "simulator.h"
 
+int			my_simulator_state = 0;
+
 #ifdef USE_SIMULATOR
 
 extern int		my_shutdown;
 extern sys_config	Config;
 
-int SIMcooling = 0;
-int SIMheating = 0;
-int SIMfan = 0;
-int SIMlight = 0;
+int			SIMcooling = 0;
+int			SIMheating = 0;
+int			SIMfan = 0;
+int			SIMlight = 0;
 
 
-#ifdef HAVE_WIRINGPI_H
-PI_THREAD (my_simulator_loop)
-#else
 void *my_simulator_loop(void *threadid)
-#endif
 {
     simulator_list	*simulator;
     time_t		now, last = (time_t)0;
@@ -46,6 +44,7 @@
     double		k_room_air, sqm_room_air, thick_room_air, air_heat_transfer;
     double		air_change, vhc_air = 0.00121;
 
+    my_simulator_state = 1;
     syslog(LOG_NOTICE, "Thread my_simulator_loop started");
 
     for (simulator = Config.simulators; simulator; simulator = simulator->next) {
@@ -131,6 +130,7 @@
     }
 
     syslog(LOG_NOTICE, "Thread my_simulator_loop stopped");
+    my_simulator_state = 0;
     return 0;
 }
 
--- a/thermferm/thermferm.c	Sat Mar 23 12:18:14 2024 +0100
+++ b/thermferm/thermferm.c	Sat Mar 23 16:01:24 2024 +0100
@@ -46,12 +46,17 @@
 extern sys_config	Config;
 extern int		lcdHandle;
 extern int		slcdHandle;
+extern int		my_devices_state;
+extern int		my_panel_state;
+extern int		my_server_state;
+extern int		my_simulator_state;
 int			setupmenu = MENU_NONE;
 units_list		*current_unit = NULL;		/* In panel editor this points to the current unit. */
 float			temp_temp = 20.0;
 
 pthread_t		threads[5];
 pthread_mutex_t		mutexes[5];
+
 extern const char       UNITMODE[5][8];
 extern const char	PROFSTATE[4][6];
 
@@ -1900,16 +1905,14 @@
 	}
 	syslog(LOG_NOTICE, "Unit `%s' stopped in mode %s", unit->alias, UNITMODE[unit->mode]);
     }
-
-    usleep(100000);
-    mqtt_disconnect();
-
     syslog(LOG_NOTICE, "Out of loop");
 
     /*
-     * Give threads time to cleanup
+     * Note that we don't care if the command server is stopped, this one
+     * does almost certain keep running but tat doesn't harm.
      */
-    usleep(1500000);
+    while ((my_devices_state + my_panel_state + my_simulator_state) > 0) {};
+    mqtt_disconnect();
 
     stopLCD();
     if (sock != -1) {

mercurial