diff --git a/mainLoop.py b/mainLoop.py index 5f90a21..7f66617 100644 --- a/mainLoop.py +++ b/mainLoop.py @@ -4,10 +4,12 @@ from Map import Map from CarController import CarController from PySide6.QtWidgets import QFileDialog, QToolBox -from PySide6.QtCore import QElapsedTimer, QTimer, QElapsedTimer, QObject, Signal, Slot +from PySide6.QtCore import QElapsedTimer, QTimer, QElapsedTimer, QObject, Signal, Slot, QThread, QMutex class mainLoop(QObject): updateFPS = Signal(int) + mutex = QMutex() + def __init__(self, parent): super().__init__() self.parent = parent @@ -30,16 +32,26 @@ class mainLoop(QObject): self.fpsTimer = QElapsedTimer() self.updateFPS.connect(self.parent.updatePhysicsFps) + def threadSafe(func): + def inner(*args, **kwargs): + args[0].mutex.lock() + func(*args, **kwargs) + args[0].mutex.unlock() + return inner + def start(self): self.fpsTimer.start() + self.stopSignal.connect(self.timer.stop) + @threadSafe def update(self): try: self.controller.update() except: (type, value, traceback) = sys.exc_info() print(type, value, traceback) - + + @threadSafe def openNetwork(self, filename): if filename == '': return @@ -48,6 +60,7 @@ class mainLoop(QObject): self.controller.prepareRoute() + @threadSafe def openVehicles(self, filename): if filename == '': return @@ -55,6 +68,7 @@ class mainLoop(QObject): if self.map.isLoaded(): self.controller.prepareRoute() + @threadSafe def quickLoad(self): self.map.fromPath("test2.net.xml") self.painter.generateTransform()