diff -r bfab45f4d5cd -r a28ef4d9afa4 thermferm/thermferm.c --- a/thermferm/thermferm.c Wed Apr 03 19:33:38 2024 +0200 +++ b/thermferm/thermferm.c Fri Apr 05 16:19:39 2024 +0200 @@ -49,15 +49,29 @@ extern int lcdHandle; extern int slcdHandle; extern int my_devices_state; +extern int my_devices_shutdown; extern int my_panel_state; +extern int my_panel_shutdown; extern int my_server_state; +extern int my_server_shutdown; extern int my_simulator_state; +#ifdef USE_SIMULATOR +extern int my_simulator_shutdown; +#endif extern int my_one_wire_state; +extern int my_one_wire_shutdown; 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_t my_one_wire_thread; +pthread_t my_devices_thread; +pthread_t my_panel_thread; +pthread_t my_server_thread; +#ifdef USE_SIMULATOR +pthread_t my_simulator_thread; +#endif + pthread_mutex_t mutexes[5]; extern const char UNITMODE[5][8]; @@ -1040,13 +1054,13 @@ /* * First scan the one-wire bus */ - rc = pthread_create(&threads[t], NULL, my_one_wire_loop, (void *)t ); + rc = pthread_create(&my_one_wire_thread, NULL, my_one_wire_loop, (void *)t ); if (rc) { fprintf(stderr, "my_one_wire_loop thread didn't start rc=%d\n", rc); syslog(LOG_NOTICE, "my_one_wire_loop thread didn't start rc=%d", rc); } else { t++; - mDelay(250); /* Wait a while to detect the devices */ + mDelay(2500); /* Wait a while to detect the devices */ } if ((rc = devices_detect())) { @@ -1054,7 +1068,7 @@ wrconfig(); } - rc = pthread_create(&threads[t], NULL, my_devices_loop, (void *)t ); + rc = pthread_create(&my_devices_thread, NULL, my_devices_loop, (void *)t ); if (rc) { fprintf(stderr, "my_devices_loop thread didn't start rc=%d\n", rc); syslog(LOG_NOTICE, "my_devices_loop thread didn't start rc=%d", rc); @@ -1062,7 +1076,7 @@ t++; } - rc = pthread_create(&threads[t], NULL, my_server_loop, (void *)t ); + rc = pthread_create(&my_server_thread, NULL, my_server_loop, (void *)t ); if (rc) { fprintf(stderr, "my_server_loop thread didn't start rc=%d\n", rc); syslog(LOG_NOTICE, "my_server_loop thread didn't start rc=%d", rc); @@ -1070,7 +1084,7 @@ t++; } - rc = pthread_create(&threads[t], NULL, my_panel_loop, (void *)t ); + rc = pthread_create(&my_panel_thread, NULL, my_panel_loop, (void *)t ); if (rc) { fprintf(stderr, "my_panel_loop thread didn't start rc=%d\n", rc); syslog(LOG_NOTICE, "my_panel_loop thread didn't start rc=%d", rc); @@ -1079,7 +1093,7 @@ } #ifdef USE_SIMULATOR - rc = pthread_create(&threads[t], NULL, my_simulator_loop, (void *)t ); + rc = pthread_create(&my_simulator_thread, NULL, my_simulator_loop, (void *)t ); if (rc) { fprintf(stderr, "my_simulator_loop thread didn't start rc=%d\n", rc); syslog(LOG_NOTICE, "my_simulator_loop thread didn't start rc=%d", rc); @@ -1925,13 +1939,38 @@ } syslog(LOG_NOTICE, "Unit `%s' stopped in mode %s", unit->alias, UNITMODE[unit->mode]); } - syslog(LOG_NOTICE, "Out of loop"); + syslog(LOG_NOTICE, "Out of loop, stopping threads.."); + + /* + * Stop threads + */ +#ifdef USE_SIMULATOR + my_simulator_shutdown = 1; + while (my_simulator_state) { mDelay(50); }; +#endif + my_panel_shutdown = 1; + while (my_panel_state) { mDelay(50); }; /* - * Note that we don't care if the command server is stopped, this one - * does almost certain keep running but that doesn't harm. + * Cancel command and shutdown via variable, one of them + * will stop this thread. Includes a failsafe. */ - while ((my_devices_state + my_panel_state + my_simulator_state + my_one_wire_state) > 0) { sleep(1); }; + my_server_shutdown = 1; + rc = pthread_cancel(my_server_thread); + rc = 0; + while (my_server_state) { + mDelay(50); + if (rc++ > 20) { + syslog(LOG_NOTICE, "Cannot terminate my_server_loop()"); + break; + } + } + + my_devices_shutdown = 1; + while (my_devices_state) { mDelay(50); }; + my_one_wire_shutdown = 1; + while (my_one_wire_state) { mDelay(50); }; + mqtt_disconnect(); stopLCD();