runtime/PLCObject.py
changeset 1442 ad9a7853dea2
parent 1440 e8daabf2c438
child 1447 d6b878525ceb
--- 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()