Win32 rutime: fix PLC Stop freezing
authorEdouard Tisserant <edouard.tisserant@gmail.com>
Fri, 29 Sep 2023 13:46:36 +0200
changeset 3850 722846bd6680
parent 3849 c3f4e114af38
child 3851 4e1906d119d5
Win32 rutime: fix PLC Stop freezing
targets/Win32/plc_Win32_main.c
--- a/targets/Win32/plc_Win32_main.c	Fri Sep 29 13:45:50 2023 +0200
+++ b/targets/Win32/plc_Win32_main.c	Fri Sep 29 13:46:36 2023 +0200
@@ -35,12 +35,6 @@
 	(*CURRENT_TIME).tv_nsec = timetmp.millitm * 1000000;
 }
 
-void PLC_timer_notify()
-{
-    PLC_GetTime(&__CURRENT_TIME);
-    __run();
-}
-
 HANDLE PLC_timer = NULL;
 void PLC_SetTimer(unsigned long long next, unsigned long long period)
 {
@@ -65,9 +59,12 @@
 {
     PLC_shutdown = 0;
     while(!PLC_shutdown) {
-        if (WaitForSingleObject(PLC_timer, INFINITE) != WAIT_OBJECT_0)
+        if (WaitForSingleObject(PLC_timer, INFINITE) != WAIT_OBJECT_0){
             PLC_shutdown = 1;
-        PLC_timer_notify();
+            break;
+        }
+        PLC_GetTime(&__CURRENT_TIME);
+        __run();
     }
 }
 
@@ -172,9 +169,16 @@
 
 int stopPLC()
 {
+ 	
+    PLC_shutdown = 1;
+    // force last wakeup of PLC thread
+    SetWaitableTimer(PLC_timer, 0, 0, NULL, NULL, 0);
+    // wait end of PLC thread
+    WaitForSingleObject(PLC_thread, INFINITE);
+
+    __cleanup();
+
     CloseHandle(PLC_timer);
-    WaitForSingleObject(PLC_thread, INFINITE);
-    __cleanup();
     CloseHandle(debug_wait_sem);
     CloseHandle(debug_sem);
     CloseHandle(python_wait_sem);