Skip to content

Commit

Permalink
Add x_get_lock, synchronized changes (#1381)
Browse files Browse the repository at this point in the history
  • Loading branch information
LadyCailin authored Apr 12, 2024
1 parent eb3b3a0 commit a019b99
Show file tree
Hide file tree
Showing 10 changed files with 367 additions and 61 deletions.
94 changes: 94 additions & 0 deletions src/main/java/com/laytonsmith/PureUtilities/Quadruplet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.laytonsmith.PureUtilities;

import java.util.Objects;

/**
* Creates an object quadruplet. The hashcode and equals functions have been overridden to use the underlying object's hash
* code and equals combined. The underlying objects may be null.
*
* @param <A> The first object's type.
* @param <B> The second object's type.
* @param <C> The third object's type.
* @param <D> The fourth object's type.
*/
public class Quadruplet<A, B, C, D> {

private final A fst;
private final B snd;
private final C trd;
private final D frth;

/**
* Creates a new Quadruplet with the specified values.
*
* @param a
* @param b
* @param c
* @param d
*/
public Quadruplet(A a, B b, C c, D d) {
fst = a;
snd = b;
trd = c;
frth = d;
}

@Override
public String toString() {
return "<"
+ Objects.toString(fst) + ", "
+ Objects.toString(snd) + ", "
+ Objects.toString(trd) + ", "
+ Objects.toString(frth) + ">";
}

@Override
public int hashCode() {
int hash = 7;
hash = 47 * hash + Objects.hashCode(this.fst);
hash = 47 * hash + Objects.hashCode(this.snd);
hash = 47 * hash + Objects.hashCode(this.trd);
hash = 47 * hash + Objects.hashCode(this.frth);
return hash;
}

@Override
public boolean equals(Object obj) {
if(obj == null) {
return false;
}
if(getClass() != obj.getClass()) {
return false;
}
final Quadruplet<?, ?, ?, ?> other = (Quadruplet<?, ?, ?, ?>) obj;
if(!Objects.equals(this.fst, other.fst)) {
return false;
}
if(!Objects.equals(this.snd, other.snd)) {
return false;
}
if(!Objects.equals(this.trd, other.trd)) {
return false;
}
if(!Objects.equals(this.frth, other.frth)) {
return false;
}
return true;
}

public A getFirst() {
return fst;
}

public B getSecond() {
return snd;
}

public C getThird() {
return trd;
}

public D getFourth() {
return frth;
}
}
21 changes: 20 additions & 1 deletion src/main/java/com/laytonsmith/abstraction/AbstractConvertor.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,23 @@
import java.util.TimerTask;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;

public abstract class AbstractConvertor implements Convertor {

private final List<Runnable> shutdownHooks = new ArrayList<>();
private final List<Runnable> persistentShutdownHooks = new ArrayList<>();

@Override
public void addShutdownHook(Runnable r) {
shutdownHooks.add(r);
}

@Override
public void addPersistentShutdownHook(Runnable r) {
persistentShutdownHooks.add(r);
}

@Override
public void runShutdownHooks() {
// Fire off the shutdown event, before we shut down all the internal hooks
Expand All @@ -35,9 +42,21 @@ public Object _GetObject() {
});
Iterator<Runnable> iter = shutdownHooks.iterator();
while(iter.hasNext()) {
iter.next().run();
try {
iter.next().run();
} catch(Error e) {
Logger.getLogger(AbstractConvertor.class.getName()).severe(e.getMessage());
}
iter.remove();
}

for(Runnable r : persistentShutdownHooks) {
try {
r.run();
} catch(Error e) {
Logger.getLogger(AbstractConvertor.class.getName()).severe(e.getMessage());
}
}
}

/**
Expand Down
25 changes: 23 additions & 2 deletions src/main/java/com/laytonsmith/abstraction/Convertor.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,26 @@ public interface Convertor {
MCInventoryHolder CreateInventoryHolder(String id, String title);

/**
* Run whenever the server is shutting down (or restarting). There is no guarantee provided as to what thread the
* Runs whenever the server is shutting down (or reloading). There is no guarantee provided as to what thread the
* runnables actually run on, so you should ensure that the runnable executes it's actions on the appropriate thread
* yourself.
* yourself. Note that this shutdown hook will only run once, so if multiple reload events occur, this will not
* be registered for the second run, unless you specifically add it yourself. If you need a shutdown hook to run
* every time, and only want to register it once, see {@link #addPersistentShutdownHook}.
*
* @param r
*/
void addShutdownHook(Runnable r);

/**
* Runs whenever the server is shutting down (or reloading). There is no guarantee provided as to what thread the
* runnables actually run on, so you should ensure that the runnable executes it's actions on the appropriate thread
* yourself. Note that this shutdown hook will never dequeue, so if multiple reload events occur, this will run
* every time, and so you should only call this once (i.e. from a static context). If you need a shutdown hook to
* dequeue after run, see {@link #addShutdownHook}.
* @param r
*/
void addPersistentShutdownHook(Runnable r);

/**
* Runs all the registered shutdown hooks. This should only be called by the shutdown mechanism. After running, each
* Runnable will be removed from the queue.
Expand Down Expand Up @@ -294,4 +306,13 @@ public interface Convertor {
* @return
*/
public MCTransformation GetTransformation(Quaternionf leftRotation, Quaternionf rightRotation, Vector3f scale, Vector3f translation);

/**
* Returns true if this is the main thread of the application. This is only applicable in some managed environments,
* in other environments where this doesn't matter, this will always return false (i.e. all threads are considered
* equally important/unimportant). If this returns true, this means that the current thread is for instance the
* UI thread, and thus should not be blocked on.
* @return
*/
public boolean IsMainThread();
}
4 changes: 4 additions & 0 deletions src/main/java/com/laytonsmith/abstraction/StaticLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ public static MCPlugin GetPlugin() {
return convertor.GetPlugin();
}

public static boolean IsMainThread() {
return convertor.IsMainThread();
}

public static synchronized Convertor GetConvertor() {
return convertor;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -910,4 +910,10 @@ public MCNamespacedKey GetNamespacedKey(String key) {
public MCTransformation GetTransformation(Quaternionf leftRotation, Quaternionf rightRotation, Vector3f scale, Vector3f translation) {
return new BukkitMCTransformation(new Transformation(translation, leftRotation, scale, rightRotation));
}

@Override
public boolean IsMainThread() {
return Bukkit.isPrimaryThread();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static ResourceTypes getResourceByType(Class<?> type) {
private static final Map<Long, CResource<?>> RESOURCES = new HashMap<>();

static {
StaticLayer.GetConvertor().addShutdownHook(new Runnable() {
StaticLayer.GetConvertor().addPersistentShutdownHook(new Runnable() {

@Override
public void run() {
Expand Down
Loading

0 comments on commit a019b99

Please sign in to comment.