#! python: #w#VIC vic_mutex.py module # # Copyright 2002, 2003 by Timothy Rue <3seas@threeseas.net> # # VIC vic_mutex.py module: version 0.5.1.python (BETA) # # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation version 2 # of the License. http://www.gnu.org/copyleft/gpl.html # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Or access http://www.gnu.org/copyleft/gpl.html ########################################################################## #August 29, 2003 made file IQ parseable 0.5.1 #July 24, 2003 initial release 0.5 # ########################################################################## #! /usr/bin/python ### ### Cross platform Mutex class. ### ### Implements an OS level mutually-exclusive lock on systems that ### support it, and a dodgey probably-work version if not. ### ### Functions: ### VicMutex.lock() -- to to acheive mutex ### VicMutex.release() -- if mutex is acheived, deactivate the lock ### VicMutex.isLocked() -- what is the current status ### #s#system modules import time import os import os.path #w#class VicMutex class VicMutex: "VIC Mutex:" #s#def __init__ def __init__(self): self.fake_mutexes = 0 try: import mutex self.the_mutex = mutex.mutex() self.time_delay = 0.05 except: self.fake_mutexes = 1 self.the_mutex_dirname = (os.path).abspath(os.tmpnam()) self.have_lock = 0 self.time_delay = 0.5 #s#def __del__ def __del__(self): if (self.fake_mutexes == 1): try: os.rmdir(self.the_mutex_dirname) except: pass #s#def lock def lock(self): if (self.fake_mutexes): ### The mutex principle here is that only one process can successsfully ### make a single directory at once. So if two processes try to both ### create the same directory at one, only one will succeed. ### Use the act of successful directory creation as the deceiding factor. while (1): if (os.path.isdir(self.the_mutex_dirname)): self.wait() # someone else has the mutex else: try: ### if succeeds in making the dir, then we have lock os.mkdir(self.the_mutex_dirname) ### os.mkdir will except if the dir already exists self.have_lock = 1 break; except: pass else: while (not self.the_mutex.testandset()): self.wait() #s#def release def release(self): if (self.fake_mutexes): if (self.have_lock): self.have_lock = 0 os.rmdir(self.the_mutex_dirname) else: self.the_mutex.unlock() #s#def isLocked def isLocked(self): if (self.fake_mutexes): return (self.have_lock == 1) else: return self.the_mutex.test() #s#def wait ### Do not call this function def wait(self): #print "mutex sleeping..." time.sleep(self.time_delay)