Added lock downgrades and fixed unlocking ordering
This commit is contained in:
parent
d5095be95d
commit
63a182afd7
1 changed files with 54 additions and 48 deletions
|
|
@ -74,8 +74,9 @@ trait NettyRemoteClientModule extends RemoteClientModule with NettyRemoteShared
|
||||||
|
|
||||||
val key = makeKey(address)
|
val key = makeKey(address)
|
||||||
lock.readLock.lock
|
lock.readLock.lock
|
||||||
|
try {
|
||||||
remoteClients.get(key) match {
|
remoteClients.get(key) match {
|
||||||
case Some(client) => try { client } finally { lock.readLock.unlock }
|
case Some(client) => client
|
||||||
case None =>
|
case None =>
|
||||||
lock.readLock.unlock
|
lock.readLock.unlock
|
||||||
lock.writeLock.lock //Lock upgrade, not supported natively
|
lock.writeLock.lock //Lock upgrade, not supported natively
|
||||||
|
|
@ -88,8 +89,9 @@ trait NettyRemoteClientModule extends RemoteClientModule with NettyRemoteShared
|
||||||
remoteClients += key -> client
|
remoteClients += key -> client
|
||||||
client
|
client
|
||||||
}
|
}
|
||||||
} finally { lock.writeLock.unlock }
|
} finally { lock.readLock.lock; lock.writeLock.unlock } //downgrade
|
||||||
}
|
}
|
||||||
|
} finally { lock.readLock.unlock }
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -99,8 +101,9 @@ trait NettyRemoteClientModule extends RemoteClientModule with NettyRemoteShared
|
||||||
val address = channel.getRemoteAddress.asInstanceOf[InetSocketAddress]
|
val address = channel.getRemoteAddress.asInstanceOf[InetSocketAddress]
|
||||||
val key = makeKey(address)
|
val key = makeKey(address)
|
||||||
lock.readLock.lock
|
lock.readLock.lock
|
||||||
|
try {
|
||||||
remoteClients.get(key) match {
|
remoteClients.get(key) match {
|
||||||
case Some(client) => try { false } finally { lock.readLock.unlock }
|
case Some(client) => false
|
||||||
case None =>
|
case None =>
|
||||||
lock.readLock.unlock
|
lock.readLock.unlock
|
||||||
lock.writeLock.lock //Lock upgrade, not supported natively
|
lock.writeLock.lock //Lock upgrade, not supported natively
|
||||||
|
|
@ -113,8 +116,9 @@ trait NettyRemoteClientModule extends RemoteClientModule with NettyRemoteShared
|
||||||
remoteClients.put(key, client)
|
remoteClients.put(key, client)
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
} finally { lock.writeLock.unlock }
|
} finally { lock.readLock.lock; lock.writeLock.unlock } //downgrade
|
||||||
}
|
}
|
||||||
|
} finally { lock.readLock.unlock }
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -124,6 +128,7 @@ trait NettyRemoteClientModule extends RemoteClientModule with NettyRemoteShared
|
||||||
val address = channel.getRemoteAddress.asInstanceOf[InetSocketAddress]
|
val address = channel.getRemoteAddress.asInstanceOf[InetSocketAddress]
|
||||||
val key = makeKey(address)
|
val key = makeKey(address)
|
||||||
lock.readLock.lock
|
lock.readLock.lock
|
||||||
|
try {
|
||||||
remoteClients.get(key) match {
|
remoteClients.get(key) match {
|
||||||
case Some(client: PassiveRemoteClient) =>
|
case Some(client: PassiveRemoteClient) =>
|
||||||
lock.readLock.unlock
|
lock.readLock.unlock
|
||||||
|
|
@ -136,10 +141,11 @@ trait NettyRemoteClientModule extends RemoteClientModule with NettyRemoteShared
|
||||||
remoteClients.remove(key)
|
remoteClients.remove(key)
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
} finally { lock.writeLock.unlock }
|
} finally { lock.readLock.lock; lock.writeLock.unlock } //downgrade
|
||||||
//Otherwise, unlock the readlock and return false
|
//Otherwise, unlock the readlock and return false
|
||||||
case _ => try { false } finally { lock.readLock.unlock }
|
case _ => false
|
||||||
}
|
}
|
||||||
|
} finally { lock.readLock.unlock }
|
||||||
}
|
}
|
||||||
|
|
||||||
private def makeKey(a: InetSocketAddress): String = a match {
|
private def makeKey(a: InetSocketAddress): String = a match {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue