Skip to content

Commit

Permalink
Allow ConfigManager.getConfig... from any thread (copy on write).
Browse files Browse the repository at this point in the history
This allows fast config getting from any thread, without need to know if
it is the main thread.
  • Loading branch information
asofold committed Jul 16, 2014
1 parent 59fca0f commit 96462da
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
Expand Up @@ -54,7 +54,7 @@ public static ChatConfig getConfig(final Player player) {
synchronized (worldsMap) {
if (!worldsMap.containsKey(player.getWorld().getName()))
worldsMap.put(player.getWorld().getName(),
new ChatConfig(ConfigManager.getConfigFileSync(player.getWorld().getName())));
new ChatConfig(ConfigManager.getConfigFile(player.getWorld().getName())));
return worldsMap.get(player.getWorld().getName());
}
}
Expand Down
Expand Up @@ -3,6 +3,7 @@
import java.io.File;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

Expand Down Expand Up @@ -30,7 +31,7 @@ public final ActionFactory newActionFactory(final Map<String, Object> library) {
};

/** The map containing the configuration files per world. */
private static final Map<String, ConfigFile> worldsMap = new HashMap<String, ConfigFile>();
private static Map<String, ConfigFile> worldsMap = new LinkedHashMap<String, ConfigFile>();

private static final WorldConfigProvider<ConfigFile> worldConfigProvider = new WorldConfigProvider<ConfigFile>() {

Expand Down Expand Up @@ -118,7 +119,7 @@ public static void cleanup() {
}

/**
* Gets the configuration file.
* Gets the configuration file. Can be called from any thread.
*
* @return the configuration file
*/
Expand All @@ -129,13 +130,15 @@ public static ConfigFile getConfigFile() {
/**
* (Synchronized version).
* @return
* @deprecated getConfigFile() is thread-safe now.
*/
@Deprecated
public static synchronized ConfigFile getConfigFileSync() {
return getConfigFile();
}

/**
* Gets the configuration file.
* Gets the configuration file. Can be called from any thread.
*
* @param worldName
* the world name
Expand All @@ -146,14 +149,17 @@ public static ConfigFile getConfigFile(final String worldName) {
if (configFile != null){
return configFile;
}
// Expensive only once, for the rest of runtime the file is returned fast.
// Expensive only once per world, for the rest of the runtime the file is returned fast.
synchronized(ConfigManager.class){
// Need to check again.
if (worldsMap.containsKey(worldName)){
return worldsMap.get(worldName);
}
final ConfigFile globalConfig = getConfigFile();
worldsMap.put(worldName, globalConfig);
// Copy the whole map with the default configuration set for this world.
final Map<String, ConfigFile> newWorldsMap = new LinkedHashMap<String, ConfigFile>(ConfigManager.worldsMap);
final ConfigFile globalConfig = newWorldsMap.get(null);
newWorldsMap.put(worldName, globalConfig);
ConfigManager.worldsMap = newWorldsMap;
return globalConfig;
}
}
Expand All @@ -162,13 +168,15 @@ public static ConfigFile getConfigFile(final String worldName) {
* (Synchronized version).
* @param worldName
* @return
* @deprecated getConfigFile() is thread-safe now.
*/
@Deprecated
public static synchronized ConfigFile getConfigFileSync(final String worldName) {
return getConfigFile(worldName);
}

/**
* Initializes the configuration manager.
* Initializes the configuration manager. Must be called in the main thread.
*
* @param plugin
* the instance of NoCheatPlus
Expand Down

0 comments on commit 96462da

Please sign in to comment.