Fixed race condition preventing to stop PLC through WAMP
authorEdouard Tisserant
Mon, 09 Feb 2015 00:45:03 +0100
changeset 1442 ad9a7853dea2
parent 1441 826730e60407
child 1443 ff8a22d45c44
Fixed race condition preventing to stop PLC through WAMP
Beremiz.py
runtime/PLCObject.py
--- a/Beremiz.py	Sun Feb 08 22:39:17 2015 +0100
+++ b/Beremiz.py	Mon Feb 09 00:45:03 2015 +0100
@@ -641,7 +641,6 @@
     ToDoBeforeQuit = []
     def AddToDoBeforeQuit(self, Thing):
         self.ToDoBeforeQuit.append(Thing)
-        print self.ToDoBeforeQuit
 
     def OnCloseFrame(self, event):
         for evt_type in [wx.EVT_SET_FOCUS,
--- a/runtime/PLCObject.py	Sun Feb 08 22:39:17 2015 +0100
+++ b/runtime/PLCObject.py	Mon Feb 09 00:45:03 2015 +0100
@@ -156,17 +156,16 @@
             else:
                 # If python confnode is not enabled, we reuse _PythonIterator
                 # as a call that block pythonthread until StopPLC
-                self.PythonIteratorLock = Lock()
-                self.PythonIteratorLock.acquire()
+                self.PlcStopping = Event()
                 def PythonIterator(res, blkid):
-                    self.PythonIteratorLock.acquire()
-                    self.PythonIteratorLock.release()
+                    self.PlcStopping.clear()
+                    self.PlcStopping.wait()
                     return None
                 self._PythonIterator = PythonIterator
 
                 def __StopPLC():
                     self._stopPLC_real()
-                    self.PythonIteratorLock.release()
+                    self.PlcStopping.set()
                 self._stopPLC = __StopPLC
 
 
@@ -311,10 +310,7 @@
         self.python_runtime_vars = None
 
     def PythonThreadProc(self):
-        self.PLCStatus = "Started"
-        self.StatusChange()
         self.StartSem.release()
-        self.PythonRuntimeCall("start")
         res,cmd,blkid = "None","None",ctypes.c_void_p()
         compile_cache={}
         while True:
@@ -341,9 +337,6 @@
             except Exception,e:
                 res = "#EXCEPTION : "+str(e)
                 self.LogMessage(1,('PyEval@0x%x(Code="%s") Exception "%s"')%(FBID,cmd,str(e)))
-        self.PLCStatus = "Stopped"
-        self.StatusChange()
-        self.PythonRuntimeCall("stop")
 
     def StartPLC(self):
         if self.CurrentPLCFilename is not None and self.PLCStatus == "Stopped":
@@ -351,6 +344,9 @@
             error = None
             res = self._startPLC(len(self.argv),c_argv(*self.argv))
             if res == 0:
+                self.PLCStatus = "Started"
+                self.StatusChange()
+                self.PythonRuntimeCall("start")
                 self.StartSem=Semaphore(0)
                 self.PythonThread = Thread(target=self.PythonThreadProc)
                 self.PythonThread.start()
@@ -366,6 +362,9 @@
             self.LogMessage("PLC stopped")
             self._stopPLC()
             self.PythonThread.join()
+            self.PLCStatus = "Stopped"
+            self.StatusChange()
+            self.PythonRuntimeCall("stop")
             if self.TraceThread is not None :
                 self.TraceWakeup.set()
                 self.TraceThread.join()