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) {
companion object {
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.
*/
internal suspend fun prep(isAsync: Boolean): ResponseWaiter {
return if (isAsync) {
ASYNC_WAITER
} else {
val responseRmi = waiterCache.receive()
rmiWaitersInUse.getAndIncrement()
logger.trace { "RMI count: ${rmiWaitersInUse.value}" }
internal suspend fun prep(): ResponseWaiter {
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)
responseRmi.prep()
// this will replace the waiter if it was cancelled (waiters are not valid if cancelled)
responseRmi.prep()
pendingLock.write {
// this just does a .toUShort().toInt() conversion. This is cleaner than doing it manually
pending[RmiUtils.unpackUnsignedRight(responseRmi.id)] = responseRmi
}
responseRmi
pendingLock.write {
// this just does a .toUShort().toInt() conversion. This is cleaner than doing it manually
pending[RmiUtils.unpackUnsignedRight(responseRmi.id)] = responseRmi
}
return responseRmi
}
/**

View File

@ -99,22 +99,23 @@ internal class RmiClient(val isGlobal: Boolean,
// 'timeout > 0' -> WAIT w/ TIMEOUT
// '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.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
return if (isAsync) {
null
connection.send(invokeMethod)
return null
} 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)
}
}