comparison src/org/tmatesoft/hg/repo/HgRepositoryLock.java @ 505:3cd3c3d37432

Use checked exception to indicate fs lock entanglements
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 30 Nov 2012 22:08:11 +0100
parents 9c0138cda59a
children c56edf42be64 2f1cd1c26de5
comparison
equal deleted inserted replaced
504:bf352ce2b97f 505:3cd3c3d37432
23 import java.lang.management.ManagementFactory; 23 import java.lang.management.ManagementFactory;
24 import java.net.InetAddress; 24 import java.net.InetAddress;
25 import java.nio.ByteBuffer; 25 import java.nio.ByteBuffer;
26 import java.nio.channels.FileChannel; 26 import java.nio.channels.FileChannel;
27 27
28 import org.tmatesoft.hg.core.HgRepositoryLockException;
28 import org.tmatesoft.hg.internal.Experimental; 29 import org.tmatesoft.hg.internal.Experimental;
29 import org.tmatesoft.hg.internal.Internals; 30 import org.tmatesoft.hg.internal.Internals;
30 31
31 /** 32 /**
32 * NOT SAFE FOR MULTITHREAD USE! 33 * NOT SAFE FOR MULTITHREAD USE!
101 * before throwing {@link HgInvalidStateException} in case lock is not available 102 * before throwing {@link HgInvalidStateException} in case lock is not available
102 * immediately. 103 * immediately.
103 * 104 *
104 * <p>Multiple calls are possible, but corresponding number of {@link #release()} 105 * <p>Multiple calls are possible, but corresponding number of {@link #release()}
105 * calls shall be made. 106 * calls shall be made.
106 */ 107 * @throws HgRepositoryLockException if failed to grab a lock
107 public void acquire() { 108 */
109 public void acquire() throws HgRepositoryLockException {
108 if (use > 0) { 110 if (use > 0) {
109 use++; 111 use++;
110 return; 112 return;
111 } 113 }
112 StringBuilder lockDescription = new StringBuilder(); 114 StringBuilder lockDescription = new StringBuilder();
133 } 135 }
134 } 136 }
135 137
136 } while (stopTime == -1/*no timeout*/ || System.currentTimeMillis() <= stopTime); 138 } while (stopTime == -1/*no timeout*/ || System.currentTimeMillis() <= stopTime);
137 String msg = String.format("Failed to aquire lock, waited for %d seconds, present owner: '%s'", timeoutSeconds, readLockInfo()); 139 String msg = String.format("Failed to aquire lock, waited for %d seconds, present owner: '%s'", timeoutSeconds, readLockInfo());
138 throw new HgInvalidStateException(msg); 140 throw new HgRepositoryLockException(msg);
139 } 141 }
140 142
141 /** 143 /**
142 * Release lock we own 144 * Release lock we own
143 */ 145 * @throws HgRepositoryLockException if there's no evidence we do own a lock
144 public void release() { 146 */
147 public void release() throws HgRepositoryLockException {
145 if (use == 0) { 148 if (use == 0) {
146 throw new HgInvalidStateException("Lock is not held!"); 149 throw new HgRepositoryLockException("Lock is not held!");
147 } 150 }
148 use--; 151 use--;
149 if (use > 0) { 152 if (use > 0) {
150 return; 153 return;
151 } 154 }