Browse Source

Fixed Race Condition properly, now that I got reminded that Locks frikkin exist

Gregorius Techneticies 3 months ago
parent
commit
13c955eda4
Signed by: Gregorius Techneticies <gregoriustech@gmail.com> GPG Key ID: 1B693EA14F2FD70F

+ 2
- 2
src/main/java/gregapi/GT_API_Proxy.java View File

@@ -239,7 +239,7 @@ public abstract class GT_API_Proxy extends Abstract_Proxy implements IGuiHandler
239 239
 	@SuppressWarnings("unchecked")
240 240
 	public void onServerTick(ServerTickEvent aEvent) {
241 241
 		if (aEvent.side.isServer()) {
242
-			TICK_LOCK = T;
242
+			TICK_LOCK.lock();
243 243
 			
244 244
 			// Making sure it is being free'd up in order to prevent exploits or Garbage Collection mishaps.
245 245
 			LAST_BROKEN_TILEENTITY.set(null);
@@ -438,7 +438,7 @@ public abstract class GT_API_Proxy extends Abstract_Proxy implements IGuiHandler
438 438
 					}
439 439
 				}
440 440
 				EntityFoodTracker.tick();
441
-				TICK_LOCK = F;
441
+				TICK_LOCK.unlock();
442 442
 			}
443 443
 		}
444 444
 	}

+ 3
- 2
src/main/java/gregapi/data/CS.java View File

@@ -22,6 +22,7 @@ package gregapi.data;
22 22
 import java.io.File;
23 23
 import java.io.PrintStream;
24 24
 import java.util.*;
25
+import java.util.concurrent.locks.ReentrantLock;
25 26
 
26 27
 import gregapi.api.Abstract_Mod;
27 28
 import gregapi.block.BlockBase;
@@ -301,8 +302,8 @@ public class CS {
301 302
 	public static long SERVER_TIME = 0;
302 303
 	/** Current Time on the Client. Used for Animations. */
303 304
 	public static long CLIENT_TIME = 0;
304
-	/** Is true whenever updateEntities and similar are running on the tick. */
305
-	public static volatile boolean TICK_LOCK = F;
305
+	/** Is locked updateEntities and similar are running on the tick. */
306
+	public static final ReentrantLock TICK_LOCK = new ReentrantLock();
306 307
 	
307 308
 	/** If I ever need to talk in Chat. XD */
308 309
 	public static final String CHAT_GREG = LH.Chat.WHITE+"<"+LH.Chat.BLUE+"GregoriusT"+LH.Chat.WHITE+"> ";

+ 2
- 1
src/main/java/gregapi/tileentity/ITileEntityMachineBlockUpdateable.java View File

@@ -125,8 +125,9 @@ public interface ITileEntityMachineBlockUpdateable {
125 125
 			
126 126
 			private void stepToUpdateMachine(World aWorld, ChunkCoordinates aCoords, HashSetNoNulls<ChunkCoordinates> aSet) {
127 127
 				// Wait for the updateEntities Thread to be done because fucking Mojang and race conditions in loading Chunks.
128
-				while (TICK_LOCK) try {Thread.sleep(1);} catch (InterruptedException e) {/**/}
128
+				TICK_LOCK.lock();
129 129
 				TileEntity tTileEntity = WD.te(aWorld, aCoords, T);
130
+				TICK_LOCK.unlock();
130 131
 				if (tTileEntity instanceof ITileEntityMachineBlockUpdateable) ((ITileEntityMachineBlockUpdateable)tTileEntity).onMachineBlockUpdate(mCoords, mBlock, mMeta, mRemoved);
131 132
 				if (aSet.size() < 5 || tTileEntity instanceof ITileEntityMachineBlockUpdateable || isMachineBlock(aWorld.getBlock(aCoords.posX, aCoords.posY, aCoords.posZ), aWorld.getBlockMetadata(aCoords.posX, aCoords.posY, aCoords.posZ))) {
132 133
 					ChunkCoordinates tCoords;

Loading…
Cancel
Save