-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
ThreadedChunkProvider.java
692 lines (613 loc) · 20.1 KB
/
ThreadedChunkProvider.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
package me.nallar.patched.storage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import com.google.common.collect.ImmutableSetMultimap;
import cpw.mods.fml.common.FMLLog;
import cpw.mods.fml.common.registry.GameRegistry;
import me.nallar.patched.annotation.FakeExtend;
import me.nallar.tickthreading.Log;
import me.nallar.tickthreading.collections.NonBlockingLongSet;
import me.nallar.tickthreading.minecraft.ChunkGarbageCollector;
import me.nallar.tickthreading.minecraft.TickThreading;
import me.nallar.tickthreading.patcher.Declare;
import me.nallar.tickthreading.util.DoNothingRunnable;
import me.nallar.tickthreading.util.ServerThreadFactory;
import me.nallar.tickthreading.util.concurrent.NativeMutex;
import me.nallar.unsafe.UnsafeUtil;
import net.minecraft.entity.EnumCreatureType;
import net.minecraft.util.IProgressUpdate;
import net.minecraft.util.LongHashMap;
import net.minecraft.world.ChunkCoordIntPair;
import net.minecraft.world.ChunkPosition;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.EmptyChunk;
import net.minecraft.world.chunk.IChunkProvider;
import net.minecraft.world.chunk.storage.IChunkLoader;
import net.minecraft.world.gen.ChunkProviderServer;
import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.common.ForgeChunkManager;
import org.cliffc.high_scale_lib.NonBlockingHashMapLong;
/**
* This is a replacement for ChunkProviderServer
* Instead of attempting to patch a class with many different implementations,
* this replaces it with an implementation which is intended to be compatible
* with both Forge and MCPC+.
*/
@FakeExtend
public abstract class ThreadedChunkProvider extends ChunkProviderServer implements IChunkProvider {
/**
* You may also use a synchronized block on generateLock,
* and are encouraged to unless tryLock() is required.
* This works as NativeMutex uses JVM monitors internally.
*/
public final NativeMutex generateLock = new NativeMutex();
private static final ThreadPoolExecutor chunkLoadThreadPool;
static {
int p = Runtime.getRuntime().availableProcessors();
chunkLoadThreadPool = new ThreadPoolExecutor(1, p, 60L, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(p * 10), new ServerThreadFactory("Async ChunkLoader"), new ThreadPoolExecutor.CallerRunsPolicy());
chunkLoadThreadPool.allowCoreThreadTimeOut(true);
}
private static final Runnable doNothingRunnable = new DoNothingRunnable();
private final NonBlockingHashMapLong<AtomicInteger> chunkLoadLocks = new NonBlockingHashMapLong<AtomicInteger>();
private final LongHashMap chunks = new LongHashMap();
private final LongHashMap loadingChunks = new LongHashMap();
private final LongHashMap unloadingChunks = new LongHashMap();
private final NonBlockingLongSet unloadStage0 = new NonBlockingLongSet();
private final ConcurrentLinkedQueue<QueuedUnload> unloadStage1 = new ConcurrentLinkedQueue<QueuedUnload>();
private final IChunkProvider generator; // Mojang shouldn't use the same interface for :(
private final IChunkLoader loader;
private final WorldServer world;
private final Chunk defaultEmptyChunk;
private final ThreadLocal<Boolean> inUnload = new BooleanThreadLocal();
private final boolean loadChunkIfNotFound;
private boolean loadedPersistentChunks = false;
private int unloadTicks = 0;
private Chunk lastChunk;
// Mojang compatiblity fields.
public final IChunkProvider currentChunkProvider;
public final Set<Long> chunksToUnload = unloadStage0;
public final List<Chunk> loadedChunks;
public final IChunkLoader currentChunkLoader;
@SuppressWarnings ("UnusedDeclaration")
public boolean loadChunkOnProvideRequest;
public ThreadedChunkProvider(WorldServer world, IChunkLoader loader, IChunkProvider generator) {
super(world, loader, generator); // This call will be removed by javassist.
currentChunkProvider = this.generator = generator;
this.world = world;
currentChunkLoader = this.loader = loader;
loadedChunks = Collections.synchronizedList(new ArrayList<Chunk>());
defaultEmptyChunk = new EmptyChunk(world, 0, 0);
loadChunkIfNotFound = TickThreading.instance.loadChunkOnProvideRequest;
}
@Override
@Declare
public List<Chunk> getLoadedChunks() {
return loadedChunks;
}
@Override
@Declare
public Set<Long> getChunksToUnloadSet() {
return unloadStage0;
}
@Override
public boolean unload100OldestChunks() {
return generator.unload100OldestChunks();
}
@SuppressWarnings ({"ConstantConditions", "FieldRepeatedlyAccessedInMethod"})
@Override
@Declare
public void tick() {
int ticks = world.tickCount;
// Handle unload requests
if (ticks % 3 == 0 && !world.canNotSave && !unloadStage0.isEmpty()) {
ImmutableSetMultimap<ChunkCoordIntPair, ForgeChunkManager.Ticket> persistentChunks = world.getPersistentChunks();
ChunkCoordIntPair chunkCoordIntPair = new ChunkCoordIntPair(0, 0);
NonBlockingHashMapLong.IteratorLong i$ = unloadStage0.iteratorLong();
while (i$.hasNext()) {
long key = i$.nextLong();
i$.remove();
if (key == 0) {
// Iterator can't remove 0 key for some reason.
unloadStage0.remove(0L);
}
int x = (int) key;
int z = (int) (key >> 32);
chunkCoordIntPair.chunkXPos = x;
chunkCoordIntPair.chunkZPos = z;
if (persistentChunks.containsKey(chunkCoordIntPair) || unloadingChunks.containsItem(key)) {
continue;
}
Chunk chunk = (Chunk) chunks.getValueByKey(key);
if (chunk == null || chunk.unloading || !fireBukkitUnloadEvent(chunk)) {
continue;
}
if (lastChunk == chunk) {
lastChunk = null;
}
chunk.onChunkUnload();
loadedChunks.remove(chunk);
chunks.remove(key);
chunk.unloading = true;
synchronized (unloadingChunks) {
unloadingChunks.add(key, chunk);
unloadStage1.add(new QueuedUnload(key, ticks));
}
}
if (loader != null) {
loader.chunkTick();
}
}
long queueThreshold = ticks - 15;
int done = 0;
// Handle unloading stage 1
{
QueuedUnload queuedUnload = unloadStage1.peek();
while (done++ <= 200 && queuedUnload != null && queuedUnload.ticks <= queueThreshold) {
long chunkHash = queuedUnload.key;
synchronized (unloadingChunks) {
if (!unloadStage1.remove(queuedUnload)) {
queuedUnload = unloadStage1.peek();
continue;
}
}
finalizeUnload(chunkHash);
queuedUnload = unloadStage1.peek();
}
}
if (this.unloadTicks++ > 1200 && world.provider.dimensionId != 0 && TickThreading.instance.allowWorldUnloading
&& loadedChunks.isEmpty() && ForgeChunkManager.getPersistentChunksFor(world).isEmpty()
&& (!TickThreading.instance.shouldLoadSpawn || !DimensionManager.shouldLoadSpawn(world.provider.dimensionId))) {
DimensionManager.unloadWorld(world.provider.dimensionId);
}
if (ticks % TickThreading.instance.chunkGCInterval == 0) {
ChunkGarbageCollector.garbageCollect(world);
}
if (!loadChunkIfNotFound && !loadedPersistentChunks && unloadTicks >= 5) {
loadedPersistentChunks = true;
int loaded = 0;
int possible = world.getPersistentChunks().size();
for (ChunkCoordIntPair chunkCoordIntPair : world.getPersistentChunks().keySet()) {
if (getChunkAt(chunkCoordIntPair.chunkXPos, chunkCoordIntPair.chunkZPos, doNothingRunnable) == null) {
loaded++;
}
possible++;
}
if (possible > 0) {
Log.info("Loaded " + loaded + '/' + possible + " persistent chunks in " + Log.name(world));
}
}
}
private void finalizeUnload(long key) {
Chunk chunk;
synchronized (unloadingChunks) {
chunk = (Chunk) unloadingChunks.getValueByKey(key);
}
if (chunk == null || !chunk.unloading) {
return;
}
synchronized (chunk) {
if (chunk.alreadySavedAfterUnload) {
return;
}
chunk.alreadySavedAfterUnload = true;
}
boolean notInUnload = !inUnload.get();
if (notInUnload) {
inUnload.set(true);
}
chunk.isChunkLoaded = false;
safeSaveChunk(chunk);
safeSaveExtraChunkData(chunk);
if (notInUnload) {
inUnload.set(false);
}
synchronized (unloadingChunks) {
unloadingChunks.remove(key);
}
}
// Public visibility as it will be accessed from net.minecraft.whatever, not actually this class
// (Inner classes are not remapped in patching)
public static class QueuedUnload {
public final int ticks;
public final long key;
public QueuedUnload(long key, int ticks) {
this.key = key;
this.ticks = ticks;
}
}
@Override
public boolean chunkExists(int x, int z) {
return chunks.containsItem(key(x, z));
}
@Override
@Declare
public boolean unloadChunk(int x, int z) {
long hash = key(x, z);
return chunks.containsItem(hash) && unloadStage0.add(hash);
}
@Override
public void unloadChunksIfNotNearSpawn(int x, int z) {
unloadChunk(x, z);
}
@Override
public void unloadAllChunks() {
if (loadedChunks.size() > world.getPersistentChunks().size()) {
synchronized (loadedChunks) {
for (Chunk chunk : loadedChunks) {
unloadStage0.add(key(chunk.xPosition, chunk.zPosition));
}
}
}
}
@Override
public final Chunk provideChunk(int x, int z) {
Chunk chunk = getChunkIfExists(x, z);
if (chunk != null) {
return chunk;
}
if (loadChunkIfNotFound) {
return getChunkAt(x, z, false, false, null);
}
return defaultEmptyChunk;
}
@Override
public final Chunk loadChunk(int x, int z) {
return getChunkAt(x, z, true, false, null);
}
@Override
@Declare
public final Chunk getChunkAt(final int x, final int z, final Runnable runnable) {
return getChunkAt(x, z, true, false, runnable);
}
public void unloadChunkImmediately(int x, int z, boolean save) {
long key = key(x, z);
finalizeUnload(key);
Chunk chunk = getChunkIfExists(x, z);
if (chunk == null || !fireBukkitUnloadEvent(chunk)) {
return;
}
chunk.unloading = true;
chunk.onChunkUnload();
chunk.isChunkLoaded = false;
if (save || chunk.isModified) {
safeSaveChunk(chunk);
safeSaveExtraChunkData(chunk);
}
loadedChunks.remove(chunk);
chunks.remove(key);
}
public Chunk regenerateChunk(int x, int z) {
unloadChunkImmediately(x, z, false);
return getChunkAt(x, z, true, true, null);
}
@Override
@SuppressWarnings ("ConstantConditions")
@Declare
public final Chunk getChunkAt(final int x, final int z, boolean allowGenerate, final Runnable runnable) {
return getChunkAt(x, z, allowGenerate, false, runnable);
}
@Override
@SuppressWarnings ("ConstantConditions")
@Declare
public final Chunk getChunkAt(final int x, final int z, boolean allowGenerate, boolean regenerate, final Runnable runnable) {
Chunk chunk = getChunkIfExists(x, z);
if (chunk != null) {
if (runnable != null) {
runnable.run();
}
return chunk;
}
if (runnable != null) {
chunkLoadThreadPool.execute(new ChunkLoadRunnable(x, z, runnable, this));
return null;
}
if (Thread.holdsLock(generateLock)) {
return defaultEmptyChunk;
}
long key = key(x, z);
final AtomicInteger lock = getLock(key);
boolean innerGenerate = false;
boolean wasGenerated = false;
try {
boolean inLoadingMap = false;
// Lock on the lock for this chunk - prevent multiple instances of the same chunk
ThreadLocal<Boolean> worldGenInProgress = world.worldGenInProgress;
synchronized (lock) {
finalizeUnload(key);
chunk = (Chunk) chunks.getValueByKey(key);
if (chunk != null) {
return chunk;
}
chunk = (Chunk) loadingChunks.getValueByKey(key);
if (chunk == null) {
chunk = regenerate ? null : safeLoadChunk(x, z);
if (chunk != null && (chunk.xPosition != x || chunk.zPosition != z)) {
Log.severe("Chunk at " + chunk.xPosition + ',' + chunk.zPosition + " was stored at " + x + ',' + z + "\nResetting this chunk.");
chunk = null;
}
if (chunk == null) {
loadingChunks.add(key, defaultEmptyChunk);
if (!allowGenerate) {
return defaultEmptyChunk;
}
} else {
loadingChunks.add(key, chunk);
inLoadingMap = true;
}
} else if (worldGenInProgress != null && worldGenInProgress.get() == Boolean.TRUE) {
return chunk;
} else {
inLoadingMap = true;
}
}
// Unlock this chunk - avoids a deadlock
// Thread A - requests chunk A - needs genned
// Thread B - requests chunk B - needs genned
// In thread A, redpower tries to load chunk B
// because its marble gen is buggy.
// Thread B is now waiting for the generate lock,
// Thread A is waiting for the lock on chunk B
// Lock the generation lock - ChunkProviderGenerate isn't threadsafe at all
// TODO: Possibly make ChunkProviderGenerate threadlocal? Would need many changes to
// structure code to get it to work properly.
try {
synchronized (generateLock) {
synchronized (lock) {
if (worldGenInProgress != null) {
if (!(innerGenerate = (worldGenInProgress.get() == Boolean.TRUE))) {
worldGenInProgress.set(Boolean.TRUE);
}
}
chunk = (Chunk) chunks.getValueByKey(key);
if (chunk != null) {
return chunk;
}
chunk = (Chunk) loadingChunks.getValueByKey(key);
if (chunk == null || chunk == defaultEmptyChunk) {
if (generator == null) {
chunk = defaultEmptyChunk;
} else {
try {
chunk = generator.provideChunk(x, z);
wasGenerated = true;
} catch (Throwable t) {
Log.severe("Failed to generate a chunk in " + Log.name(world) + " at chunk coords " + x + ',' + z);
throw UnsafeUtil.throwIgnoreChecked(t);
}
}
} else {
if (generator != null) {
generator.recreateStructures(x, z);
}
}
if (chunk == null) {
throw new IllegalStateException("Null chunk was provided for " + x + ',' + z);
}
if (!inLoadingMap) {
loadingChunks.add(key, chunk);
}
chunk.threadUnsafeChunkLoad();
chunks.add(key, chunk);
loadedChunks.add(chunk);
chunk.populateChunk(this, this, x, z);
}
}
} finally {
if (!innerGenerate && worldGenInProgress != null) {
worldGenInProgress.set(Boolean.FALSE);
}
}
} finally {
if (lock.decrementAndGet() == 0) {
loadingChunks.remove(key);
}
}
// TODO: Do initial mob spawning here - doing it while locked is stupid and can cause deadlocks with some bukkit plugins
chunk.onChunkLoad();
fireBukkitLoadEvent(chunk, wasGenerated);
chunkLoadLocks.remove(key);
return chunk;
}
private AtomicInteger getLock(long key) {
AtomicInteger lock = chunkLoadLocks.get(key);
if (lock != null) {
lock.incrementAndGet();
return lock;
}
AtomicInteger newLock = new AtomicInteger(1);
lock = chunkLoadLocks.putIfAbsent(key, newLock);
if (lock != null) {
lock.incrementAndGet();
return lock;
}
return newLock;
}
@Override
@Declare
public void fireBukkitLoadEvent(Chunk chunk, boolean newlyGenerated) {
}
@Override
@Declare
public boolean fireBukkitUnloadEvent(Chunk chunk) {
return true;
}
@Override
protected Chunk safeLoadChunk(int x, int z) {
if (loader == null) {
return null;
}
try {
Chunk chunk = loader.loadChunk(world, x, z);
if (chunk != null) {
chunk.lastSaveTime = world.getTotalWorldTime();
}
return chunk;
} catch (Exception e) {
FMLLog.log(Level.SEVERE, e, "Failed to load chunk at " + x + ',' + z);
return null;
}
}
@Override
protected void safeSaveExtraChunkData(Chunk chunk) {
if (loader == null) {
return;
}
try {
loader.saveExtraChunkData(world, chunk);
} catch (Exception e) {
FMLLog.log(Level.SEVERE, e, "Failed to save extra chunk data for " + chunk);
}
}
@Override
protected void safeSaveChunk(Chunk chunk) {
if (loader == null) {
return;
}
try {
chunk.lastSaveTime = world.getTotalWorldTime();
loader.saveChunk(world, chunk);
} catch (Exception e) {
FMLLog.log(Level.SEVERE, e, "Failed to save chunk " + chunk);
}
}
@Override
public void populate(IChunkProvider chunkProvider, int x, int z) {
Chunk chunk = getChunkIfExists(x, z);
if (chunk == null) {
return;
}
synchronized (generateLock) {
if (chunk.isTerrainPopulated) {
return;
}
if (generator != null) {
generator.populate(chunkProvider, x, z);
GameRegistry.generateWorld(x, z, world, generator, chunkProvider);
chunk.setChunkModified();
}
chunk.isTerrainPopulated = true;
}
}
@Override
@Declare
public void fireBukkitPopulateEvent(Chunk chunk) {
}
@Override
public boolean saveChunks(boolean saveAll, IProgressUpdate progressUpdate) {
int savedChunks = 0;
List<Chunk> chunksToSave = new ArrayList<Chunk>();
synchronized (loadedChunks) {
for (Chunk chunk : loadedChunks) {
if (chunk.unloading) {
if (saveAll) {
chunk.alreadySavedAfterUnload = true;
} else {
continue;
}
}
if (chunk.needsSaving(saveAll)) {
chunksToSave.add(chunk);
}
}
}
for (Chunk chunk : chunksToSave) {
if (chunks.getValueByKey(key(chunk.xPosition, chunk.zPosition)) != chunk) {
Log.warning("Not saving " + chunk + ", not in correct location in chunks map.");
continue;
}
if (saveAll) {
safeSaveExtraChunkData(chunk);
}
safeSaveChunk(chunk);
chunk.isModified = false;
if (++savedChunks == 24 && !saveAll) {
return false;
}
}
if (saveAll && loader != null) {
loader.saveExtraData();
}
return true;
}
@Override
public boolean canSave() {
return !world.canNotSave;
}
@Override
public String makeString() {
return "Loaded " + loadedChunks.size() + " Loading " + loadingChunks.getNumHashElements() + " Unload " + unloadStage0.size() + " UnloadSave " + unloadStage1.size() + " Locks " + chunkLoadLocks.size();
}
@Override
public List getPossibleCreatures(EnumCreatureType creatureType, int x, int y, int z) {
return generator.getPossibleCreatures(creatureType, x, y, z);
}
@Override
public ChunkPosition findClosestStructure(World world, String name, int x, int y, int z) {
return generator.findClosestStructure(world, name, x, y, z);
}
@Override
public int getLoadedChunkCount() {
return loadedChunks.size();
}
@Override
public void recreateStructures(int x, int z) {
}
@Override
@Declare
public final Chunk getChunkIfExists(int x, int z) {
Chunk chunk = lastChunk;
if (chunk != null && chunk.xPosition == x && chunk.zPosition == z) {
return chunk;
}
chunk = (Chunk) chunks.getValueByKey(key(x, z));
if (chunk == null) {
return null;
}
lastChunk = chunk;
return chunk;
}
private static long key(int x, int z) {
return (((long) z) << 32) | (x & 0xffffffffL);
}
public static class ChunkLoadRunnable implements Runnable {
private final int x;
private final int z;
private final Runnable runnable;
private final ChunkProviderServer provider;
public ChunkLoadRunnable(int x, int z, Runnable runnable, ChunkProviderServer provider) {
this.x = x;
this.z = z;
this.runnable = runnable;
this.provider = provider;
}
@Override
public void run() {
try {
Chunk ch = provider.getChunkAt(x, z, null);
if (ch == null) {
FMLLog.warning("Failed to load chunk at " + x + ',' + z + " asynchronously.");
} else {
runnable.run();
}
} catch (Throwable t) {
FMLLog.log(Level.SEVERE, t, "Exception loading chunk asynchronously.");
}
}
}
public static class BooleanThreadLocal extends ThreadLocal<Boolean> {
@Override
public Boolean initialValue() {
return false;
}
}
}