From b9d5a63c235db5a42e93d0262dd1ef3a5dc5cd6d Mon Sep 17 00:00:00 2001 From: Mygod Date: Thu, 4 Oct 2018 15:56:24 +0800 Subject: [PATCH] Add synchronization in IpNeighbourMonitor --- .../vpnhotspot/net/monitor/IpNeighbourMonitor.kt | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/IpNeighbourMonitor.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/IpNeighbourMonitor.kt index 55f0577f..427668fb 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/IpNeighbourMonitor.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/IpNeighbourMonitor.kt @@ -10,8 +10,8 @@ class IpNeighbourMonitor private constructor() : IpMonitor() { private val callbacks = HashSet() var instance: IpNeighbourMonitor? = null - fun registerCallback(callback: Callback) { - if (!callbacks.add(callback)) return + fun registerCallback(callback: Callback) = synchronized(this) { + if (!callbacks.add(callback)) return@synchronized var monitor = instance if (monitor == null) { monitor = IpNeighbourMonitor() @@ -21,8 +21,8 @@ class IpNeighbourMonitor private constructor() : IpMonitor() { callback.onIpNeighbourAvailable(synchronized(monitor.neighbours) { monitor.neighbours.values.toList() }) } } - fun unregisterCallback(callback: Callback) { - if (!callbacks.remove(callback) || callbacks.isNotEmpty()) return + fun unregisterCallback(callback: Callback) = synchronized(this) { + if (!callbacks.remove(callback) || callbacks.isNotEmpty()) return@synchronized instance?.destroy() instance = null } @@ -67,7 +67,9 @@ class IpNeighbourMonitor private constructor() : IpMonitor() { updatePosted = false neighbours.values.toList() } - for (callback in callbacks) callback.onIpNeighbourAvailable(neighbours) + synchronized(IpNeighbourMonitor) { + for (callback in callbacks) callback.onIpNeighbourAvailable(neighbours) + } } updatePosted = true }