Fix another concurrent modification

This commit is contained in:
Mygod
2019-07-18 14:10:39 +08:00
parent 96fbb7ddf9
commit 81979aad38

View File

@@ -10,7 +10,7 @@ class IpLinkMonitor private constructor() : IpMonitor() {
private val callbacks = HashMap<Any, Pair<String, (Boolean) -> Unit>>() private val callbacks = HashMap<Any, Pair<String, (Boolean) -> Unit>>()
private var instance: IpLinkMonitor? = null private var instance: IpLinkMonitor? = null
fun registerCallback(owner: Any, iface: String, callback: (Boolean) -> Unit) { fun registerCallback(owner: Any, iface: String, callback: (Boolean) -> Unit) = synchronized(this) {
check(callbacks.put(owner, Pair(iface, callback)) == null) check(callbacks.put(owner, Pair(iface, callback)) == null)
var monitor = instance var monitor = instance
if (monitor == null) { if (monitor == null) {
@@ -19,8 +19,8 @@ class IpLinkMonitor private constructor() : IpMonitor() {
} }
monitor.flush() monitor.flush()
} }
fun unregisterCallback(owner: Any) { fun unregisterCallback(owner: Any) = synchronized(this) {
if (callbacks.remove(owner) == null || callbacks.isNotEmpty()) return if (callbacks.remove(owner) == null || callbacks.isNotEmpty()) return@synchronized
instance?.destroy() instance?.destroy()
instance = null instance = null
} }
@@ -32,12 +32,14 @@ class IpLinkMonitor private constructor() : IpMonitor() {
val match = parser.find(line) ?: return val match = parser.find(line) ?: return
val iface = match.groupValues[2] val iface = match.groupValues[2]
val present = match.groupValues[1].isEmpty() val present = match.groupValues[1].isEmpty()
for ((target, callback) in callbacks.values) if (target == iface) callback(present) synchronized(IpLinkMonitor) {
for ((target, callback) in callbacks.values) if (target == iface) callback(present)
}
} }
override fun processLines(lines: Sequence<String>) { override fun processLines(lines: Sequence<String>) {
val present = HashSet<String>() val present = HashSet<String>()
for (it in lines) present.add((parser.find(it) ?: continue).groupValues[2]) for (it in lines) present.add((parser.find(it) ?: continue).groupValues[2])
callbacks.values.forEach { (iface, callback) -> callback(present.contains(iface)) } synchronized(IpLinkMonitor) { for ((iface, callback) in callbacks.values) callback(present.contains(iface)) }
} }
} }