Created
July 18, 2012 19:10
-
-
Save Randgalt/3138156 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class InterProcessSemaphoreMutex implements InterProcessLock | |
{ | |
private InterProcessSemaphore semaphore; | |
private volatile Lease lease; | |
public InterProcessSemaphoreMutex(InterProcessSemaphore semaphore) | |
{ | |
this.semaphore = semaphore; | |
} | |
@Override | |
public void acquire() throws Exception | |
{ | |
Preconditions.checkState(lease == null, "Already acquired"); | |
lease = semaphore.acquire(); | |
} | |
@Override | |
public boolean acquire(long time, TimeUnit unit) throws Exception | |
{ | |
Preconditions.checkState(lease == null, "Already acquired"); | |
lease = semaphore.acquire(time, unit); | |
return (lease != null); | |
} | |
@Override | |
public void release() throws Exception | |
{ | |
Preconditions.checkState(lease != null, "Not acquired"); | |
try | |
{ | |
lease.close(); | |
} | |
finally | |
{ | |
lease = null; | |
} | |
} | |
@Override | |
public boolean isAcquiredInThisProcess() | |
{ | |
return (lease != null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment