Added additional idle strategies

This commit is contained in:
Robinson 2023-07-21 21:16:49 +02:00
parent ed89b634a2
commit ee558e666d
No known key found for this signature in database
GPG Key ID: 8E7DB78588BD6F5C
3 changed files with 247 additions and 0 deletions

View File

@ -0,0 +1,55 @@
/*
* Copyright 2023 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.network.aeron
import org.agrona.concurrent.BusySpinIdleStrategy
import org.agrona.concurrent.IdleStrategy
import org.agrona.hints.ThreadHints
class CoroutineBusySpinIdleStrategy : CoroutineIdleStrategy {
companion object {
const val ALIAS = "spin"
val INSTANCE = CoroutineBusySpinIdleStrategy()
}
override suspend fun idle(workCount: Int) {
if (workCount <= 0) {
ThreadHints.onSpinWait()
}
}
override suspend fun idle() {
ThreadHints.onSpinWait()
}
override fun reset() {}
override fun alias(): String {
return "spin"
}
override fun clone(): CoroutineIdleStrategy {
return CoroutineBusySpinIdleStrategy()
}
override fun cloneToNormal(): IdleStrategy {
return BusySpinIdleStrategy()
}
override fun toString(): String {
return "CoroutineBusySpinIdleStrategy{alias=spin}"
}
}

View File

@ -0,0 +1,96 @@
/*
* Copyright 2023 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Copyright 2014-2023 Real Logic Limited.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.network.aeron
import org.agrona.concurrent.IdleStrategy
import org.agrona.concurrent.NoOpIdleStrategy
/**
* Low-latency idle strategy to be employed in loops that do significant work on each iteration such that any
* work in the idle strategy would be wasteful.
*/
class CoroutineNoOpIdleStrategy : CoroutineIdleStrategy {
companion object {
/**
* Name to be returned from [.alias].
*/
const val ALIAS = "noop"
/**
* As there is no instance state then this object can be used to save on allocation.
*/
val INSTANCE = CoroutineNoOpIdleStrategy()
}
/**
* **Note**: this implementation will result in no safepoint poll once inlined.
*
*
* {@inheritDoc}
*/
override suspend fun idle(workCount: Int) {}
/**
* **Note**: this implementation will result in no safepoint poll once inlined.
*
*
* {@inheritDoc}
*/
override suspend fun idle() {}
/**
* {@inheritDoc}
*/
override fun reset() {}
/**
* {@inheritDoc}
*/
override fun alias(): String {
return ALIAS
}
override fun clone(): CoroutineIdleStrategy {
return CoroutineNoOpIdleStrategy()
}
override fun cloneToNormal(): IdleStrategy {
return NoOpIdleStrategy()
}
/**
* {@inheritDoc}
*/
override fun toString(): String {
return "CoroutineNoOpIdleStrategy{alias=" + ALIAS + "}"
}
}

View File

@ -0,0 +1,96 @@
/*
* Copyright 2023 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Copyright 2014-2023 Real Logic Limited.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.network.aeron
import org.agrona.concurrent.IdleStrategy
import org.agrona.concurrent.YieldingIdleStrategy
/**
* [IdleStrategy] that will call [Thread.yield] when the work count is zero.
*/
class CoroutineYieldingIdleStrategy : CoroutineIdleStrategy {
companion object {
/**
* Name to be returned from [.alias].
*/
const val ALIAS = "yield"
/**
* As there is no instance state then this object can be used to save on allocation.
*/
val INSTANCE = CoroutineYieldingIdleStrategy()
}
/**
* {@inheritDoc}
*/
override suspend fun idle(workCount: Int) {
if (workCount > 0) {
return
}
Thread.yield()
}
/**
* {@inheritDoc}
*/
override suspend fun idle() {
Thread.yield()
}
/**
* {@inheritDoc}
*/
override fun reset() {}
/**
* {@inheritDoc}
*/
override fun alias(): String {
return ALIAS
}
override fun clone(): CoroutineIdleStrategy {
return CoroutineYieldingIdleStrategy()
}
override fun cloneToNormal(): IdleStrategy {
return YieldingIdleStrategy()
}
/**
* {@inheritDoc}
*/
override fun toString(): String {
return "CoroutineYieldingIdleStrategy{alias=" + ALIAS + "}"
}
}