mercurial/lock.py
changeset 4947 3e25a6eb5c9a
parent 3877 abaee83ce0a6
child 4959 8933b8ea871a
equal deleted inserted replaced
4946:e8f4e40f285a 4947:3e25a6eb5c9a
    27     # are atomic even over nfs.
    27     # are atomic even over nfs.
    28 
    28 
    29     # old-style lock: symlink to pid
    29     # old-style lock: symlink to pid
    30     # new-style lock: symlink to hostname:pid
    30     # new-style lock: symlink to hostname:pid
    31 
    31 
       
    32     _host = None
       
    33 
    32     def __init__(self, file, timeout=-1, releasefn=None, desc=None):
    34     def __init__(self, file, timeout=-1, releasefn=None, desc=None):
    33         self.f = file
    35         self.f = file
    34         self.held = 0
    36         self.held = 0
    35         self.timeout = timeout
    37         self.timeout = timeout
    36         self.releasefn = releasefn
    38         self.releasefn = releasefn
    37         self.id = None
       
    38         self.host = None
       
    39         self.pid = None
       
    40         self.desc = desc
    39         self.desc = desc
    41         self.lock()
    40         self.lock()
    42 
    41 
    43     def __del__(self):
    42     def __del__(self):
    44         self.release()
    43         self.release()
    57                     continue
    56                     continue
    58                 raise LockHeld(errno.ETIMEDOUT, inst.filename, self.desc,
    57                 raise LockHeld(errno.ETIMEDOUT, inst.filename, self.desc,
    59                                inst.locker)
    58                                inst.locker)
    60 
    59 
    61     def trylock(self):
    60     def trylock(self):
    62         if self.id is None:
    61         if lock._host is None:
    63             self.host = socket.gethostname()
    62             lock._host = socket.gethostname()
    64             self.pid = os.getpid()
    63         lockname = lock._host + ':' + str(os.getpid())
    65             self.id = '%s:%s' % (self.host, self.pid)
       
    66         while not self.held:
    64         while not self.held:
    67             try:
    65             try:
    68                 util.makelock(self.id, self.f)
    66                 util.makelock(lockname, self.f)
    69                 self.held = 1
    67                 self.held = 1
    70             except (OSError, IOError), why:
    68             except (OSError, IOError), why:
    71                 if why.errno == errno.EEXIST:
    69                 if why.errno == errno.EEXIST:
    72                     locker = self.testlock()
    70                     locker = self.testlock()
    73                     if locker is not None:
    71                     if locker is not None:
    91         locker = util.readlock(self.f)
    89         locker = util.readlock(self.f)
    92         try:
    90         try:
    93             host, pid = locker.split(":", 1)
    91             host, pid = locker.split(":", 1)
    94         except ValueError:
    92         except ValueError:
    95             return locker
    93             return locker
    96         if host != self.host:
    94         if host != lock._host:
    97             return locker
    95             return locker
    98         try:
    96         try:
    99             pid = int(pid)
    97             pid = int(pid)
   100         except:
    98         except:
   101             return locker
    99             return locker