#3882: Reduce lock boilerplate by using lomboks @Locked

This commit is contained in:
Outfluencer
2025-09-26 10:03:05 +10:00
committed by md_5
parent 4c02676b7a
commit d37a430f5a
3 changed files with 49 additions and 132 deletions

View File

@@ -30,6 +30,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.logging.Level;
import lombok.Locked;
import lombok.RequiredArgsConstructor;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.CommandSender;
@@ -98,10 +99,8 @@ public final class PluginManager
* @param plugin the plugin owning this command
* @param command the command to register
*/
@Locked.Write("commandsLock")
public void registerCommand(Plugin plugin, Command command)
{
commandsLock.writeLock().lock();
try
{
commandMap.put( command.getName().toLowerCase( Locale.ROOT ), command );
for ( String alias : command.getAliases() )
@@ -109,10 +108,6 @@ public final class PluginManager
commandMap.put( alias.toLowerCase( Locale.ROOT ), command );
}
commandsByPlugin.put( plugin, command );
} finally
{
commandsLock.writeLock().unlock();
}
}
/**
@@ -120,17 +115,11 @@ public final class PluginManager
*
* @param command the command to unregister
*/
@Locked.Write("commandsLock")
public void unregisterCommand(Command command)
{
commandsLock.writeLock().lock();
try
{
while ( commandMap.values().remove( command ) );
commandsByPlugin.values().remove( command );
} finally
{
commandsLock.writeLock().unlock();
}
}
/**
@@ -138,10 +127,8 @@ public final class PluginManager
*
* @param plugin the plugin to register the commands of
*/
@Locked.Write("commandsLock")
public void unregisterCommands(Plugin plugin)
{
commandsLock.writeLock().lock();
try
{
for ( Iterator<Command> it = commandsByPlugin.get( plugin ).iterator(); it.hasNext(); )
{
@@ -149,10 +136,6 @@ public final class PluginManager
while ( commandMap.values().remove( command ) );
it.remove();
}
} finally
{
commandsLock.writeLock().unlock();
}
}
private Command getCommandIfEnabled(String commandName, CommandSender sender)
@@ -467,10 +450,8 @@ public final class PluginManager
* @param plugin the owning plugin
* @param listener the listener to register events for
*/
@Locked("listenersLock")
public void registerListener(Plugin plugin, Listener listener)
{
listenersLock.lock();
try
{
for ( Method method : listener.getClass().getDeclaredMethods() )
{
@@ -478,10 +459,6 @@ public final class PluginManager
}
eventBus.register( listener );
listenersByPlugin.put( plugin, listener );
} finally
{
listenersLock.unlock();
}
}
/**
@@ -489,17 +466,11 @@ public final class PluginManager
*
* @param listener the listener to unregister
*/
@Locked("listenersLock")
public void unregisterListener(Listener listener)
{
listenersLock.lock();
try
{
eventBus.unregister( listener );
listenersByPlugin.values().remove( listener );
} finally
{
listenersLock.unlock();
}
}
/**
@@ -507,20 +478,14 @@ public final class PluginManager
*
* @param plugin target plugin
*/
@Locked("listenersLock")
public void unregisterListeners(Plugin plugin)
{
listenersLock.lock();
try
{
for ( Iterator<Listener> it = listenersByPlugin.get( plugin ).iterator(); it.hasNext(); )
{
eventBus.unregister( it.next() );
it.remove();
}
} finally
{
listenersLock.unlock();
}
}
/**
@@ -528,16 +493,10 @@ public final class PluginManager
*
* @return commands
*/
@Locked.Read("commandsLock")
public Collection<Map.Entry<String, Command>> getCommands()
{
commandsLock.readLock().lock();
try
{
return Collections.unmodifiableCollection( commandMap.entrySet() );
} finally
{
commandsLock.readLock().unlock();
}
}
boolean isTransitiveDepend(PluginDescription plugin, PluginDescription depend)

View File

@@ -10,6 +10,7 @@ import java.util.Map;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.logging.Level;
import lombok.Locked;
import net.md_5.bungee.api.AbstractReconnectHandler;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.config.ServerInfo;
@@ -53,31 +54,17 @@ public class YamlReconnectHandler extends AbstractReconnectHandler
}
@Override
@Locked.Read("lock")
protected ServerInfo getStoredServer(ProxiedPlayer player)
{
ServerInfo server = null;
lock.readLock().lock();
try
{
server = ProxyServer.getInstance().getServerInfo( data.get( key( player ) ) );
} finally
{
lock.readLock().unlock();
}
return server;
return ProxyServer.getInstance().getServerInfo( data.get( key( player ) ) );
}
@Override
@Locked.Write("lock")
public void setServer(ProxiedPlayer player)
{
lock.writeLock().lock();
try
{
data.put( key( player ), ( player.getReconnectServer() != null ) ? player.getReconnectServer().getName() : player.getServer().getInfo().getName() );
} finally
{
lock.writeLock().unlock();
}
}
private String key(ProxiedPlayer player)

View File

@@ -45,6 +45,7 @@ import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import lombok.Getter;
import lombok.Locked;
import lombok.Setter;
import lombok.Synchronized;
import net.md_5.bungee.api.CommandSender;
@@ -525,19 +526,13 @@ public class BungeeCord extends ProxyServer
*
* @param packet the packet to send
*/
@Locked.Read("connectionLock")
public void broadcast(DefinedPacket packet)
{
connectionLock.readLock().lock();
try
{
for ( UserConnection con : connections.values() )
{
con.unsafe().sendPacket( packet );
}
} finally
{
connectionLock.readLock().unlock();
}
}
@Override
@@ -598,17 +593,11 @@ public class BungeeCord extends ProxyServer
}
@Override
@Locked.Read("connectionLock")
@SuppressWarnings("unchecked")
public Collection<ProxiedPlayer> getPlayers()
{
connectionLock.readLock().lock();
try
{
return Collections.unmodifiableCollection( new HashSet( connections.values() ) );
} finally
{
connectionLock.readLock().unlock();
}
}
@Override
@@ -618,16 +607,10 @@ public class BungeeCord extends ProxyServer
}
@Override
@Locked.Read("connectionLock")
public ProxiedPlayer getPlayer(String name)
{
connectionLock.readLock().lock();
try
{
return connections.get( name );
} finally
{
connectionLock.readLock().unlock();
}
}
public UserConnection getPlayerByOfflineUUID(UUID uuid)
@@ -647,16 +630,10 @@ public class BungeeCord extends ProxyServer
}
@Override
@Locked.Read("connectionLock")
public ProxiedPlayer getPlayer(UUID uuid)
{
connectionLock.readLock().lock();
try
{
return connectionsByUUID.get( uuid );
} finally
{
connectionLock.readLock().unlock();
}
}
@Override
@@ -782,10 +759,8 @@ public class BungeeCord extends ProxyServer
return true;
}
@Locked.Write("connectionLock")
public void removeConnection(UserConnection con)
{
connectionLock.writeLock().lock();
try
{
// TODO See #1218
if ( connections.get( con.getName() ) == con )
@@ -794,10 +769,6 @@ public class BungeeCord extends ProxyServer
connectionsByUUID.remove( con.getUniqueId() );
connectionsByOfflineUUID.remove( con.getPendingConnection().getOfflineId() );
}
} finally
{
connectionLock.writeLock().unlock();
}
}
@Override