Alter synchronization for TickTask queues.

This commit is contained in:
asofold 2012-12-02 18:22:54 +01:00
parent 5fc17e0c11
commit 9ed8a150bc

View File

@ -1,7 +1,6 @@
package fr.neatmonster.nocheatplus.utilities; package fr.neatmonster.nocheatplus.utilities;
import java.util.Collections; import java.util.LinkedHashSet;
import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -47,10 +46,10 @@ public class TickTask implements Runnable {
} }
/** Permissions to update: player name -> check type. */ /** Permissions to update: player name -> check type. */
private static final Set<PermissionUpdateEntry> permissionUpdates = Collections.synchronizedSet(new HashSet<PermissionUpdateEntry>(50)); private static final Set<PermissionUpdateEntry> permissionUpdates = new LinkedHashSet<PermissionUpdateEntry>(50);
/** Actions to execute. */ /** Actions to execute. */
public static final List<ViolationData> delayedActions = Collections.synchronizedList(new LinkedList<ViolationData>()); public static final List<ViolationData> delayedActions = new LinkedList<ViolationData>();
/** Task id of the running TickTask */ /** Task id of the running TickTask */
protected static int taskId = -1; protected static int taskId = -1;
@ -76,6 +75,7 @@ public class TickTask implements Runnable {
public void executeActions() { public void executeActions() {
final List<ViolationData> copyActions = new LinkedList<ViolationData>(); final List<ViolationData> copyActions = new LinkedList<ViolationData>();
synchronized (delayedActions) { synchronized (delayedActions) {
if (delayedActions.isEmpty()) return;
copyActions.addAll(delayedActions); copyActions.addAll(delayedActions);
delayedActions.clear(); delayedActions.clear();
} }
@ -91,6 +91,7 @@ public class TickTask implements Runnable {
public static void updatePermissions() { public static void updatePermissions() {
final List<PermissionUpdateEntry> copyPermissions = new LinkedList<PermissionUpdateEntry>(); final List<PermissionUpdateEntry> copyPermissions = new LinkedList<PermissionUpdateEntry>();
synchronized (permissionUpdates) { synchronized (permissionUpdates) {
if (permissionUpdates.isEmpty()) return;
copyPermissions.addAll(permissionUpdates); copyPermissions.addAll(permissionUpdates);
permissionUpdates.clear(); permissionUpdates.clear();
} }
@ -118,9 +119,11 @@ public class TickTask implements Runnable {
* @param checkType * @param checkType
*/ */
public static void requestPermissionUpdate(final String playerName, final CheckType checkType){ public static void requestPermissionUpdate(final String playerName, final CheckType checkType){
synchronized(permissionUpdates){
if (locked) return; if (locked) return;
permissionUpdates.add(new PermissionUpdateEntry(playerName, checkType)); permissionUpdates.add(new PermissionUpdateEntry(playerName, checkType));
} }
}
/** /**
* Request actions execution.<br> * Request actions execution.<br>
@ -128,9 +131,11 @@ public class TickTask implements Runnable {
* @param actions * @param actions
*/ */
public static void requestActionsExecution(final ViolationData actions) { public static void requestActionsExecution(final ViolationData actions) {
synchronized (delayedActions) {
if (locked) return; if (locked) return;
delayedActions.add(actions); delayedActions.add(actions);
} }
}
/** /**
* Get the tasks tick count. It is increased with every server tick.<br> * Get the tasks tick count. It is increased with every server tick.<br>
@ -183,7 +188,8 @@ public class TickTask implements Runnable {
} }
/** /**
* Control if new elements can be added to request queues. * Control if new elements can be added to request queues.<br>
* NOTE: This is just a flag, no sync is done here.
* @param locked * @param locked
*/ */
public static void setLocked(boolean locked){ public static void setLocked(boolean locked){
@ -194,9 +200,13 @@ public class TickTask implements Runnable {
* Empty queues (call after setLocked(true) * Empty queues (call after setLocked(true)
*/ */
public static void purge(){ public static void purge(){
synchronized (permissionUpdates) {
permissionUpdates.clear(); permissionUpdates.clear();
}
synchronized (delayedActions) {
delayedActions.clear(); delayedActions.clear();
} }
}
////////////////////////// //////////////////////////
// Instance methods // Instance methods
@ -207,8 +217,8 @@ public class TickTask implements Runnable {
tick ++; tick ++;
final long time = System.currentTimeMillis(); final long time = System.currentTimeMillis();
// The isEmpty checks are faster than synchronizing fully always, the actions get delayed one tick at most. // The isEmpty checks are faster than synchronizing fully always, the actions get delayed one tick at most.
if (!delayedActions.isEmpty()) executeActions(); executeActions();
if (!permissionUpdates.isEmpty()) updatePermissions(); updatePermissions();
if (timeLast > time) { if (timeLast > time) {
LogUtil.logSevere("[NoCheatPlus] System time ran backwards (" + timeLast + "->" + time + "), clear all data and history..."); LogUtil.logSevere("[NoCheatPlus] System time ran backwards (" + timeLast + "->" + time + "), clear all data and history...");
DataManager.clearData(CheckType.ALL); DataManager.clearData(CheckType.ALL);