# HG changeset patch
# User Edouard Tisserant
# Date 1423439103 -3600
# Node ID ad9a7853dea25e8188509755f10da744981a6a5f
# Parent  826730e60407930a08c7c26c072df6b1e5d84cba
Fixed race condition preventing to stop PLC through WAMP

diff -r 826730e60407 -r ad9a7853dea2 Beremiz.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,
diff -r 826730e60407 -r ad9a7853dea2 runtime/PLCObject.py
--- 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()