Add synchronization in IpNeighbourMonitor

This commit is contained in:
Mygod
2018-10-04 15:56:24 +08:00
parent e66025c746
commit b9d5a63c23

View File

@@ -10,8 +10,8 @@ class IpNeighbourMonitor private constructor() : IpMonitor() {
private val callbacks = HashSet<Callback>() private val callbacks = HashSet<Callback>()
var instance: IpNeighbourMonitor? = null var instance: IpNeighbourMonitor? = null
fun registerCallback(callback: Callback) { fun registerCallback(callback: Callback) = synchronized(this) {
if (!callbacks.add(callback)) return if (!callbacks.add(callback)) return@synchronized
var monitor = instance var monitor = instance
if (monitor == null) { if (monitor == null) {
monitor = IpNeighbourMonitor() monitor = IpNeighbourMonitor()
@@ -21,8 +21,8 @@ class IpNeighbourMonitor private constructor() : IpMonitor() {
callback.onIpNeighbourAvailable(synchronized(monitor.neighbours) { monitor.neighbours.values.toList() }) callback.onIpNeighbourAvailable(synchronized(monitor.neighbours) { monitor.neighbours.values.toList() })
} }
} }
fun unregisterCallback(callback: Callback) { fun unregisterCallback(callback: Callback) = synchronized(this) {
if (!callbacks.remove(callback) || callbacks.isNotEmpty()) return if (!callbacks.remove(callback) || callbacks.isNotEmpty()) return@synchronized
instance?.destroy() instance?.destroy()
instance = null instance = null
} }
@@ -67,8 +67,10 @@ class IpNeighbourMonitor private constructor() : IpMonitor() {
updatePosted = false updatePosted = false
neighbours.values.toList() neighbours.values.toList()
} }
synchronized(IpNeighbourMonitor) {
for (callback in callbacks) callback.onIpNeighbourAvailable(neighbours) for (callback in callbacks) callback.onIpNeighbourAvailable(neighbours)
} }
}
updatePosted = true updatePosted = true
} }
} }