Skip to content

Instantly share code, notes, and snippets.

@Randgalt
Created July 18, 2012 19:10
Show Gist options
  • Save Randgalt/3138156 to your computer and use it in GitHub Desktop.
Save Randgalt/3138156 to your computer and use it in GitHub Desktop.
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