Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix thread issues #246

Merged
merged 22 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
if: endswith(github.ref_name, 'master') && github.ref_protected && github.ref_type == 'branch'
runs-on: ubuntu-latest
env:
APPVEYOR_BUILD_VERSION: '3.6.2'
APPVEYOR_BUILD_VERSION: '3.6.3'
CURSETOKEN: ${{ secrets.CURSETOKEN }}
steps:
- uses: actions/checkout@v3
Expand Down
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## [1.12.2 - 3.6.3]
* fix: threads blocking each other
* ref: better system for pathway updateing and signal setting
* fix: wrong lamp in wn signal

## [1.12.2 - 3.6.2]
* feat: added train length signs
* feat: added trainnumber element
Expand Down
19 changes: 10 additions & 9 deletions src/main/java/com/troblecodings/signals/handler/NameHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,19 @@ private static ByteBuffer packToBuffer(final BlockPos pos, final String name) {
}

public static boolean isNameLoaded(final StateInfo info) {
synchronized (ALL_NAMES) {
return ALL_NAMES.containsKey(info);
}
return ALL_NAMES.containsKey(info);
}

public static void runTaskWhenNameLoaded(final StateInfo info,
final NameStateListener listener) {
if (!info.isValid() || info.worldNullOrClientSide())
return;
if (isNameLoaded(info)) {
final String name;
synchronized (ALL_NAMES) {
listener.update(info, ALL_NAMES.get(info), ChangedState.UPDATED);
name = ALL_NAMES.get(info);
}
listener.update(info, name, ChangedState.UPDATED);
} else {
synchronized (TASKS_WHEN_LOAD) {
final List<NameStateListener> list = TASKS_WHEN_LOAD.computeIfAbsent(info,
Expand Down Expand Up @@ -361,12 +361,13 @@ public static void loadNames(final List<StateLoadHolder> infos,
ALL_NAMES.put(info.info, name);
}
sendToAll(info.info, name);
final List<NameStateListener> tasks;
synchronized (TASKS_WHEN_LOAD) {
final List<NameStateListener> tasks = TASKS_WHEN_LOAD.remove(info.info);
if (tasks != null) {
tasks.forEach(listener -> listener.update(info.info, name,
ChangedState.ADDED_TO_CACHE));
}
tasks = TASKS_WHEN_LOAD.remove(info.info);
}
if (tasks != null) {
tasks.forEach(listener -> listener.update(info.info, name,
ChangedState.ADDED_TO_CACHE));
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,11 @@ public static void runTaskWhenSignalLoaded(final SignalStateInfo info,
if (!info.isValid() || info.worldNullOrClientSide())
return;
if (isSignalLoaded(info)) {
final Map<SEProperty, String> properties;
synchronized (CURRENTLY_LOADED_STATES) {
listener.update(info, CURRENTLY_LOADED_STATES.get(info), ChangedState.UPDATED);
properties = CURRENTLY_LOADED_STATES.get(info);
}
listener.update(info, properties, ChangedState.UPDATED);
} else {
synchronized (TASKS_WHEN_LOAD) {
final List<SignalStateListener> list = TASKS_WHEN_LOAD.computeIfAbsent(info,
Expand Down Expand Up @@ -560,12 +562,13 @@ public static void loadSignals(final List<SignalStateLoadHoler> signals,
}
sendToAll(info.info, properties);
updateListeners(info.info, properties, ChangedState.ADDED_TO_CACHE);
final List<SignalStateListener> tasks;
synchronized (TASKS_WHEN_LOAD) {
final List<SignalStateListener> tasks = TASKS_WHEN_LOAD.remove(info.info);
if (tasks != null) {
tasks.forEach(listener -> listener.update(info.info, properties,
ChangedState.ADDED_TO_CACHE));
}
tasks = TASKS_WHEN_LOAD.remove(info.info);
}
if (tasks != null) {
tasks.forEach(listener -> listener.update(info.info, properties,
ChangedState.ADDED_TO_CACHE));
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ public boolean tryBlock(final BlockPos position) {
pathwayToBlock = (InterSignalBoxPathway) pw;
pathwayToBlock.setPathStatus(EnumPathUsage.BLOCKED);
pathwayToBlock.updateTrainNumber(trainNumber);
otherGrid.updateToNet(pathwayToBlock);
});
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ protected void resetPathway(final SignalBoxPathway pathway) {
updatePrevious(pathway);
this.startsToPath.remove(pathway.getFirstPoint());
this.endsToPath.remove(pathway.getLastPoint());
pathway.postReset();
}

protected void updateToNet(final SignalBoxPathway pathway) {
Expand Down Expand Up @@ -182,9 +183,19 @@ protected void addPathway(final PathwayData data) {
}

protected void updatePrevious(final SignalBoxPathway pathway) {
SignalBoxPathway previousPath = endsToPath.get(pathway.getFirstPoint());
if (previousPath != null) {
SignalBoxPathway previousPath = pathway;
int count = 0;
while ((previousPath = endsToPath.get(previousPath.getFirstPoint())) != null) {
if (count > endsToPath.size()) {
break;
}
previousPath.setSignals();
count++;
}
if (count == 0) {
if (OpenSignalsMain.isDebug()) {
OpenSignalsMain.getLogger().debug("Could not find previous! " + pathway);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
public class SignalBoxPathway implements IChunkLoadable {

protected final PathwayData data;
protected final SignalConfig config;

protected boolean isBlocked;
protected boolean isAutoPathway = false;
Expand All @@ -58,7 +57,6 @@ public void setTile(final SignalBoxTileEntity tile) {
}

public SignalBoxPathway(final PathwayData data) {
this.config = new SignalConfig(this);
this.data = data;
this.originalFirstPoint = new Point(data.getFirstPoint());
updatePathwayToAutomatic();
Expand Down Expand Up @@ -205,7 +203,7 @@ protected void setSignals(final SignalStateInfo lastSignal) {
if (first == null)
return;
final SignalStateInfo firstInfo = new SignalStateInfo(world, startSignal.pos, first);
config.change(new ConfigInfo(firstInfo, lastSignal, data));
SignalConfig.change(new ConfigInfo(firstInfo, lastSignal, data));
updatePreSignals();
}
final SignalBoxPathway next = getNextPathway();
Expand All @@ -225,9 +223,9 @@ protected void setSignals(final SignalStateInfo lastSignal) {
new SignalStateInfo(world, position.pos, current), lastSignal, data,
position.isRepeater);
if (position.guiMode.equals(EnumGuiMode.HP)) {
config.loadDisable(info);
SignalConfig.loadDisable(info);
} else {
config.change(info);
SignalConfig.change(info);
}
});
updateSignalStates();
Expand All @@ -237,10 +235,6 @@ private void updatePreSignals() {
final MainSignalIdentifier startSignal = data.getStartSignal();
if (startSignal == null)
return;
final SignalBoxPathway next = getNextPathway();
if (next != null && (next.isEmptyOrBroken() || next.isBlocked)) {
return;
}
final StateInfo identifier = new StateInfo(tile.getWorld(), tile.getPos());
final Signal first = SignalBoxHandler.getSignal(identifier, startSignal.pos);
if (first == null)
Expand All @@ -251,7 +245,7 @@ private void updatePreSignals() {
final Signal current = SignalBoxHandler.getSignal(identifier, posIdent.pos);
if (current == null)
return;
config.change(
SignalConfig.change(
new ConfigInfo(new SignalStateInfo(tile.getWorld(), posIdent.pos, current),
firstInfo, data, posIdent.isRepeater));
});
Expand Down Expand Up @@ -445,11 +439,14 @@ public void resetPathway(final @Nullable Point point) {
resetAllTrainNumbers();
sendTrainNumberUpdates();
resetProtectionWay();
final SignalBoxPathway next = getNextPathway();
if (next != null) {
next.updatePreSignals();
next.updateSignalStates();
}
}
}

public void postReset() {
final SignalBoxPathway next = getNextPathway();
if (next != null) {
next.updatePreSignals();
next.updateSignalStates();
}
}

Expand Down Expand Up @@ -610,6 +607,8 @@ private void sendTrainNumberUpdates() {
buffer.putInt(trainNumberDisplays.size());
trainNumberDisplays.forEach(ident -> {
final SignalBoxNode node = grid.getNode(ident.point);
if (node == null)
return;
node.getPoint().writeNetwork(buffer);
node.writeNetwork(buffer);
});
Expand All @@ -621,9 +620,15 @@ private void resetAllTrainNumbers() {
}

private void resetAllTrainNumbers(final List<ModeIdentifier> trainNumberDisplays) {
if (grid != null && trainNumberDisplays != null)
trainNumberDisplays.forEach(ident -> grid.getNode(ident.point).getOption(ident.mode)
.orElse(new PathOptionEntry()).removeEntry(PathEntryType.TRAINNUMBER));
if (grid != null && trainNumberDisplays != null) {
trainNumberDisplays.forEach(ident -> {
final SignalBoxNode node = grid.getNode(ident.point);
if (node == null)
return;
node.getOption(ident.mode).orElse(new PathOptionEntry())
.removeEntry(PathEntryType.TRAINNUMBER);
});
}
}

public void deactivateAllOutputsOnPathway() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,16 @@
import com.troblecodings.signals.handler.SignalStateHandler;
import com.troblecodings.signals.handler.SignalStateInfo;
import com.troblecodings.signals.properties.PredicatedPropertyBase.ConfigProperty;
import com.troblecodings.signals.signalbox.SignalBoxPathway;

public final class SignalConfig {

private static final LoadHolder<Class<SignalConfig>> LOAD_HOLDER = new LoadHolder<>(
SignalConfig.class);

private final SignalBoxPathway pathway;

public SignalConfig(final SignalBoxPathway pathway) {
this.pathway = pathway;
private SignalConfig() {
}

public void change(final ConfigInfo info) {
public static void change(final ConfigInfo info) {
final Signal currentSignal = info.currentinfo.signal;
if (info.type.equals(PathType.NORMAL)) {
if (info.nextinfo != null && info.nextinfo.isValid()) {
Expand All @@ -55,7 +51,7 @@ public void change(final ConfigInfo info) {
}
}

private void loadDefault(final ConfigInfo info) {
private static void loadDefault(final ConfigInfo info) {
if (!info.currentinfo.isValid())
return;
final List<ConfigProperty> defaultValues = OneSignalPredicateConfigParser.DEFAULTCONFIGS
Expand Down Expand Up @@ -88,15 +84,15 @@ public static void reset(final ResetInfo info) {
});
}

public void loadDisable(final ConfigInfo info) {
public static void loadDisable(final ConfigInfo info) {
final List<ConfigProperty> disableValues = OneSignalPredicateConfigParser.DISABLECONFIGS
.get(info.currentinfo.signal);
if (disableValues != null) {
changeIfPresent(disableValues, info);
}
}

private void changeIfPresent(final List<ConfigProperty> values, final ConfigInfo info) {
private static void changeIfPresent(final List<ConfigProperty> values, final ConfigInfo info) {
loadSignalAndRunTask(info.currentinfo, (stateInfo, oldProperties, _u) -> {
if (info.nextinfo != null) {
loadSignalAndRunTask(info.nextinfo, (nextInfo, nextProperties, _u2) -> {
Expand All @@ -106,10 +102,9 @@ private void changeIfPresent(final List<ConfigProperty> values, final ConfigInfo
changeSignals(values, info, oldProperties, null);
}
});

}

private void changeSignals(final List<ConfigProperty> values, final ConfigInfo info,
private static void changeSignals(final List<ConfigProperty> values, final ConfigInfo info,
final Map<SEProperty, String> oldProperties,
final Map<SEProperty, String> nextProperties) {
final Map<Class<?>, Object> object = new HashMap<>();
Expand All @@ -127,11 +122,10 @@ private void changeSignals(final List<ConfigProperty> values, final ConfigInfo i
});
if (!propertiesToSet.isEmpty()) {
SignalStateHandler.setStates(info.currentinfo, propertiesToSet);
pathway.updatePrevious();
}
}

private void loadWithoutPredicate(final List<ConfigProperty> values,
private static void loadWithoutPredicate(final List<ConfigProperty> values,
final SignalStateInfo current) {
if (values != null) {
loadSignalAndRunTask(current, (info, oldProperties, _u) -> {
Expand All @@ -141,9 +135,9 @@ private void loadWithoutPredicate(final List<ConfigProperty> values,
.filter(entry -> oldProperties.containsKey(entry.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
});
if (!propertiesToSet.isEmpty())
if (!propertiesToSet.isEmpty()) {
SignalStateHandler.setStates(current, propertiesToSet);
pathway.updatePrevious();
}
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@
{
"blockstate":"hasandis(WNTYPE) && with(WNCROSS.BLINK)",
"retexture":{
"lamp_1": "lamp_white_blink"
"lamp_1_north": "lamp_white_blink",
"lamp_1_south": "lamp_white_blink"
}
}
]
Expand Down
Loading