Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented not thread bound lock manager and used for pessimistic re…
…cord locking
- Loading branch information
Showing
8 changed files
with
311 additions
and
30 deletions.
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
core/src/main/java/com/orientechnologies/common/concur/lock/ONotThreadRWLockManager.java
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,105 @@ | |||
package com.orientechnologies.common.concur.lock; | |||
|
|||
import com.orientechnologies.common.exception.OException; | |||
|
|||
import java.util.HashMap; | |||
import java.util.Map; | |||
import java.util.concurrent.TimeUnit; | |||
import java.util.concurrent.locks.Condition; | |||
import java.util.concurrent.locks.Lock; | |||
import java.util.concurrent.locks.ReentrantLock; | |||
|
|||
public class ONotThreadRWLockManager<T> implements OSimpleRWLockManager<T> { | |||
|
|||
private class LockGuard { | |||
private int count; | |||
private Condition condition; | |||
private boolean shared; | |||
|
|||
public LockGuard(int count, Condition condition, boolean shared) { | |||
this.count = count; | |||
this.condition = condition; | |||
this.shared = shared; | |||
} | |||
} | |||
|
|||
private final Lock lock = new ReentrantLock(); | |||
private final Map<T, LockGuard> map = new HashMap<>(); | |||
|
|||
public ONotThreadRWLockManager() { | |||
} | |||
|
|||
public void lock(T key, boolean shared, long timeout) { | |||
|
|||
lock.lock(); | |||
try { | |||
try { | |||
|
|||
LockGuard c; | |||
do { | |||
c = map.get(key); | |||
if (c != null) { | |||
if (c.shared && shared) { | |||
c.count++; | |||
return; | |||
} else { | |||
if (timeout == 0) { | |||
c.condition.await(); | |||
} else { | |||
if (!c.condition.await(timeout, TimeUnit.MILLISECONDS)) { | |||
throw new OLockException(String.format("Time out acquire lock for resource: '%s' ", key)); | |||
} | |||
} | |||
} | |||
} | |||
} while (c != null); | |||
c = new LockGuard(1, lock.newCondition(), shared); | |||
map.put(key, c); | |||
} catch (InterruptedException e) { | |||
throw OException.wrapException(new OInterruptedException("Interrupted Lock"), e); | |||
} | |||
} finally { | |||
lock.unlock(); | |||
} | |||
|
|||
} | |||
|
|||
public void unlock(T key, boolean shared) { | |||
lock.lock(); | |||
try { | |||
LockGuard c = map.get(key); | |||
assert c != null; | |||
if (c.shared != shared) { | |||
throw new OLockException("Impossible to release a not acquired lock"); | |||
} | |||
c.count--; | |||
if (c.count == 0) { | |||
map.remove(key); | |||
c.condition.signalAll(); | |||
} | |||
} finally { | |||
lock.unlock(); | |||
} | |||
} | |||
|
|||
@Override | |||
public void acquireReadLock(T key, long timeout) { | |||
lock(key, true, timeout); | |||
} | |||
|
|||
@Override | |||
public void acquireWriteLock(T key, long timeout) { | |||
lock(key, false, timeout); | |||
} | |||
|
|||
@Override | |||
public void releaseReadLock(T key) { | |||
unlock(key, true); | |||
} | |||
|
|||
@Override | |||
public void releaseWriteLock(T key) { | |||
unlock(key, false); | |||
} | |||
|
|||
} |
13 changes: 13 additions & 0 deletions
13
core/src/main/java/com/orientechnologies/common/concur/lock/OSimpleRWLockManager.java
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,13 @@ | |||
package com.orientechnologies.common.concur.lock; | |||
|
|||
public interface OSimpleRWLockManager<T> { | |||
|
|||
void acquireReadLock(T key, long timeout); | |||
|
|||
void acquireWriteLock(T key, long timeout); | |||
|
|||
void releaseReadLock(T key); | |||
|
|||
void releaseWriteLock(T key); | |||
|
|||
} |
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
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
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
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
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
Oops, something went wrong.