mirror of
https://github.com/SlimeVR/SlimeVR-Server.git
synced 2026-11-28 12:56:32 -08:00
Small optimisations / fixes to make sure couroutines do well and prevent dataraces
This commit is contained in:
1 parent
af4665e7c9
commit
e37a37bcf9
3 files changed
+19
-24
No files matched your search
@@ -6,7 +6,6 @@ import dev.slimevr.context.CustomModule
|
||||
import dev.slimevr.context.createContext
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import solarxr_protocol.data_feed.DataFeedConfig
|
||||
@@ -35,19 +34,14 @@ class PacketDispatcher<T : Any> {
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
inline fun <reified P : T> on(crossinline callback: suspend (P) -> Unit) {
|
||||
runBlocking {
|
||||
mutex.withLock {
|
||||
val list =
|
||||
listeners.getOrPut(P::class as KClass<out T>) { mutableListOf() }
|
||||
list.add { callback(it as P) }
|
||||
}
|
||||
synchronized(this) {
|
||||
listeners.getOrPut(P::class as KClass<out T>) { mutableListOf() }
|
||||
.add { callback(it as P) }
|
||||
}
|
||||
}
|
||||
|
||||
fun onAny(callback: suspend (T) -> Unit) {
|
||||
runBlocking {
|
||||
mutex.withLock { globalListeners.add(callback) }
|
||||
}
|
||||
synchronized(this) { globalListeners.add(callback) }
|
||||
}
|
||||
|
||||
suspend fun emit(event: T) {
|
||||
|
||||
@@ -9,9 +9,9 @@ import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.flow.distinctUntilChangedBy
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
|
||||
data class VRServerState(
|
||||
val handleId: Int,
|
||||
val trackers: Map<Int, Tracker>,
|
||||
val devices: Map<Int, Device>,
|
||||
)
|
||||
@@ -27,15 +27,8 @@ typealias VRServerModule = BasicModule<VRServerState, VRServerActions>
|
||||
val BaseModule = VRServerModule(
|
||||
reducer = { s, a ->
|
||||
when (a) {
|
||||
is VRServerActions.NewTracker -> s.copy(
|
||||
trackers = s.trackers + (a.trackerId to a.context),
|
||||
handleId = a.trackerId,
|
||||
)
|
||||
|
||||
is VRServerActions.NewDevice -> s.copy(
|
||||
devices = s.devices + (a.deviceId to a.context),
|
||||
handleId = a.deviceId,
|
||||
)
|
||||
is VRServerActions.NewTracker -> s.copy(trackers = s.trackers + (a.trackerId to a.context))
|
||||
is VRServerActions.NewDevice -> s.copy(devices = s.devices + (a.deviceId to a.context))
|
||||
}
|
||||
},
|
||||
observer = { context ->
|
||||
@@ -47,15 +40,16 @@ val BaseModule = VRServerModule(
|
||||
|
||||
data class VRServer(
|
||||
val context: VRServerContext,
|
||||
// Moved this outside of the context to make this faster and safer to use
|
||||
private val handleCounter: AtomicInteger,
|
||||
) {
|
||||
fun nextHandle() = context.state.value.handleId + 1
|
||||
fun nextHandle() = handleCounter.incrementAndGet()
|
||||
fun getTracker(id: Int) = context.state.value.trackers[id]
|
||||
fun getDevice(id: Int) = context.state.value.devices[id]
|
||||
|
||||
companion object {
|
||||
fun create(scope: CoroutineScope): VRServer {
|
||||
val server = VRServerState(
|
||||
handleId = 0,
|
||||
trackers = mapOf(),
|
||||
devices = mapOf(),
|
||||
)
|
||||
@@ -72,6 +66,7 @@ data class VRServer(
|
||||
|
||||
return VRServer(
|
||||
context = context,
|
||||
handleCounter = AtomicInteger(0),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user