Added additional idle strategies
This commit is contained in:
parent
ed89b634a2
commit
ee558e666d
|
@ -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}"
|
||||
}
|
||||
}
|
|
@ -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 + "}"
|
||||
}
|
||||
}
|
|
@ -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 + "}"
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue