-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re-writing locking support to provide better safety
- Loading branch information
1 parent
2ad0eda
commit 012860e
Showing
5 changed files
with
93 additions
and
88 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package lightdb.lock | ||
|
||
import java.util.concurrent.locks.ReentrantLock | ||
|
||
class Lock[V](value: => Option[V], val lock: ReentrantLock) { | ||
private lazy val v: Option[V] = value | ||
|
||
def apply(): Option[V] = v | ||
} |
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package lightdb.lock | ||
|
||
import java.util.concurrent.ConcurrentHashMap | ||
import java.util.concurrent.locks.ReentrantLock | ||
|
||
class LockManager[K, V] { | ||
private val locks = new ConcurrentHashMap[K, Lock[V]]() | ||
|
||
def apply(key: K, value: => Option[V], establishLock: Boolean = true) | ||
(f: Option[V] => Option[V]): Option[V] = if (establishLock) { | ||
val v = acquire(key, value) | ||
try { | ||
val modified = f(v) | ||
release(key, modified) | ||
} catch { | ||
case t: Throwable => | ||
release(key, v) | ||
throw t | ||
} | ||
} else { | ||
f(value) | ||
} | ||
|
||
// Attempts to acquire a lock for a given K and V. | ||
def acquire(key: K, value: => Option[V]): Option[V] = { | ||
// Get or create the Lock object with the ReentrantLock. | ||
val lock = locks.computeIfAbsent(key, _ => new Lock(value, new ReentrantLock)) | ||
|
||
// Acquire the underlying ReentrantLock. | ||
lock.lock.lock() | ||
lock() // Return the associated value after acquiring the lock. | ||
} | ||
|
||
// Releases the lock for the given K and supplies the latest V. | ||
def release(key: K, newValue: => Option[V]): Option[V] = { | ||
val v: Option[V] = newValue | ||
locks.compute(key, (_, existingLock) => { | ||
// Update the value associated with the lock. | ||
existingLock.lock.unlock() | ||
|
||
if (!existingLock.lock.hasQueuedThreads) { | ||
// No other threads are waiting, so remove the lock entry. | ||
null | ||
} else { | ||
// Other threads are waiting, so update the value but keep the lock. | ||
new Lock(v, existingLock.lock) | ||
} | ||
}) | ||
v | ||
} | ||
} |
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