add a timeout when a lock is held (default 1024 sec)
authorBenoit Boissinot <benoit.boissinot@ens-lyon.org>
Tue, 21 Feb 2006 23:21:15 +0100
changeset 1787 e431344e604c
parent 1782 b9671b41e360
child 1788 750b9cd83965
add a timeout when a lock is held (default 1024 sec) - change the wait keyword from lock.lock to timeout, a negative timeout of means "wait forever" - refactor the two lock functions from localrepo.py - make them use the timeout (default 1024, can be changed with ui.timeout in the config file - update the doc
doc/hgrc.5.txt
mercurial/localrepo.py
mercurial/lock.py
--- a/doc/hgrc.5.txt	Tue Feb 21 16:46:38 2006 +0100
+++ b/doc/hgrc.5.txt	Tue Feb 21 23:21:15 2006 +0100
@@ -247,6 +247,9 @@
     remote command to use for clone/push/pull operations. Default is 'hg'.
   ssh;;
     command to use for SSH connections. Default is 'ssh'.
+  timeout;;
+    The timeout used when a lock is held (in seconds), a negative value
+    means no timeout. Default is 1024.
   username;;
     The committer of a changeset created when running "commit".
     Typically a person's name and email address, e.g. "Fred Widget
--- a/mercurial/localrepo.py	Tue Feb 21 16:46:38 2006 +0100
+++ b/mercurial/localrepo.py	Tue Feb 21 23:21:15 2006 +0100
@@ -261,7 +261,14 @@
             if not wait:
                 raise inst
             self.ui.warn(_("waiting for lock held by %s\n") % inst.args[0])
-            l = lock.lock(self.join(lockname), wait, releasefn)
+            try:
+                # default to 1024 seconds timeout
+                l = lock.lock(self.join(lockname),
+                              int(self.ui.config("ui", "timeout") or 1024),
+                              releasefn)
+            except lock.LockHeld, inst:
+                raise util.Abort(_("timeout while waiting for "
+                                   "lock held by %s") % inst.args[0])
         if acquirefn:
             acquirefn()
         return l
--- a/mercurial/lock.py	Tue Feb 21 16:46:38 2006 +0100
+++ b/mercurial/lock.py	Tue Feb 21 23:21:15 2006 +0100
@@ -16,10 +16,10 @@
     pass
 
 class lock(object):
-    def __init__(self, file, wait=1, releasefn=None):
+    def __init__(self, file, timeout=-1, releasefn=None):
         self.f = file
         self.held = 0
-        self.wait = wait
+        self.timeout = timeout
         self.releasefn = releasefn
         self.lock()
 
@@ -27,13 +27,16 @@
         self.release()
 
     def lock(self):
+        timeout = self.timeout
         while 1:
             try:
                 self.trylock()
                 return 1
             except LockHeld, inst:
-                if self.wait:
+                if timeout != 0:
                     time.sleep(1)
+                    if timeout > 0:
+                        timeout -= 1
                     continue
                 raise inst