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>()
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,8 +67,10 @@ class IpNeighbourMonitor private constructor() : IpMonitor() {
updatePosted = false
neighbours.values.toList()
}
synchronized(IpNeighbourMonitor) {
for (callback in callbacks) callback.onIpNeighbourAvailable(neighbours)
}
}
updatePosted = true
}
}