Skip to content

Commit

Permalink
Keep ChangeLock inteface and its only implementation in one file
Browse files Browse the repository at this point in the history
  • Loading branch information
grzesiek2010 committed Oct 26, 2024
1 parent c1ea862 commit 6cceed3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 36 deletions.
33 changes: 33 additions & 0 deletions shared/src/main/java/org/odk/collect/shared/locks/ChangeLock.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,36 @@ interface ChangeLock {

fun unlock()
}

class ThreadSafeBooleanChangeLock : ChangeLock {
private var locked = false

override fun <T> withLock(function: Function<Boolean, T>): T {
val acquired = tryLock()

return try {
function.apply(acquired)
} finally {
if (acquired) {
unlock()
}
}
}

override fun tryLock(): Boolean {
return synchronized(this) {
if (locked) {
false
} else {
locked = true
true
}
}
}

override fun unlock() {
synchronized(this) {
locked = false
}
}
}

This file was deleted.

0 comments on commit 6cceed3

Please sign in to comment.