Async allocation and branch cleanup

This commit is contained in:
nathan 2020-09-29 14:36:18 +02:00
parent 69681f626a
commit e5c35ea24e
2 changed files with 23 additions and 27 deletions

View File

@ -35,7 +35,6 @@ import kotlin.concurrent.write
internal class ResponseManager(private val logger: KLogger, private val actionDispatch: CoroutineScope) { internal class ResponseManager(private val logger: KLogger, private val actionDispatch: CoroutineScope) {
companion object { companion object {
val TIMEOUT_EXCEPTION = Exception() val TIMEOUT_EXCEPTION = Exception()
val ASYNC_WAITER = ResponseWaiter(RemoteObjectStorage.ASYNC_RMI) // this is never waited on, we just need this to optimize how we assigned waiters.
} }
@ -109,24 +108,20 @@ internal class ResponseManager(private val logger: KLogger, private val actionDi
* *
* We ONLY care about the ID to get the correct response info. If there is no response, the ID can be ignored. * We ONLY care about the ID to get the correct response info. If there is no response, the ID can be ignored.
*/ */
internal suspend fun prep(isAsync: Boolean): ResponseWaiter { internal suspend fun prep(): ResponseWaiter {
return if (isAsync) { val responseRmi = waiterCache.receive()
ASYNC_WAITER rmiWaitersInUse.getAndIncrement()
} else { logger.trace { "RMI count: ${rmiWaitersInUse.value}" }
val responseRmi = waiterCache.receive()
rmiWaitersInUse.getAndIncrement()
logger.trace { "RMI count: ${rmiWaitersInUse.value}" }
// this will replace the waiter if it was cancelled (waiters are not valid if cancelled) // this will replace the waiter if it was cancelled (waiters are not valid if cancelled)
responseRmi.prep() responseRmi.prep()
pendingLock.write { pendingLock.write {
// this just does a .toUShort().toInt() conversion. This is cleaner than doing it manually // this just does a .toUShort().toInt() conversion. This is cleaner than doing it manually
pending[RmiUtils.unpackUnsignedRight(responseRmi.id)] = responseRmi pending[RmiUtils.unpackUnsignedRight(responseRmi.id)] = responseRmi
}
responseRmi
} }
return responseRmi
} }
/** /**

View File

@ -99,22 +99,23 @@ internal class RmiClient(val isGlobal: Boolean,
// 'timeout > 0' -> WAIT w/ TIMEOUT // 'timeout > 0' -> WAIT w/ TIMEOUT
// 'timeout == 0' -> WAIT FOREVER // 'timeout == 0' -> WAIT FOREVER
// If we are async, we ignore the response....
// The response, even if there is NOT one (ie: not void) will always return a thing (so our code excution is in lockstep
val rmiWaiter = responseManager.prep(isAsync)
invokeMethod.isGlobal = isGlobal invokeMethod.isGlobal = isGlobal
invokeMethod.packedId = RmiUtils.packShorts(rmiObjectId, rmiWaiter.id)
if (isAsync) {
// If we are async, we ignore the response....
connection.send(invokeMethod) invokeMethod.packedId = RmiUtils.packShorts(rmiObjectId, RemoteObjectStorage.ASYNC_RMI)
// if we are async, then this will immediately return connection.send(invokeMethod)
return if (isAsync) { return null
null
} else { } else {
responseManager.waitForReply(rmiWaiter, timeoutMillis) // The response, even if there is NOT one (ie: not void) will always return a thing (so our code execution is in lockstep
val rmiWaiter = responseManager.prep()
invokeMethod.packedId = RmiUtils.packShorts(rmiObjectId, rmiWaiter.id)
connection.send(invokeMethod)
return responseManager.waitForReply(rmiWaiter, timeoutMillis)
} }
} }