Skip to content

Commit

Permalink
Fixed thread-unsafeness of ResourceHolderCustomAssetExtension.getMain…
Browse files Browse the repository at this point in the history
…Class().
  • Loading branch information
MF42-DZH committed Sep 25, 2019
1 parent 4a15be0 commit 0c9ce1e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
21 changes: 16 additions & 5 deletions src/zeroxfc/nullpo/custom/libs/FieldScatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,23 @@ public class FieldScatter {
private int lifeTime;

/**
* Makes a new field explostion.
* Makes a new field explosion. Destroys blocks by default.
* @param receiver Renderer to draw with.
* @param engine Current GameEngine.
* @param playerID Current player ID.
*/
public FieldScatter(EventReceiver receiver, GameEngine engine, int playerID) {
this(receiver, engine, playerID, true);
}

/**
* Makes a new field explosion.
* @param receiver Renderer to draw with.
* @param engine Current GameEngine.
* @param playerID Current player ID.
* @param clearBlocks Clear the blocks in the field?
*/
public FieldScatter(EventReceiver receiver, GameEngine engine, int playerID, boolean clearBlocks) {
// Direction randomiser
Random rdm = new Random(engine.randSeed + engine.statistics.time);
lifeTime = 0;
Expand All @@ -49,16 +60,16 @@ public FieldScatter(EventReceiver receiver, GameEngine engine, int playerID) {
-1, 1, 1, PhysicsObject.ANCHOR_POINT_TL, blk.color
));

receiver.blockBreak(engine, playerID, j, i, blk);
if (clearBlocks) receiver.blockBreak(engine, playerID, j, i, blk);

blk.copy(eBlock);
if (clearBlocks) blk.copy(eBlock);
}
}
}
}

/**
* Makes a new field explostion.
* Makes a new field explosion. Destroys specfic blocks.
* @param receiver Renderer to draw with.
* @param engine Current GameEngine.
* @param playerID Current player ID.
Expand Down Expand Up @@ -99,7 +110,7 @@ public FieldScatter(EventReceiver receiver, GameEngine engine, int playerID, Arr
}

/**
* Makes a new field explostion.
* Makes a new field explosion. Destroys specfic blocks.
* @param receiver Renderer to draw with.
* @param engine Current GameEngine.
* @param playerID Current player ID.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.awt.Graphics2D;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.newdawn.slick.Music;
Expand All @@ -73,10 +74,17 @@ public class ResourceHolderCustomAssetExtension {

private int holderType;

/**
* Creates a new custom resource holder with 8 initial capacity.
*/
public ResourceHolderCustomAssetExtension() {
this(8);
}

/**
* Creates a new custom resource holder.
* @param initialCapacity Start capacity of the internal hashmaps.
*/
public ResourceHolderCustomAssetExtension(int initialCapacity) {
String mainClass = getMainClassName();
// log.info("MAIN CLASS: " + mainClass);
Expand All @@ -101,11 +109,30 @@ public ResourceHolderCustomAssetExtension(int initialCapacity) {
}
}

/**
* Gets the current instance's main class name.
* @return Main class name.
*/
public static String getMainClassName()
{
StackTraceElement[] trace = Thread.currentThread().getStackTrace();
if (trace.length > 0) {
return trace[trace.length - 1].getClassName();
/*
* Old, thread unsafe code.
*
* StackTraceElement[] trace = Thread.currentThread().getStackTrace();
* if (trace.length > 0) {
* return trace[trace.length - 1].getClassName();
* }
* return "Unknown";
*/

// New, thread-safe code:
Map<Thread, StackTraceElement[]> k = Thread.getAllStackTraces();
for (StackTraceElement[] g : k.values()) {
for (StackTraceElement i : g) {
if (i.getClassName().contains("NullpoMinoSlick") || i.getClassName().contains("NullpoMinoSwing") || i.getClassName().contains("NullpoMinoSDL")) {
return i.getClassName();
}
}
}
return "Unknown";
}
Expand Down

0 comments on commit 0c9ce1e

Please sign in to comment.