Skip to content

Commit

Permalink
Small changes to make DequeActionQueue thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
retrodaredevil committed May 7, 2021
1 parent e59f07b commit d90d517
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
6 changes: 6 additions & 0 deletions src/main/java/me/retrodaredevil/action/ActionQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ public interface ActionQueue extends SingleActiveActionHolder, ActionCollection

/**
* Ends the current action if there is one and removes it. This happens immediately.
* <p>
* Note: This is not thread safe
* @return true if there was a current action to end, false otherwise
*/
boolean removeCurrentAction();
/**
* Ends the current action and moves it to the end of the queue if there is one. This happens immediately.
* <p>
* NOTE: This requires the current action to be recyclable
* <p>
* Note: This is not thread safe
* @param doNothingIfEmpty true if you want to do nothing if the queue is empty, false otherwise.
* If set to true, the current action will not be ended (nothing will happen).
*/
Expand All @@ -36,6 +40,8 @@ public interface ActionQueue extends SingleActiveActionHolder, ActionCollection
* will allow you to stop the current running action and put another one in.
* <p>
* NOTE: This requires the current action to be recyclable
* <p>
* Note: This is not thread safe
* @return true if there was a current action to put at the front of the queue, false otherwise
*/
boolean moveCurrentToNext();
Expand Down
48 changes: 36 additions & 12 deletions src/main/java/me/retrodaredevil/action/DequeActionQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ public DequeActionQueue(boolean canRecycle, Deque<Action> actionQueue, boolean c

@Override
public Action getActiveAction() {
return currentAction;
synchronized (this) {
return currentAction;
}
}

@Override
public Collection<? extends Action> getActiveActions() {
Action currentAction = getActiveAction();
if(currentAction == null){
return Collections.emptySet();
}
Expand All @@ -57,18 +60,24 @@ public boolean add(Action action){
if(action == this){
throw new IllegalArgumentException();
}
actionQueue.addLast(action);
synchronized (this) {
actionQueue.addLast(action);
}
return true;
}

@Override
public boolean removeQueued(Action action) {
return actionQueue.remove(action);
synchronized (this) {
return actionQueue.remove(action);
}
}

@Override
public void clear() {
actionQueue.clear();
synchronized (this) {
actionQueue.clear();
}
}

/**
Expand All @@ -82,7 +91,9 @@ public boolean addBeginning(Action action){
if(action.isActive()){
throw new IllegalArgumentException("action cannot be active when you add it!");
}
actionQueue.addFirst(action);
synchronized (this) {
actionQueue.addFirst(action);
}
return true;
}

Expand All @@ -102,11 +113,16 @@ public boolean removeCurrentAction(){

@Override
public boolean moveCurrentToEnd(boolean doNothingIfEmpty){
if((doNothingIfEmpty && actionQueue.isEmpty()) || currentAction == null)
return false;
synchronized (this) {
if ((doNothingIfEmpty && actionQueue.isEmpty()) || currentAction == null) {
return false;
}
}

currentAction.end();
actionQueue.addLast(currentAction);
synchronized (this) {
actionQueue.addLast(currentAction);
}
currentAction = null;
return true;
}
Expand All @@ -116,7 +132,9 @@ public boolean moveCurrentToNext(){
return false;

currentAction.end();
actionQueue.addFirst(currentAction);
synchronized (this) {
actionQueue.addFirst(currentAction);
}
currentAction = null;
return true;
}
Expand All @@ -128,7 +146,9 @@ protected void onUpdate() {
}
private void updateAction(){
if(currentAction == null){
currentAction = actionQueue.poll();
synchronized (this) {
currentAction = actionQueue.poll();
}
}
if(currentAction != null){
currentAction.update();
Expand All @@ -154,14 +174,18 @@ protected void onEnd(boolean peacefullyEnded) {
}
if(clearQueuedOnEnd){
currentAction = null;
actionQueue.clear();
synchronized (this) {
actionQueue.clear();
}
}
}

@Override
protected void onIsDoneRequest() {
if (canBeDone) {
setDone(currentAction == null && actionQueue.isEmpty());
synchronized (this) {
setDone(currentAction == null && actionQueue.isEmpty());
}
}
}
}

0 comments on commit d90d517

Please sign in to comment.