[vlma-devel] commit: Thread safety. (Adrien Grand )

git version control git at videolan.org
Thu Feb 12 03:43:55 CET 2009


vlma | branch: master | Adrien Grand <jpountz at videolan.org> | Thu Feb 12 03:41:27 2009 +0100| [02d6b5d11f7e87a41018be105062bda4444de618] | committer: Adrien Grand 

Thread safety.

> http://git.videolan.org/gitweb.cgi/vlma.git/?a=commit;h=02d6b5d11f7e87a41018be105062bda4444de618
---

 vlma-watchdog/src/vlc.py |   50 +++++++++++++++++++++++++++++++++------------
 1 files changed, 36 insertions(+), 14 deletions(-)

diff --git a/vlma-watchdog/src/vlc.py b/vlma-watchdog/src/vlc.py
index 160fbf2..2e8c0b5 100644
--- a/vlma-watchdog/src/vlc.py
+++ b/vlma-watchdog/src/vlc.py
@@ -19,44 +19,66 @@ class VLCRunner(threading.Thread):
     self.logger = logging.getLogger("VLC Runner")
     threading.Thread.__init__(self)
     self.setName("VLC Runner")
+    self.__lock = threading.RLock()
     self.__pid = 0
     self.__shouldRun = True
-    self.startTime = 0
+    self.__startTime = 0
+    self.__version = None
 
   def run(self):
-    while (self.__shouldRun):
+    while True:
+      self.__lock.acquire()
+      if not self.__shouldRun:
+        break
       self.version = None
       self.logger.info("Running %s %s", conf.VLC_EXE, conf.VLC_ARGS)
       args = conf.VLC_ARGS.split()
       args.insert(0, conf.VLC_EXE)
-      self.startTime = long(math.floor(1000L * time.time()))
+      self.__startTime = long(math.floor(1000L * time.time()))
       self.__pid = os.spawnvp(os.P_NOWAIT, conf.VLC_EXE, args)
+      self.__lock.release()
       exit_status = os.waitpid(self.__pid, 0)[1]
+      self.__lock.acquire()
       self.startTime = 0
+      self.__lock.release()
       self.logger.info("VLC exited with return code %d", exit_status)
     self.logger.info("VLC has been stopped")
 
   def getVersion(self):
-    if(self.version is None):
+    self.__lock.acquire()
+    version = self.__version
+    self.__lock.release()
+    if version is None:
       out = os.popen2("%s --version" %conf.VLC_EXE)[1]
-      self.version = out.readline()
-    return self.version
+      version = out.readline()
+      self.__lock.acquire()
+      self.__version = version
+      self.__lock.release()
+    return version
 
   def getPid(self):
-    return self.__pid
+    self.__lock.acquire()
+    pid = self.__pid
+    self.__lock.release()
+    return pid
 
   def getUptime(self):
-    startTime = self.startTime
-    if(startTime == 0):
+    self.__lock.acquire()
+    startTime = self.__startTime
+    self.__lock.release()
+    if startTime == 0:
       return 0L
     else:
       return long(math.floor(1000L * time.time())) - startTime
 
   def stop(self):
+    self.__lock.acquire()
     self.__shouldRun = False
-    if (self.__pid > 0):
+    pid = self.__pid
+    self.__lock.release()
+    if (pid > 0):
       try:
-        os.kill(self.__pid, 2)
+        os.kill(pid, 9)
       except OSError:
         #  Process already stopped
         pass
@@ -67,10 +89,10 @@ class VLC:
 
   def __init__(self):
     self.logger = logging.getLogger("VLC")
-    self.runner = VLCRunner()
+    self.runner = None
 
   def start(self):
-    if self.runner.isAlive():
+    if not self.runner is None and self.runner.isAlive():
       logger.error("Cannot start VLC, another instance is still running.")
       logger.info("Please stop VLC first")
     else:
@@ -101,7 +123,7 @@ class VLC:
         beginningOfFileReached = True
         f.seek(0)
       logs = f.read().split("\n")[-lines:]
-      if(len(logs) < lines and not beginningOfFileReached):
+      if len(logs) < lines and not beginningOfFileReached:
         avgcharsperline *= 1.3
       else:
         break



More information about the vlma-devel mailing list