-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
ThreadManager.java
157 lines (138 loc) · 5.14 KB
/
ThreadManager.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
package me.nallar.tickthreading.minecraft;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import me.nallar.tickthreading.minecraft.tickthread.EntityTickThread;
import me.nallar.tickthreading.minecraft.tickthread.TileEntityTickThread;
import net.minecraft.src.Entity;
import net.minecraft.src.TileEntity;
import net.minecraft.src.World;
public class ThreadManager {
public final int regionSize = 16;
private CyclicBarrier tileEntityTickNotifyLatch = new CyclicBarrier(1);
private CyclicBarrier tileEntityTickEndLatch = new CyclicBarrier(1);
private CyclicBarrier entityTickNotifyLatch = new CyclicBarrier(1);
private CyclicBarrier entityTickEndLatch = new CyclicBarrier(1);
private final List<TileEntity> toRemoveTileEntities = new ArrayList<TileEntity>();
private final List<Entity> toRemoveEntities = new ArrayList<Entity>();
private final List<TileEntity> toAddTileEntities = new ArrayList<TileEntity>();
private final List<Entity> toAddEntities = new ArrayList<Entity>();
private final Map<Integer, TileEntityTickThread> tileEntityThreads = new HashMap<Integer, TileEntityTickThread>();
private final Map<Integer, EntityTickThread> entityThreads = new HashMap<Integer, EntityTickThread>();
private final World world;
public ThreadManager(World world) {
this.world = world;
}
// TODO: Don't use thread.stop() in unload(), use thread.interrupt() and make this return false.
@SuppressWarnings ("SameReturnValue")
public boolean waitForTileEntityTick() throws BrokenBarrierException {
try {
if (tileEntityTickNotifyLatch.await() == 0) {
tileEntityTickNotifyLatch.reset();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return true;
}
public void endTileEntityTick() throws BrokenBarrierException {
try {
if (tileEntityTickEndLatch.await() == 0) {
tileEntityTickEndLatch.reset();
processChanges();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// TODO: Don't use thread.stop() in unload(), use thread.interrupt() and make this return false.
@SuppressWarnings ("SameReturnValue")
public boolean waitForEntityTick() throws BrokenBarrierException {
try {
if (entityTickNotifyLatch.await() == 0) {
entityTickNotifyLatch.reset();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return true;
}
public void endEntityTick() throws BrokenBarrierException {
try {
if (entityTickEndLatch.await() == 0) {
entityTickEndLatch.reset();
processChanges();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@SuppressWarnings ("NumericCastThatLosesPrecision")
private TileEntityTickThread getThreadForTileEntity(TileEntity tileEntity) {
int hashCode = getHashCode(tileEntity);
TileEntityTickThread thread = tileEntityThreads.get(hashCode);
if (thread == null) {
thread = new TileEntityTickThread(world, "region: x" + tileEntity.xCoord / regionSize + ",z" + tileEntity.zCoord / regionSize, this, hashCode);
tileEntityThreads.put(hashCode, thread);
}
return thread;
}
@SuppressWarnings ("NumericCastThatLosesPrecision")
private EntityTickThread getThreadForEntity(Entity entity) {
int hashCode = getHashCode(entity);
EntityTickThread thread = entityThreads.get(hashCode);
if (thread == null) {
thread = new EntityTickThread(world, "region: x" + (int) entity.posX / regionSize + ",z" + (int) entity.posZ / regionSize, this, hashCode);
entityThreads.put(hashCode, thread);
}
return thread;
}
public int getHashCode(TileEntity tileEntity) {
return tileEntity.xCoord / regionSize + (tileEntity.zCoord / regionSize << 16);
}
@SuppressWarnings ("NumericCastThatLosesPrecision")
public int getHashCode(Entity entity) {
return ((int) entity.posX) / regionSize + (((int) entity.posY) / regionSize << 16);
}
private synchronized void processChanges() {
for (TileEntity tileEntity : toAddTileEntities) {
getThreadForTileEntity(tileEntity).add(tileEntity);
}
for (TileEntity tileEntity : toRemoveTileEntities) {
getThreadForTileEntity(tileEntity).remove(tileEntity);
}
for (Entity entity : toAddEntities) {
getThreadForEntity(entity).add(entity);
}
for (Entity entity : toRemoveEntities) {
getThreadForEntity(entity).remove(entity);
}
}
public synchronized void add(TileEntity tileEntity) {
toAddTileEntities.add(tileEntity);
}
public synchronized void add(Entity entity) {
toAddEntities.add(entity);
}
public synchronized void remove(TileEntity tileEntity) {
toRemoveTileEntities.add(tileEntity);
}
public synchronized void remove(Entity entity) {
toRemoveEntities.add(entity);
}
public void unload() {
for (Thread t : getThreads()) {
t.stop();
}
}
private Collection<Thread> getThreads() {
Collection<Thread> collection = new ArrayList<Thread>();
collection.addAll(tileEntityThreads.values());
collection.addAll(entityThreads.values());
return collection;
}
}