File tree Expand file tree Collapse file tree
onebusaway-sdk-java-core/src/main/kotlin/org/onebusaway/core Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -4,6 +4,8 @@ package org.onebusaway.core
44
55import java.util.Collections
66import java.util.SortedMap
7+ import java.util.concurrent.CompletableFuture
8+ import java.util.concurrent.locks.Lock
79import org.onebusaway.errors.OnebusawaySdkInvalidDataException
810
911@JvmSynthetic
@@ -90,3 +92,24 @@ internal fun Any?.contentToString(): String {
9092}
9193
9294internal interface Enum
95+
96+ /* *
97+ * Executes the given [action] while holding the lock, returning a [CompletableFuture] with the
98+ * result.
99+ *
100+ * @param action The asynchronous action to execute while holding the lock
101+ * @return A [CompletableFuture] that completes with the result of the action
102+ */
103+ @JvmSynthetic
104+ internal fun <T > Lock.withLockAsync (action : () -> CompletableFuture <T >): CompletableFuture <T > {
105+ lock()
106+ val future =
107+ try {
108+ action()
109+ } catch (e: Throwable ) {
110+ unlock()
111+ throw e
112+ }
113+ future.whenComplete { _, _ -> unlock() }
114+ return future
115+ }
You can’t perform that action at this time.
0 commit comments